How to use Windows ToolTip Control without bounding to a tool

后端 未结 2 1980
感动是毒
感动是毒 2021-01-04 10:32

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

I read the doc, it seems that I have to send a TTM_ADDTOOL message to bond a tool to

相关标签:
2条回答
  • 2021-01-04 10:58

    Addition Windows 10 (Visual Studio 2015, Win32 Console Application)

    #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();
    }
    
    0 讨论(0)
  • 2021-01-04 11:11

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

    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)

    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...

    0 讨论(0)
提交回复
热议问题