开发者

Are multiline tooltips possible using CWnd::EnableTooltips()?

I'm attempting to make my tooltips multi开发者_如何学运维line, 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?


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);
...
}


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 :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜