2011-05-05 65 views
10

我想使用本機窗口工具提示控件(純Win32 API,不需要MFC東西)。如何在不綁定工具的情況下使用Windows ToolTip控件

我閱讀了文檔,似乎我必須發送TTM_ADDTOOL消息來將工具綁定到工具提示控件。只有在此之後,才能發送TTM_TRACKACTIVATE & TTM_TRACKPOSITION以顯示工具提示。

但我想在任何我想要的位置顯示工具提示。例如,當鼠標懸停在我窗口的某個區域上時。這個區域不是Windows眼中的工具,它只是我窗口中的一個區域。

也許我可以將窗口綁定到工具提示控件,但是,這並不意味着我必須將創建的每個窗口綁定到工具提示控件?

是否有一個簡單的解決方案,以便我不必爲每個窗口發送TTM_ADDTOOL消息?


我實際上已經寫了一些代碼,但工具提示只是沒有出現。安德斯的答案實際上解決了一些問題。當我捅了我的代碼之後,我開始工作了。

如果有人想知道它是如何工作的:

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消息),調用此:

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. 

回答

5

你需要至少調用一次TTM_ADDTOOL,你不能調用TTM_SETTOOLINFO或沒有得到它TTN_GETDISPINFO AFAIK。

如果你的目標是XP +你可以逃脫使用TTM_POPUP顯示在任意位置,並在任何時間提示(但你需要自己處理的初始延遲,除非你想跟蹤提示)一般

你調用TTM_ADDTOOL並將其與矩形(TOOLINFO.rect)或子窗口相關聯,或者可以將文本設置爲LPSTR_TEXTCALLBACK,並在所有內容都有提示時處理TTN_GETDISPINFO。 MSDN有一些sample code你應該看看......

+0

確定。我終於解決了這個問題。使用本地Win32 API是令人頭疼的事情。我可以通過TTM_POPUP使它工作,但我使用TTM_TRACKPOSITION解決了它。 – MorrisLiang 2011-05-05 12:39:42

+0

示例代碼包含了所有需要的東西......謝謝 – 2014-04-13 09:16:45

0

加入的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(); 
} 
相關問題