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函數(shù)
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消息處理函數(shù))的Windows控件添加一些提示信息。要使用它,包含3個步驟:
-
- Enabling Tool Tips
- Handling TTN_NEEDTEXT Notification for Tool Tips
- The TOOLTIPTEXT Structure
也就是說:
第一步需要先打開這個功能(Tool Tips)。EnableToolTips
第二步需要處理TTN_NEEDTEXT消息,并不是必須的。
第三步是利用TOOLTIPTEXT結(jié)構(gòu)體提供的信息,設(shè)置提示內(nèi)容。AddTool
?
CToolTipCtrl控件提供的功能只限于文本顯示相關(guān)操作,對于復雜的ToolTip功能該控件可能滿足不了要求,所以需要自定義ToolTips控件。
相關(guān)實現(xiàn)可參考:https://www.codeproject.com/Articles/18382/Custom-ToolTips-for-MFC-Projects
補充:上述基本使用對于模態(tài)對話框正常,但是對于非模態(tài)對話框,PreTranslateMessage函數(shù)并沒有被調(diào)用,那么非模態(tài)對話框如何響應PreTranslateMessage函數(shù)呢?使用鉤子函數(shù)來實現(xiàn):
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(); } |
轉(zhuǎn)載于:https://www.cnblogs.com/MakeView660/p/6814339.html
總結(jié)
以上是生活随笔為你收集整理的VC++ ToolTip的简单使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笔记本(华硕UL80VT)软件超频set
- 下一篇: C和C++的关键字区别