多行工具提示和 CMFCButton

Multiline tooltips and CMFCButton

目前我正在使用标准 CButton 控件和我自己派生的 CMultilineToolTipCtrl 来显示多行工具提示。

我想我会试试 CMFCButton control as an alternative because it has native support for a tooltip by calling SetTooltip

但是,据我所知,它似乎不支持多行文本。有没有简单的解决方案,还是我应该坚持使用我编写的代码?

这是确定工具提示控件正确宽度的派生工具提示控件:

UINT CMultilineToolTipCtrl::FitText(CString &rStrText)
{
    CClientDC       *pDC ;
    CFont           *pFntTip, *pFntOld ;
    CRect           rctMargin, rctTip ;
    CSize           sizText ;
    CString         strTextCopy, strLine, strChaff ;
    CStringArray    aryStrLines ;
    UINT            uMaxWidth;
    INT_PTR         uNumLines, uLineIndex ;

    uMaxWidth = 0 ; // Fallback.

    GetMargin(&rctMargin);
    GetClientRect(&rctTip);

    // Reduce by the given margins to get the true area.
    rctTip.left += rctMargin.left ;
    rctTip.top += rctMargin.top ;
    rctTip.right -= rctMargin.right ;
    rctTip.bottom -= rctMargin.bottom ;

    pDC = new CClientDC(this);
    if (pDC != nullptr)
    {
        pFntTip = GetFont();
        pFntOld = (CFont *)pDC->SelectObject(pFntTip);

        // First, find the longest line.
        strTextCopy = rStrText ;
        uMaxWidth = 0 ;
        while (strTextCopy != _T("") )
        {
            strLine = strTextCopy.SpanExcluding(_T("\r\n") );
            strTextCopy.Delete(0, strLine.GetLength() );
            strChaff = strTextCopy.SpanIncluding(_T("\r\n") );
            strTextCopy.Delete(0, strChaff.GetLength() );

            sizText = pDC->GetTextExtent(strLine);
            if (sizText.cx > (int)uMaxWidth)
                uMaxWidth = sizText.cx ;
            aryStrLines.Add(strLine);
        }

        rStrText = _T("");
        uNumLines = aryStrLines.GetSize();

        // Now, pad all lines to that max width.
        for (uLineIndex = 0 ; uLineIndex < uNumLines ; uLineIndex++)
        {
            strLine = aryStrLines.GetAt(uLineIndex);
            sizText = pDC->GetTextExtent(strLine);
            while (sizText.cx <= (int)uMaxWidth)
            {
                strLine += _T(" ");
                sizText = pDC->GetTextExtent(strLine);
            }

            rStrText += strLine ;
        }

        SendMessage(TTM_SETMAXTIPWIDTH, 0, uMaxWidth);

        pDC->SelectObject(pFntOld);

        delete pDC ;
    }

    return uMaxWidth ;
}

如果您有更简单的方法来计算宽度,欢迎您的回答。