Tooltip 被限制在 80 个字符甚至 setmaxtipwidth 为 32767

Tooltip is limited by 80 characters even setmaxtipwidth to 32767

我有一个使用 NMTTDISPINFO 结构的 MFC C++ 项目。当工具提示文本 少于 80 个字符时一切正常,但有时我需要使用 多于 80 个字符的文本。

正如我在 MSDN 上读到的那样,我必须使用 lpszText 而不是 sztext

NMTTDISPINFOA structure

我的代码是:

TTooltipText& tiTxt = *(TTooltipText*)nmhdr;
::SendMessage(NMHDR(tiTxt).hwndFrom, TTM_SETMAXTIPWIDTH, 0, SHRT_MAX);
CString pricesStr = GetPrices();
tiTxt.lpszText = pricesStr.GetBuffer(pricesStr.GetLength());

但不幸的是,这段代码不起作用,有什么帮助吗?

我无法用 win32 应用程序重现您的问题。

使用 SendMessage(hwndTT, TTM_SETMAXTIPWIDTH, 0, 150); 我得到这样的多行工具提示:

没有 SendMessage(hwndTT, TTM_SETMAXTIPWIDTH, 0, 150); 我得到这样的单行工具提示:

我使用的代码(c++)如下基于official document

void CreateToolTipForRect(HWND hwndParent)
{
    // Create a tooltip.
    HWND hwndTT = 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);

    SetWindowPos(hwndTT, HWND_TOPMOST, 0, 0, 0, 0,
        SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

    // Set up "tool" information. In this case, the "tool" is the entire parent window.

    TOOLINFO ti = { 0 };
    ti.cbSize = sizeof(TOOLINFO);
    ti.uFlags = TTF_SUBCLASS;
    ti.hwnd = hwndParent;
    ti.hinst = hInst;
    ti.lpszText = (LPSTR)"this string length is more than 80 !!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-80-!!!!!!!!!!!!!!!";

    GetClientRect(hwndParent, &ti.rect);

    SendMessage(hwndTT, TTM_SETMAXTIPWIDTH, 0, 150);

    // Associate the tooltip with the "tool" window.
    SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&ti);
}

首先创建一个工具提示如下:

HWND CreateToolTip(HWND hwndParent)
{
   // Create a tooltip.
    HWND hwndTT = 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);

    SetWindowPos(hwndTT, HWND_TOPMOST, 0, 0, 0, 0,
     SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

    //Set up "tool" information. In this case, the "tool" is the entire parent window

  TOOLINFO ti = { 0 };
  ti.cbSize = sizeof(TOOLINFO);
  ti.uFlags = TTF_SUBCLASS;
  ti.hwnd = hwndParent;
  ti.hinst = hInst;
  ti.lpszText = LPSTR_TEXTCALLBACK; // this to use tooltip notify message

  GetClientRect(hwndParent, &ti.rect);

  SendMessage(hwndTT, TTM_SETMAXTIPWIDTH, 0, 500);

   // Associate the tooltip with the "tool" window.
  SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&ti);
}

注意:使用 LPSTR_TEXTCALLBACK 意味着您必须定义 TTN_NEEDTEXT 消息处理程序

ON_NOTIFY(TTN_NEEDTEXT, 0, HandleTooltipText)

最后我们定义一个 TTN_NEEDTEXT 消息处理程序为:

void HandleTooltipText(NMHDR* nmhdr, LRESULT*)
{
    NMTTDISPINFO* toolTip = (NMTTDISPINFO*)nmhdr;
    CString prices = GetPrices();
    if (toolTip->hdr.code == TTN_GETDISPINFO)
    {
       toolTip->lpszText = new TCHAR[prices.GetLength() + 1];
       memset(toolTip->lpszText, 0, (prices.GetLength() + 1) * sizeof(TCHAR));
       _tcscpy_s(toolTip->lpszText, prices.GetLength() + 1, prices.GetString());
       toolTip->hinst = 0;
    }
}

设置 MFC 共享工具提示控件的最大宽度。

static CToolTipCtrl* pToolTip = NULL;
CToolTipCtrl* pTT = AfxGetModuleThreadState()->m_pToolTip;

// MFC shared tooltip sometimes loses its maxtipwidth eventhough
// its object has not changed
if (/*pTT != pToolTip 
        && */pTT != NULL) {
    pToolTip = pTT;
    pToolTip->SetMaxTipWidth(SHRT_MAX);
    pToolTip->SetDelayTime(TTDT_AUTOPOP, SHRT_MAX);
}