Are multiline tooltips possible using CWnd::Enable

2019-07-21 06:12发布

问题:

I'm attempting to make my tooltips multiline, but I don't seem to be having much luck with it. I call CWnd::EnableTooltips() directly after creation (in this case, an edit box) and I handle the TTN_NEEDTEXT message. My tooltips display correctly, but only display as a single line.

I've tried adding '\n' to the string I pass when handling TTN_NEEDTEXT, and also tried '\r\n'. No luck. It just displays them as normal text in the tooltip string.

I then tried manually inserting 0x0D0A, but this just displays as boxes.

I've been digging a bit, and have found a few offhand references on the web saying that multiline behavior may not work when using tooltips through the CWnd functions. I'd prefer not to have to replace with CToolTipCtrl (since it's a rather large project). Has anyone ran into this before? If so, is there any way around it?

回答1:

I was successful in making a \n delimited tooltip into a multi-line tooltip using the following code in the TTN_NEEDTEXT handler

For DevStudio 6

CToolTipCtrl* pToolTip = AfxGetThreadState()->m_pToolTip;
pToolTip->SetMaxTipWidth(SHRT_MAX);

You have to call again each time TTN_NEEDTEXT is called or it won't stick.

I found this trick reading the code from http://www.codeproject.com/KB/list/CListCtrl_ToolTip.aspx

NOTE: the code there actually does the following but that won't compile in VS6 as the ModuleThreadState doesn't have the m_pToolTip member in VS6 (I haven't tried the following in VS2005+ but i presume it would work there)

BOOL CListCtrl_EnableToolTip::OnToolNeedText(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
{
...
   // Break tooltip into multiple lines if it contains newlines (/n/r)
   CToolTipCtrl* pToolTip = AfxGetModuleThreadState()->m_pToolTip;
   if (pToolTip)
      pToolTip->SetMaxTipWidth(SHRT_MAX);
...
}


回答2:

I vaguely recall that I got that to work. I googled a bit, I think what I did was set SetMaxTipWidth() to 'force' the tooltip to be more narrow than the text that I was putting in. I later switched to my own tooltip control for other reasons, but I am using the same 'design' there, it's quite likely that I copied that behaviour from my old code. Give it a shot I'd say if you already have a CToolTipCtrl anyway :)



回答3:

This is something I have struggled with on and off in my MFC application. I have sub-classes of all common view/dialog classes that deal with my tooltips for me. I found that in some (such as headers in CListCtrls or CPropertySheets) don't need you to call SetMaxTipWidth every time (as mentioned above), but others (CView, CDialog, CPropertyPage, CListCtrl, CTreeCtrl) do require you to call it every time the tooltip is going to pop up. Its a bit silly, but it seems to work.



标签: c++ mfc tooltip