1、前言
在编写dll工程的时候,我习惯生成一个MFC的exe进行测试dll的功能。但是有的时候dll中的某些接口的某个参数支持多种配置,这个时候去找对应的文档说明感觉比较麻烦,因此最好将文档中的对应说明在界面中进行显示,这就避免了频繁的查看文档。
二、主要流程
1、定义CToolTipCtrl对象
在需要使用提示的界面类中顶一个CToolTipCtrl对象
2、设置提示信息
在对应界面类的OnInitDialog函数中,实现如下的代码:
//提示信息
m_Mytip.Create(this);
CString strTip = _T("1——connect,\r\n 2——accept,\r\n 3——disconn,\r\n ");
strTip.AppendFormat(_T("%s"), _T("1——url,\r\n 2——data,\r\n 3——ip_data,\r\n"));
strTip.AppendFormat(_T("%s"),_T("0x40——dns,\r\n 0x80——smb,\r\n 0x1000——cb,\r\n"));
strTip.AppendFormat(_T("%s"),_T("0x2000——add_rule,\r\n 0x4000——del_rule,\r\n 0x8000——notify_caller,\r\n"));
strTip.AppendFormat(_T("%s"),_T("0x400000——block,\r\n 0x800000——allow\r\n"));
m_Mytip.AddTool(GetDlgItem(IDC_EDIT_FLAGS), strTip);
strTip = _T("1——in,\r\n 2——out,\r\n 3——both,\r\n 17——send,\r\n 18——recv\r\n");
m_Mytip.AddTool(GetDlgItem(IDC_EDIT_DIRECTION), strTip);
strTip = _T("1——ICMP,\r\n 6——TCP,\r\n 17——UDP\r\n");
m_Mytip.AddTool(GetDlgItem(IDC_EDIT_PROT), strTip);
strTip = _T("2——AF_INET,\r\n 23——AF_INET6\r\n");
m_Mytip.AddTool(GetDlgItem(IDC_EDIT_IP_FAMILY), strTip);
m_Mytip.SetDelayTime(TTDT_INITIAL, 100); //设置延迟
m_Mytip.SetDelayTime(TTDT_AUTOPOP, 20000); //显示提示时间
m_Mytip.SetMaxTipWidth(200); //设置显示宽度,超长自动换行
m_Mytip.SetTipTextColor( RGB(85,123,205) ); //设置提示文本的颜色
m_Mytip.SetTipBkColor( RGB(255,255,255)); //设置提示框的背景颜色
m_Mytip.Activate(TRUE); //设置是否启用提示
3、重写 PreTranslateMessage函数
打开工程的Class View(Ctrl+Shift+C),找到使用CToolTipCtrl类的界面类,打开其属性,点击Overrides
选择PreTranslateMessage,此时在对应界面类中会生成一下内容:
//头文件
virtual BOOL PreTranslateMessage(MSG* pMsg);
//cpp文件
BOOL CSetNetDrvRuleDialog::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::PreTranslateMessage(pMsg);
}
4、鼠标在控件上显示提示内容
在PreTranslateMessage中添加代码
BOOL CSetNetDrvRuleDialog::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_MOUSEMOVE )
m_Mytip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
到此,当用户将鼠标移到到对应控件上时,会显示对应的提示信息