![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef tooltipsH
#define tooltipsH
#include <commctrl.h>
#include <windows.h>
namespace NSTS {
struct ToolTipsItem{
HWND hwnd;
TCHAR* pszTip;
};
class CToolTips {
public:
CToolTips (void) {
m_hSelf = NULL;
m_hInst = NULL;
};
~CToolTips (void) {
Destroy ();
};
public:
bool Init (HINSTANCE hInst, DWORD dwStyle = WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP) {
INITCOMMONCONTROLSEX iccex;
/* INITIALIZE COMMON CONTROLS */
iccex.dwICC = ICC_WIN95_CLASSES|ICC_COOL_CLASSES|ICC_BAR_CLASSES|ICC_USEREX_CLASSES;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&iccex);
if (hInst == NULL) {
return false;
}
Destroy ();
/* CREATE A TOOLTIP WINDOW */
m_hSelf = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
dwStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
NULL
);
if (m_hSelf == NULL) {
return false;
}
SetWindowPos(m_hSelf,
HWND_TOPMOST,
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
m_hInst = hInst;
return true;
};
bool Init (HINSTANCE hInst, HWND hwnd, LPTSTR pszText, DWORD dwStyle = WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP) {
if (hwnd != NULL && IsWindow (hwnd) && hInst != NULL) {
if (!Init (hInst, dwStyle)) {
return false;
}
return AddItem (hwnd, pszText);
}
return false;
};
bool Init (HINSTANCE hInst, ToolTipsItem items[], int iSize, DWORD dwStyle = WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP) {
if (hInst != NULL && items != NULL && iSize >= 0) {
if (Init (hInst, dwStyle)) {
bool fRet = true;
for (int i = 0; i < iSize; ++i) {
if (!AddItem (items[i].hwnd, items[i].pszTip)) {
fRet = false;
}
}
if (!fRet) {
Destroy ();
}
return fRet;
}
}
return false;
};
void Destroy (void) {
if (m_hSelf != NULL) {
DestroyWindow (m_hSelf);
m_hSelf = NULL;
}
m_hInst = NULL;
};
bool AddItem (HWND hwnd, LPTSTR pszText) {
if (m_hSelf != NULL && IsWindow (m_hSelf) &&
hwnd != NULL && IsWindow (hwnd)) {
RECT rc;
TOOLINFO ti = {0};
GetClientRect (hwnd, &rc);
#if _WIN32_WINN != 0x0500
ti.cbSize = sizeof (TOOLINFO) - sizeof (void*);
#else
ti.cbSize = sizeof (TOOLINFO);
#endif
ti.hinst = m_hInst;
ti.hwnd = hwnd;
ti.uFlags = TTF_SUBCLASS;
ti.uId = 0;
ti.lpszText = pszText;
ti.rect.left = rc.left;
ti.rect.top = rc.top;
ti.rect.right = rc.right;
ti.rect.bottom = rc.bottom;
return SendMessage (m_hSelf, TTM_ADDTOOL, (WPARAM)0, (LPARAM)&ti);
}
return false;
};
bool SetItem (HWND hwnd, LPTSTR pszText) {
if (hwnd != NULL && IsWindow (hwnd) &&
m_hSelf != NULL && IsWindow (m_hSelf)) {
TOOLINFO ti = {0};
#if _WIN32_WINN != 0x0500
ti.cbSize = sizeof (TOOLINFO) - sizeof (void*);
#else
ti.cbSize = sizeof (TOOLINFO);
#endif
ti.hinst = m_hInst;
ti.hwnd = hwnd;
ti.uId = 0;
ti.lpszText = pszText;
SendMessage (m_hSelf, TTM_UPDATETIPTEXT, (WPARAM)0, (LPARAM)&ti);
Update ();
return true;
}
return false;
};
bool DelItem (HWND hwnd){
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
TOOLINFO ti = {0};
#if _WIN32_WINN != 0x0500
ti.cbSize = sizeof (TOOLINFO) - sizeof (void*);
#else
ti.cbSize = sizeof (TOOLINFO);
#endif
ti.hwnd = hwnd;
ti.uId = 0;
SendMessage (m_hSelf, TTM_DELTOOL, (WPARAM)0, (LPARAM)&ti);
return true;
}
return false;
};
bool UseRectangle (void) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
DWORD dwStyle = ::GetWindowLong (m_hSelf, GWL_STYLE);
LONG lRet;
dwStyle &= ~TTS_BALLOON;
lRet = ::SetWindowLong (m_hSelf, GWL_STYLE, dwStyle);
return lRet ? true : false;
}
return false;
};
bool UseBalloon (void) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
DWORD dwStyle = ::GetWindowLong (m_hSelf, GWL_STYLE);
LONG lRet;
dwStyle |= TTS_BALLOON | TTS_NOFADE | TTS_NOANIMATE;
lRet = ::SetWindowLong (m_hSelf, GWL_STYLE, dwStyle);
Update ();
return lRet ? true : false;
}
return false;
};
COLORREF GetBkColor (void) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
return (COLORREF)SendMessage (m_hSelf, TTM_GETTIPBKCOLOR, (WPARAM)0, (LPARAM)0);
}
return RGB (0,0,0);
};
bool SetBkColor (COLORREF clrBk) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
SendMessage (m_hSelf, TTM_SETTIPBKCOLOR, (WPARAM)clrBk, (LPARAM)0);
return true;
}
return false;
};
COLORREF GetTextColor (void) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
return (COLORREF)SendMessage (m_hSelf, TTM_GETTIPTEXTCOLOR, (WPARAM)0, (LPARAM)0);
}
return RGB (0, 0, 0);
};
bool SetTextColor (COLORREF clrText) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
SendMessage (m_hSelf, TTM_SETTIPTEXTCOLOR, (WPARAM)clrText, (LPARAM)0);
return true;
}
return false;
};
void Update (void) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
SendMessage (m_hSelf, TTM_UPDATE, (WPARAM)0, (LPARAM)0);
}
};
bool SetMaxWidth (INT iWidth) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
SendMessage (m_hSelf, TTM_SETMAXTIPWIDTH, (WPARAM)0, (LPARAM)iWidth);
return true;
}
return false;
};
enum EIcon { eNone = 0, eInfo, eWarning, eError };
bool SetTitle (EIcon eIcon, LPCTSTR pszTitle) {
if (m_hSelf != NULL && IsWindow (m_hSelf)) {
return SendMessage (m_hSelf, TTM_SETTITLE, (WPARAM)eIcon, (LPARAM)pszTitle);
}
return false;
};
HWND GetHwnd (void) const { return m_hSelf; };
private:
HWND m_hSelf;
HINSTANCE m_hInst;
};
}
#endif // tooltipsH
Usage:
NSTS::CToolTips g_tt, g_tt2;
NSTS::CToolTips g_tt3;
WM_INITDIALOG:
/** 初始化 */
assert (g_tt.Init (hInst));
assert (g_tt2.Init (hInst, GetDlgItem (hwndDlg, IDC_BTN_SETHEIGHT), TEXT ("IDC_BTN_SETHEIGHT")));
NSTS::ToolTipsItem ttis[] = {
{ GetDlgItem (hwndDlg, IDC_BTN_REMOVECTRL), TEXT ("REMOVE CTRL")},
{ GetDlgItem (hwndDlg, IDC_BTN_SETICON), TEXT ("SET ICON")}
};
assert (g_tt3.Init (hInst, ttis, 2));
/* 添、删、改*/
assert (g_tt.AddItem (GetDlgItem (hwndDlg, IDC_BTN_RESIZE), TEXT ("resize")));
g_tt.SetItem (GetDlgItem (hwndDlg, IDC_BUTTON1), TEXT ("222"));
g_tt.DelItem (GetDlgItem (hwndDlg, IDC_BUTTON1));
/** 添加标题和图标 */
assert (g_tt2.SetTitle (NSTS::CToolTips::eError, TEXT ("TITLE")));
/** 颜色 */
assert (g_tt.SetBkColor (RGB (128, 0, 0)));
assert (g_tt.SetTextColor (RGB (0, 128, 0)));
/** 形状,球或矩形 */
assert (g_tt.UseBalloon ());
assert (g_tt.UseRectangle ());