VC++ ToolTip的简单使用

    1、在基于对话框的MFC应用程序中使用Tooltip,首先在Dlg类的头文件中定义一个变量:  

     CToolTipCtrl m_iToolTips;

    2、在Dlg类的OnInitDialog中添加代码:   

       EnableToolTips(TRUE);
       m_iToolTips.Create(this);
       m_iToolTips.Activate(TRUE);
       m_iToolTips.SetDelayTime(150);
       m_iToolTips.AddTool(GetDlgItem(IDC_BTN_SELECT), _T("选择ocx/dll控件"));
       m_iToolTips.AddTool(GetDlgItem(IDC_EDIT_OCX_PATH), _T("ocx/dll控件路径"));
       m_iToolTips.AddTool(GetDlgItem(IDC_BTN_REGISTER), _T("注册"));
       m_iToolTips.AddTool(GetDlgItem(IDC_BTN_UNREGISTER), _T("反注册"));
       m_iToolTips.AddTool(GetDlgItem(IDC_BTN_ISREGISTED), _T("是否注册"));
       m_iToolTips.SetTipBkColor(RGB(255,255,255));    //背景色为白色
       m_iToolTips.SetTipTextColor(RGB(0,0,0));         //字体颜色为黑色

    3、重载PreTranslateMessage函数

     BOOL CControlRegisterDlg::PreTranslateMessage( MSG* pMsg )
     {
        switch(pMsg->message)
        {
        case WM_MOUSEMOVE:
            m_iToolTips.RelayEvent(pMsg);
            break;
        default:
            break;
        }
        return CDialog::PreTranslateMessage(pMsg);
     }

    4、编译运行

    

    ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl。

   CToolTipCtrl是用来显示单行文本的弹出框,可以给继承自CFrameWnd(提供了一个缺省的TTN_NEEDTEXT消息处理函数)的Windows控件添加一些提示信息。要使用它,包含3个步骤:

    • Enabling Tool Tips
    • Handling TTN_NEEDTEXT Notification for Tool Tips
    • The TOOLTIPTEXT Structure

   也就是说:

   第一步需要先打开这个功能(Tool Tips)。EnableToolTips

   第二步需要处理TTN_NEEDTEXT消息,并不是必须的。

   第三步是利用TOOLTIPTEXT结构体提供的信息,设置提示内容。AddTool

   CToolTipCtrl控件提供的功能只限于文本显示相关操作,对于复杂的ToolTip功能该控件可能满足不了要求,所以需要自定义ToolTips控件。  

   相关实现可参考:https://www.codeproject.com/Articles/18382/Custom-ToolTips-for-MFC-Projects

  补充:上述基本使用对于模态对话框正常,但是对于非模态对话框,PreTranslateMessage函数并没有被调用,那么非模态对话框如何响应PreTranslateMessage函数呢?使用钩子函数来实现:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
class CMyApp : public CWinApp
{
public:
    BOOL InitInstance();
    
int ExitInstance();

    
static LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam);
    HHOOK  m_hHook;

};


LRESULT CALLBACK CMyApp::GetMessageProc(
int nCode, WPARAM wParam, LPARAM lParam)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    LPMSG lpMsg = (LPMSG)lParam;

    
if(AfxGetApp()->PreTranslateMessage(lpMsg))
    {
        lpMsg->message = WM_NULL;
        lpMsg->lParam = 0L;
        lpMsg->wParam = 
0;
    }

    
// Passes the hook information to the next hook procedure in the current hook chain.
    return ::CallNextHookEx(theApp.m_hHook, nCode, wParam, lParam);
}

BOOL CMyApp::InitInstance()
{
    BOOL bInit = CWinApp::InitInstance();
    
if (bInit)
    {
        m_hHook = ::SetWindowsHookEx(WH_GETMESSAGE,
                                     GetMessageProc,
                                     AfxGetInstanceHandle(),
                                     GetCurrentThreadId());

        ASSERT(m_hHook);
    }

    
return bInit;
}

int CMyApp::ExitInstance()
{
    UnhookWindowsHookEx(m_hHook);
    
return CWinApp::ExitInstance();
}
原文地址:https://www.cnblogs.com/MakeView660/p/6814339.html