//我们知道,当我们在使用一个软件时,我们移动鼠标,当光标移到一个按钮或其他控件上时,一般会出现一个矩形的提示框,列出相关信息,下面我们利用提示控件实现这一功能。
下面的代码我是参考其他资料完善的。
#include "stdafx.h"
#include "resource.h"
#include "Windows.h"
#include "tchar.h"
#include "commctrl.h"
#pragma comment(lib,"comctl32.lib")
TCHAR ToolTipsClassName[] = _T("Tooltips_class32");
TCHAR MainDialogText1[] = _T("This is the upper left area of the dialog");
TCHAR MainDialogText2[] = _T("This is the upper right area of the dialog");
TCHAR MainDialogText3[] = _T("This is the lower left area of the dialog");
TCHAR MainDialogText4[] = _T("This is the lower right area of the dialog");
HINSTANCE g_hInstance;
HWND hwndTool;
void SetDlgToolArea(HWND hDlg,LPTOOLINFO pTi,LPSTR lpText,DWORD id,RECT *lprect)
{
if(id == 0)
{
pTi->rect.left = 0;
pTi->rect.top = 0;
pTi->rect.right = (lprect->right - lprect->left)/2;
pTi->rect.bottom = (lprect->bottom - lprect->top)/2;
}
else if(id == 1)
{
pTi->rect.left = (lprect->right - lprect->left)/2 + 1;
pTi->rect.top = 0;
pTi->rect.right = (lprect->right - lprect->left);
pTi->rect.bottom = (lprect->bottom - lprect->top);
}
else if(id == 2)
{
pTi->rect.top = (lprect->bottom - lprect->top)/ 2 + 1;
pTi->rect.left = 0;
pTi->rect.right = (lprect->right - lprect->left)/2;
pTi->rect.bottom = lprect->bottom - lprect->top;
}
else
{
pTi->rect.left = (lprect->right - lprect->left)/2 + 1;
pTi->rect.top = (lprect->bottom - lprect->top)/2 + 1;
pTi->rect.bottom = lprect->bottom - lprect->top;
pTi->rect.right = lprect->right - lprect->left;
}
pTi->lpszText = lpText;
SendMessage(hwndTool,TTM_ADDTOOL,NULL,(LPARAM)pTi);
}
BOOL CALLBACK EnumChild( HWND hwnd,
LPARAM lParam
)
{
CHAR buffer[256] = {0};
LPTOOLINFO pTi = (LPTOOLINFO)lParam;
pTi->uId = (UINT)hwnd;
pTi->uFlags |= TTF_IDISHWND;
GetWindowText(hwnd,buffer,255);
pTi->lpszText = buffer;
SendMessage(hwndTool,TTM_ADDTOOL,NULL,(LPARAM)pTi);
return TRUE;
}
INT_PTR CALLBACK DialogProc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
TOOLINFO ti;
DWORD id;
RECT rect;
switch(uMsg)
{
case WM_INITDIALOG:
InitCommonControls();
hwndTool = CreateWindowEx(NULL,ToolTipsClassName,NULL,TTS_ALWAYSTIP,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,NULL);
id = 0;
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = hwndDlg;
GetWindowRect(hwndDlg,&rect);
SetDlgToolArea(hwndDlg,&ti,MainDialogText1,id,&rect);
id++;
SetDlgToolArea(hwndDlg,&ti,MainDialogText2,id,&rect);
id++;
SetDlgToolArea(hwndDlg,&ti,MainDialogText3,id,&rect);
id++;
SetDlgToolArea(hwndDlg,&ti,MainDialogText4,id,&rect);
EnumChildWindows(hwndDlg,EnumChild,(LPARAM)&ti);
break;
case WM_CLOSE:
EndDialog(hwndDlg,NULL);
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
g_hInstance = hInstance;
DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_ABOUTBOX),NULL,(DLGPROC)DialogProc,NULL);
return 0;
}
ps:MSDN上是这样写的,已经调试过,可以直接使用:
void CreateToolTipForRect(HWND hCtrl,TCHAR *szText)//hCtrl是控件的句柄,szText是鼠标滑动到该控件上显示的文本
{
// Create a tooltip.
HINSTANCE hInst=(HINSTANCE)GetWindowLong(hCtrl,GWL_HINSTANCE);
HWND hwndTT = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hCtrl, NULL, hInst,NULL);
SetWindowPos(hwndTT, HWND_TOPMOST,
0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
// Set up "tool" information.
// In this case, the "tool" is the entire parent window.
TOOLINFO ti = { 0 };
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = hCtrl;
ti.hinst = hInst;
ti.lpszText = szText;
GetClientRect (hCtrl, &ti.rect);
// Associate the tooltip with the "tool" window.
SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
}