如何在不受限于工具的情况下使用 Windows ToolTip Control
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5896030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Windows ToolTip Control without bounding to a tool
提问by MorrisLiang
I want to use the native windows tooltip control (pure Win32 API, no MFC stuff).
我想使用本机 Windows 工具提示控件(纯 Win32 API,没有 MFC 的东西)。
I read the doc, it seems that I have to send a TTM_ADDTOOL message to bond a tool to the tooltip control. Only after that can I send TTM_TRACKACTIVATE & TTM_TRACKPOSITION to show the tooltip.
我阅读了文档,似乎必须发送 TTM_ADDTOOL 消息才能将工具绑定到工具提示控件。只有在那之后我才能发送 TTM_TRACKACTIVATE & TTM_TRACKPOSITION 来显示工具提示。
But I want to display the tooltip anywhere I want it to be. For example, when the mouse hovers over a region of my window. This region is not a tool in the eye of Windows, it's just a region in my window.
但我想在我想要的任何地方显示工具提示。例如,当鼠标悬停在我的窗口区域上时。这个区域不是 Windows 眼中的工具,它只是我窗口中的一个区域。
Perhaps I can bond the window to the tooltip control, but, doesn't this mean that I have to bond every window I created to the tooltip control?
也许我可以将窗口绑定到工具提示控件,但是,这是否意味着我必须将我创建的每个窗口绑定到工具提示控件?
Is there an easy solution so that I don't have to send TTM_ADDTOOL messages for every window?
有没有一个简单的解决方案,这样我就不必为每个窗口发送 TTM_ADDTOOL 消息?
I actually have written some code, but the tooltip just doesn't appear. Anders' answer solves some questions actually. And after I poke around my code, I make it work.
我实际上已经写了一些代码,但是工具提示没有出现。安德斯的回答实际上解决了一些问题。在我浏览了我的代码之后,我让它工作了。
In case someone wants to know how it work:
如果有人想知道它是如何工作的:
HWND toolTipWnd = ::CreateWindowExW(WS_EX_TOPMOST,
TOOLTIPS_CLASSW,0,WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
0,0,appHandle,0);
TOOLINFOW ti = {};
ti.cbSize = sizeof(TOOLINFOW);
ti.uFlags = TTF_ABSOLUTE | TTF_IDISHWND /* | TTF_TRACK */; // Don't specify TTF_TRACK here. Otherwise the tooltip won't show up.
ti.hwnd = toolTipWnd; // By doing this, you don't have to create another window.
ti.hinst = NULL;
ti.uId = (UINT)toolTipWnd;
ti.lpszText = L"";
::SendMessageW(toolTipWnd, TTM_ADDTOOLW, 0, (LPARAM)&ti);
::SendMessageW(toolTipWnd, TTM_SETMAXTIPWIDTH,0, (LPARAM)350);
This will create a tooltip window which is not bound to any other window. So when you want to show the tooltip (e.g. in responds to WM_MOUSEHOVER message), call this:
这将创建一个不绑定到任何其他窗口的工具提示窗口。所以当你想显示工具提示时(例如响应 WM_MOUSEHOVER 消息),调用这个:
TOOLINFOW ti = {};
ti.cbSize = sizeof(TOOLINFOW);
ti.hwnd = toolTipWnd;
ti.uId = (UINT)toolTipWnd;
ti.lpszText = L"Sample Tip Text";
::SendMessageW(toolTipWnd,TTM_UPDATETIPTEXTW,0,(LPARAM)&ti); // This will update the tooltip content.
::SendMessageW(toolTipWnd,TTM_TRACKACTIVATE,(WPARAM)TRUE,(LPARAM)&ti);
::SendMessageW(toolTipWnd, TTM_TRACKPOSITION,0,(LPARAM)MAKELONG(x,y)); // Update the position of your tooltip. Screen coordinate.
//::SendMessageW(toolTipWnd,TTM_POPUP,0,0); // TTM_POPUP not working.. Don't know why.
采纳答案by Anders
You need to call TTM_ADDTOOL at least once, you can't call TTM_SETTOOLINFO or get TTN_GETDISPINFO without it AFAIK.
您需要至少调用一次 TTM_ADDTOOL,如果没有 AFAIK,则不能调用 TTM_SETTOOLINFO 或获取 TTN_GETDISPINFO。
If your target it XP+ you can get away with using TTM_POPUP to display the tip at any position and at any time (But you need to handle the initial delay yourself unless you want a tracking tooltip)
如果您的目标是 XP+,您可以使用 TTM_POPUP 在任何位置和任何时间显示提示(但您需要自己处理初始延迟,除非您想要跟踪工具提示)
Generally you call TTM_ADDTOOL and associate it with a rectangle (TOOLINFO.rect) or a child window, or you can set the text to LPSTR_TEXTCALLBACK and handle TTN_GETDISPINFO if everything has a tip. MSDN has some sample codeyou should take a look at...
通常,您调用 TTM_ADDTOOL 并将其与矩形 (TOOLINFO.rect) 或子窗口相关联,或者您可以将文本设置为 LPSTR_TEXTCALLBACK 并处理 TTN_GETDISPINFO 如果一切都有提示。MSDN 有一些示例代码,你应该看看......
回答by CarpeDiemKopi
Addition Windows 10 (Visual Studio 2015, Win32 Console Application)
添加 Windows 10(Visual Studio 2015、Win32 控制台应用程序)
#include "Commctrl.h"
#pragma comment (lib,"comctl32.lib")
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
TOOLINFOW ti = {};
ti.cbSize = sizeof(TOOLINFOW);
ti.uFlags = TTF_ABSOLUTE | TTF_IDISHWND | TTF_TRACK ; // WITH TTF_TRACK! Otherwise the tooltip doesn't follow TTM_TRACKPOSITION message!
ti.hwnd = toolTipWnd;
ti.hinst = 0;
ti.uId = (UINT)toolTipWnd;
ti.lpszText = L"";
LRESULT result;
int error;
if (!SendMessageW(toolTipWnd, TTM_ADDTOOLW, 0, (LPARAM)&ti)) {
MessageBox(NULL, L"Couldn't create the ToolTip control.", L"Error", MB_OK);
error = 0;
error = GetLastError();
}
if (!SendMessageW(toolTipWnd, TTM_SETMAXTIPWIDTH, 0, (LPARAM)350)) {
MessageBox(NULL, L"Couldn't create the ToolTip control.", L"Error", MB_OK);
error = 0;
error = GetLastError();
}