簡體   English   中英

Win32 C++ 中的跟蹤工具提示

[英]Tracking tooltip in Win32 C++

在 Win32/C++ 應用程序中,在 64 位 Windows 7 下運行,使用 Visual Studio 16.7.7,我想在主(也是唯一)窗口中實現跟蹤工具提示。 按照Microsoft SDK 文檔中的示例,跟蹤似乎有效,但工具提示窗口本身並未出現。

我已經使用調試器驗證了工具提示已激活和停用,正在發生預期的鼠標跟蹤, TTM_TRACKPOSITION消息中的屏幕坐標是否正確,以及文本是否正常。 該應用程序是 Unicode,我已經檢查過結構是 Unicode 版本,並且初始化了常用控件,並且鏈接了當前版本的常用控件庫。 根據 Spy++,工具提示窗口具有WS_EX_TOPMOSTWS_EX_TOOLWINDOW擴展樣式。

需要進行哪些更改才能顯示工具提示?

這是我正在使用的代碼:

全局變量:

HINSTANCE hInst;
HWND hWnd;
HWND hwndTT;
WCHAR ttText[12];
TOOLINFO toolTipInfo;
BOOL trackingMouse;

初始化:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   // Set up for mouse tracking (tooltips)
   INITCOMMONCONTROLSEX icc;
   icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
   icc.dwICC = ICC_BAR_CLASSES;
   BOOL ok=InitCommonControlsEx(&icc);
   trackingMouse = FALSE;//
   WCHAR nullString[15] = { L"After create" };
   hwndTT = CreateTrackingToolTip(0 /*toolID*/, hWnd, nullString);
   ...

HWND CreateTrackingToolTip(int toolID, HWND hWndParent, WCHAR* pText)
{
    // Create a tooltip.
    HWND h = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
        WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        hWndParent, NULL, hInst, NULL);

    if(!h)
    {
        return NULL;
    }

    // Set up the tool information. In this case, the "tool" is the entire parent window.
    memset(&toolTipInfo, 0, sizeof(TOOLINFO));
    toolTipInfo.cbSize = sizeof(TOOLINFO);
    toolTipInfo.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
    toolTipInfo.hwnd = hWndParent;
    toolTipInfo.hinst = hInst;
    toolTipInfo.lpszText = pText;
    toolTipInfo.uId = (UINT_PTR)hWndParent;

    GetClientRect(hWndParent, &toolTipInfo.rect);

    // Associate the tooltip with the tool window.
    SendMessage(h, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&toolTipInfo);

    return h;
}

窗口程序:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_MOUSELEAVE: // The mouse pointer has left our window. Deactivate the tooltip.

        SendMessage(hwndTT, TTM_TRACKACTIVATE, (WPARAM)FALSE, (LPARAM)&toolTipInfo);
        trackingMouse = FALSE;
        TRACE(L"\nDeactivate tooltip");
        return FALSE;

    case WM_MOUSEMOVE:
            static int oldX, oldY;
        int newX, newY;

        if(!trackingMouse)   // The mouse has just entered the window.
        {                    // Request notification when the mouse leaves.

            TRACKMOUSEEVENT tme = { sizeof(TRACKMOUSEEVENT) };
            tme.hwndTrack = hWnd;
            tme.dwFlags = TME_LEAVE;

            BOOL ok=TrackMouseEvent(&tme);

            // Activate the tooltip.
            SendMessage(hwndTT, TTM_TRACKACTIVATE, (WPARAM)TRUE, (LPARAM)&toolTipInfo);
            trackingMouse = TRUE;
            TRACE(L"\nActivate tooltip");
        }

        newX = GET_X_LPARAM(lParam); newY = GET_Y_LPARAM(lParam);

        // Make sure the mouse has actually moved. The presence of the tooltip 
        // causes Windows to send the message continuously.
        if((newX != oldX) || (newY != oldY))
        {
            oldX = newX; oldY = newY;

            // Update the text.
            swprintf_s(ttText, ARRAYSIZE(ttText), L"%d, %d", newX, newY);
            toolTipInfo.lpszText = ttText;
            SendMessage(hwndTT, TTM_SETTOOLINFO, 0, (LPARAM)&toolTipInfo);

            // Position the tooltip. The coordinates are adjusted so that the tooltip does not overlap the mouse pointer.
            POINT pt = { newX, newY };
            ClientToScreen(hWnd, &pt);
            SendMessage(hwndTT, TTM_TRACKPOSITION, 0, (LPARAM)MAKELONG(pt.x + 10, pt.y - 20));
        }
    return FALSE;
...

需要以下行:

#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

有關更多詳細信息,請參閱“ 使用清單或指令確保視覺樣式可以應用於應用程序”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM