tags:

views:

365

answers:

3

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?

A: 

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

Roel
The problem with that is that I don't have a specific width I want to wrap at, I was just wanting line breaks. If there's no other way to do it, then I guess that's what I have to do, but it's a little bit of a hassle. (And a little odd. Why support escape characters everywhere except tooltips?)
ctoneal
Tooltips are a strange beast. I try to avoid the build-in ones because they are unintuitive to use and not very flexible, as you've found out :)
Roel
+1  A: 

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);
...
}
Ruddy
While doing this doesn't make escape characters work for me, it does seem to make 0x0D0A work, and that's good enough for me. Seems pretty ridiculous that SetMaxTipWidth magically makes certain things work. I has assumed that it did what it was supposed to do (set the max tip width!), but I guess that it has hidden benefits as well. Thanks to both of you.
ctoneal
Yea Undocumented Magic Numbers are on of my favorite things.
Ruddy
A: 

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.

Dillon