2011-05-05 25 views
10

Quiero usar el control nativo de información sobre herramientas (API Win32 pura, sin elementos MFC).Cómo usar Windows ToolTip Control sin limitar a una herramienta

He leído el documento, parece que tengo que enviar un mensaje TTM_ADDTOOL para vincular una herramienta con el control de información sobre herramientas. Solo después de eso puedo enviar TTM_TRACKACTIVATE & TTM_TRACKPOSITION para mostrar la información sobre herramientas.

Pero quiero mostrar la información sobre herramientas en cualquier lugar que desee. Por ejemplo, cuando el mouse se desplaza sobre una región de mi ventana. Esta región no es una herramienta en el ojo de Windows, es solo una región en mi ventana.

Quizás pueda unir la ventana al control de información sobre herramientas, pero, ¿no significa que tengo que vincular cada ventana que creé con el control de información sobre herramientas?

¿Existe una solución fácil para que no tenga que enviar mensajes TTM_ADDTOOL para cada ventana?


De hecho, me he escrito algo de código, pero la información sobre herramientas simplemente no aparece. La respuesta de Anders resuelve algunas preguntas en realidad. Y después de hurgar en mi código, lo hago funcionar.

En caso de que alguien quiere saber cómo funciona:

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

Esto creará una ventana de información sobre herramientas que no está unido a ninguna otra ventana. Así que cuando se quiere mostrar la información sobre herramientas (por ejemplo, en el mensaje responde a WM_MOUSEHOVER), llamar a esto:

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. 

Respuesta

5

es necesario llamar a TTM_ADDTOOL al menos una vez, no se puede llamar TTM_SETTOOLINFO o conseguir TTN_GETDISPINFO sin que yo sepa.

Si su objetivo es XP + usted puede conseguir lejos con el uso TTM_POPUP para mostrar la punta en cualquier posición y en cualquier momento (pero hay que manejar el retraso inicial a sí mismo a menos que desee una información sobre herramientas de seguimiento)

En general, usted llame a TTM_ADDTOOL y asócielo con un rectángulo (TOOLINFO.rect) o una ventana secundaria, o puede establecer el texto en LPSTR_TEXTCALLBACK y manejar TTN_GETDISPINFO si todo tiene una sugerencia. MSDN tiene alguna sample code usted debe echar un vistazo a ...

+0

OK. Finalmente resuelvo ese problema. Trabajar con la API Win32 nativa es un dolor de cabeza. Puedo hacer que funcione con TTM_POPUP pero lo resuelvo con TTM_TRACKPOSITION. – MorrisLiang

+0

código de muestra tiene todo exactamente necesario ... gracias –

0

adición de Windows 10 (Visual Studio 2015, Aplicación de consola 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(); 
} 
Cuestiones relacionadas