\Win32 API
tooltip_class.cpp
#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include#include #pragma comment(lib, "comctl32.lib") class CToolTip { public: CToolTip(HWND hWndParent); virtual ~CToolTip(); LRESULT SetFont(CONST LOGFONT *lplf); LRESULT SetMaxTipWidth(int iWidth); LRESULT SetTipTextColor(COLORREF clr); LRESULT SetBkTipColor(COLORREF clr); LRESULT AddToolTipToWnd(HWND hWnd,LPSTR lpszToolTip); HWND hToolTip; private: static CToolTip *pThis; static LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam); HHOOK hMsgHook; HWND hWndParent; }; CToolTip * CToolTip::pThis = NULL; CToolTip::CToolTip(HWND hWndParent) { if (hWndParent == NULL) return; INITCOMMONCONTROLSEX icx; icx.dwSize = sizeof(icx); icx.dwICC = ICC_BAR_CLASSES; InitCommonControlsEx(&icx); CToolTip::hWndParent = hWndParent; CToolTip::pThis = this; DWORD dwStyle = WS_POPUP | WS_BORDER | TTS_ALWAYSTIP; DWORD dwExStyle = WS_EX_TOOLWINDOW | WS_EX_TOPMOST; hToolTip = CreateWindowEx(dwExStyle, TOOLTIPS_CLASS, NULL, dwStyle, 0, 0, 0, 0, hWndParent, NULL, GetModuleHandle(NULL),NULL); if (!hToolTip) { MessageBox(hWndParent,"Error Creating ToolTip Control","CToolTip",MB_OK | MB_ICONSTOP); return; } hMsgHook = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,NULL, GetCurrentThreadId()); } CToolTip::~CToolTip() { UnhookWindowsHookEx(hMsgHook); } LRESULT CALLBACK CToolTip::GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode < 0) return CallNextHookEx(pThis->hMsgHook, nCode, wParam, lParam); switch (((MSG*)lParam)->message) { case WM_MOUSEMOVE: { if (IsChild(pThis->hWndParent,((MSG*)lParam)->hwnd)) { SendMessage(pThis->hToolTip, TTM_RELAYEVENT, 0,lParam); } } break; default: break; } return (CallNextHookEx(pThis->hMsgHook, nCode, wParam, lParam)); } LRESULT CToolTip::AddToolTipToWnd(HWND hWnd, LPSTR lpszToolTip) { TOOLINFO ti; ti.cbSize = sizeof(TOOLINFO); ti.uFlags = TTF_IDISHWND; ti.hwnd = hWndParent; ti.uId = (UINT)hWnd; ti.lpszText = lpszToolTip; return SendMessage(hToolTip, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti); } LRESULT CToolTip::SetBkTipColor(COLORREF rgb) { return SendMessage(hToolTip, TTM_SETTIPBKCOLOR,(WPARAM)rgb,0); } LRESULT CToolTip::SetTipTextColor(COLORREF rgb) { return SendMessage(hToolTip, TTM_SETTIPTEXTCOLOR,(WPARAM)rgb,0); } LRESULT CToolTip::SetMaxTipWidth(int iWidth) { return SendMessage(hToolTip, TTM_SETMAXTIPWIDTH, 0,(LPARAM)iWidth); } LRESULT CToolTip::SetFont(CONST LOGFONT *lplf) { HFONT hfont = CreateFontIndirect(lplf); return SendMessage(hToolTip, WM_SETFONT, (WPARAM)hfont, 0L); }