工具提示类






4.95/5 (18投票s)
一个简单的工具提示类,可以帮助显示气泡和简单的工具提示,用于控件和系统托盘窗口。
引言
最近我正在做一个基于 Win32 对话框应用程序的项目。我需要为它提供一个工具提示。我在 CodeProject 和其他网站上为此搜索了很多,但只找到了 MFC 应用程序的帮助。所以我自己创建了一个类来轻松使用工具提示。我认为将内容放在 CodeProject 网站上是个好主意,以便其他人也可以使用它。我已经创建了这些类来帮助显示控件的工具提示。它支持简单和气泡工具提示。以下是演示应用程序的截图。

Using the Code
我创建了一个简单的类 gToolTip
,其中包含显示控件工具提示的所有代码。要在您的应用程序中使用这些类,只需在您的项目中包含 gToolTip.cpp 和 gToolTip.h。然后只需调用 AddTip
函数并传入参数,工具提示就会显示在控件上。
以下是显示如何调用 AddTip
函数的代码。要将工具提示分配给任何控件
//AddTip( HWND,HINSTANCE,TCHAR*,UINT,BOOL = FALSE);
//HwndDlg = Handle of the Window(in my case it's a handle to Dialog)
//hInst = Instance of the Window.
//tip = The Tool tip you wish to have on you control
//UINT = ID of the Control for which you want the tool tip to be displayed
//TRUE = Balloon Tool tip will be displayed
//this will show balloon tool tip for the IDC_EDIT1 Control
gToolTip::AddTip(hWndDlg,hInst,tip,IDC_EDIT1,TRUE);
//This will show the normal Tool tip for the IDC_EDIT1
gToolTip::AddTip(hWndDlg,hInst,tip,IDC_EDIT1);
在 gToolTip 类内部
控件工具提示
工具提示是 Commctrl.h 文件中定义的常用控件。因此,请在您的项目中包含此头文件和 comctl32.lib。
现在,在使用任何常用控件之前,您需要初始化它们。以下是初始化工具提示常用控件的代码行:
//You need to initialize the structure before you use any of the common controls
INITCOMMONCONTROLSEX icc;
HWND hwndTip;
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC = ICC_TAB_CLASSES ; //Load tab and ToolTip control classes
InitCommonControlsEx(&icc); //Will initialize the common control
好的。现在您已经初始化了工具提示控件。现在您需要为工具提示创建一个窗口,其类为 Tooltip
类。以下代码显示了如何执行此操作:
hwndTip =CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP |TTS_BALLOON,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
hWnd, //Handle of the Parent window.
//(i.e. Window for which you want Tooltip)
NULL,
hInst,
NULL);
如果您想要一个简单的工具提示,请不要在上面的代码中使用 TTS_BALLON
样式。
现在只需初始化工具信息 (TOOLINFO
) 结构并添加工具提示。以下代码显示了如何执行此操作:
TOOLINFO ti;
ti.cbSize = sizeof(TOOLINFO);
//Flags TTF_IDISHWND show that the uID is the handle to control
ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
//Handle of the Control for which you want to Tooltip to be displayed
ti.uId =(UINT)GetDlgItem(hWnd,id);
//Handle of the parent window (The window in which the Control resides)
ti.hwnd = hWnd;
ti.hinst = hInst;
ti.lpszText = Tip; //Text you want as a Tooltip
//Below is the Rectangle for ToolTip. But It will be ignored
//If you use TTF_IDISHWND in Flag
//ti.rect.left = ti.rect.top = ti.rect.bottom = ti.rect.right = 0;
//Will Activate the tooltip window
SendMessage(hwndTip,TTM_ACTIVATE,TRUE,0);
//will Add the Tooltip Window
if(!SendMessage(hwndTip,TTM_ADDTOOL,0,(LPARAM)&ti)){
MessageBox(NULL,L"Couldn't create the ToolTipcontrol.",L"Error",MB_OK);
}
系统托盘图标的气泡工具提示
任务栏图标中的气泡工具提示受 Shell32.dll 版本 5.0 及更高版本支持。因此,要在 NOTIFYICONDATA
结构中激活气泡工具提示成员,请包含以下内容:
#define _WIN32_IE 0x0500 // I have used this in my code
#define _WIN32_IE 0x0600
现在,以下代码行描述了如何将应用程序放入任务栏并显示气泡工具提示。
NOTIFYICONDATA nfd; //Structure that you need to initialize
nfd.cbSize = sizeof(NOTIFYICONDATA);
nfd.hWnd = hWndDlg; //Handle of the Window
wcscpy(nfd.szTip,L"Tip.. " ); //Normal Tool Tip you want to display
wcscpy(nfd. szInfoTitle, _T(" szInfoTitle.")); //Will display Title for
//Tooltip as show in Image
wcscpy(nfd.szInfo, _T("szInfo"));//The Text will appear below Title in
//Balloon Tool Tip
//Time for which the Balloon should appear.The Value is in milliseconds.
//1000*10 < uTimeout < 1000*30. Only this values will work
nfd.uTimeout = TimeOut*1000;
// I haven't used the below things.But if you want write down
// the User Message you want to handle.
//nfd.uCallbackMessage
nfd.uFlags = NIF_ICON | NIF_TIP | NIF_INFO;
nfd.dwInfoFlags =NIIF_INFO; //Icon you want for your balloon
nfd.hIcon = hIcon; //Icon that you want to show
//in System Tray
if( !Shell_NotifyIcon(NIM_ADD,&nfd)) //Will add the Icon in System Tray
MessageBox(NULL,L"Not able to add Icon in system Tray",L"Error" ,0);
历史
- 2007 年 10 月 03 日:首次提交文章
- 2007 年 10 月 08 日:更新以描述
gToolTip
类