如何在不绑定工具的情况下使用Windows ToolTip Control [英] How to use Windows ToolTip Control without bounding to a tool

查看:333
本文介绍了如何在不绑定工具的情况下使用Windows ToolTip Control的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用本机Windows工具提示控件(纯Win32 API,没有MFC的东西)。

I want to use the native windows tooltip control (pure Win32 API, no MFC stuff).

我读了doc,看来我必须发送TTM_ADDTOOL消息将工具绑定到工具提示控件。只有在那之后,我可以发送TTM_TRACKACTIVATE& TTM_TRACKPOSITION以显示工具提示。

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.

但我想在任何我想要的地方显示工具提示。例如,当鼠标悬停在我窗口的一个区域上时。这个区域在Windows眼中不是一个工具,它只是我窗口中的一个区域。

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.

也许我可以将窗口绑定到工具提示控件,但是,这意味着我必须将每个窗口绑定到工具提示控件?

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?

有一个简单的解决方案,所以我不必发送TTM_ADDTOOL消息为每个窗口?

Is there an easy solution so that I don't have to send TTM_ADDTOOL messages for every window?

我实际上已经写了一些代码,但工具提示没有出现。安德斯的答案实际上解决了一些问题。

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);

这将创建一个不绑定到任何其他窗口的工具提示窗口。
所以当你想显示工具提示(例如,响应WM_MOUSEHOVER消息),调用这个:

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:

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.


推荐答案

您需要至少调用TTM_ADDTOOL一次, 't调用TTM_SETTOOLINFO或获取TTN_GETDISPINFO没有它AFAIK。

You need to call TTM_ADDTOOL at least once, you can't call TTM_SETTOOLINFO or get TTN_GETDISPINFO without it AFAIK.

如果你的目标它XP +你可以使用TTM_POPUP显示提示在任何位置和任何时间但是你需要自己处理初始延迟,除非你想要一个跟踪工具提示)

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)

通常你调用TTM_ADDTOOL并将它与矩形(TOOLINFO.rect)或子窗口,或者您可以将文本设置为LPSTR_TEXTCALLBACK,并处理TTN_GETDISPINFO,如果一切都有提示。 MSDN有一些示例代码,您应该看看。 ..

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 code you should take a look at...

这篇关于如何在不绑定工具的情况下使用Windows ToolTip Control的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆