我蒸一个更简单,更高效的解决方案来创建一个工具提示单个菜单项:
#include <windows.h>
#include <commctrl.h>
#define ID_BUTTON 123
HWND hToolTip;
TOOLINFO ti = {0};
// WndProc
case WM_CREATE: {
// create menu
HMENU hmenu = CreateMenu();
AppendMenu(hmenu, MF_HELP, ID_BUTTON, "?");
SetMenu(hWnd, hmenu);
InitCommonControls(); // necessary in Windows XP to register TOOLTIPS_CLASS
// create tooltip
hToolTip = CreateWindow(TOOLTIPS_CLASS, 0, WS_POPUP, 0, 0, 0, 0, hWnd, 0, 0, 0);
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = hWnd; // main window handle
ti.uId = (UINT)ID_BUTTON; // distinguish it from eventual other tooltips
ti.lpszText = "My tooltip";
SendMessage(hToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
break;
}
case WM_SIZE:
ti.uId = (UINT)ID_BUTTON;
// get new coordinates of item
GetMenuItemRect(hWnd, GetMenu(hWnd), 0, &ti.rect);
//^zero-based position of the menu item
// convert from screen to client coordinates
MapWindowPoints(NULL, hWnd, (LPPOINT)&ti.rect, 2);
//^desktop handle ^POINT structures that is one RECT
SendMessage(hToolTip, TTM_NEWTOOLRECT, 0, (LPARAM)&ti);
break;
我用InitCommonControls
因为是短的作品,但也已过时,InitCommonControlsEx
应改为使用。
上面的代码也在Windows XP上测试过:它能正常工作,但后面的GetLastError
返回1309 ERROR_NO_IMPERSONATION_TOKEN
。
为了解决这个问题,create a manifest file和资源(.RC)文件中添加一行:
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "YourApp.exe.manifest"
不知道这一点,但你尝试任何WM_MOUSEHOVER,WM_MOUSEMOVE或WM_NCMOUSEMOVE的事件,无论他们的工作? – Laszlo
@Laszlo:'WM_NCMOUSEMOVE'是唯一有用的:'wParam'总是在整个菜单上,但是'lParam' x和y鼠标坐标......我可以用'GetMenuItemRect()'来标识按钮! – Salvador
只要菜单项*被选中*,无论是从滚动/悬停在菜单项上,还是使用键盘,都会发送WM_MENUSELECT。这是处理显示工具提示的正确消息。它工作正常。如果您无法使用它,请显示您的实际代码。 –