Multi-line ToolTips

How does one implement a multi-line tooltip?  I've tried putting various characters (like \r and \n) in the string resource to be display but I always get:

This is line 1|This is line 2

If the line is too long, it gets truncated.
LVL 32
jhanceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

piano_boxerCommented:
Microsoft's ToolTip control does NOT currently support multi-line text. You will have to write i your self.
0
jhanceAuthor Commented:
If that is the case, then what method does MS use in IE4.0 for the tooltips which appear on the toolbar icons?  They seem to have the capability of having a multi-line display.  
0
gelbertCommented:
Just use Spy to watch for tooltips and toolbar messages used inside IE 4.0 and make sure that you are using the same common control library as IE 4.0. It gives you list of messages and the rest(what do they mean and how to use them) you will have to figure out yourself.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

RONSLOWCommented:
MS does support multi-line tooltips.

You just need to set the width of the tooltip control and it will automatically extend to multiple lines (with word-wrap) to fit the text in.

I can show you code to do this.


0
jhanceAuthor Commented:
RONSLOW,

Some more information on how to do this would help.  I'm not creating a tooltip control explicitly in MFC.  How do you get a handle to it to control it's width?
0
RONSLOWCommented:
I'll dig it out.  I do this myself because I have long descriptive strings and they look very silly when all one one long skinny tooltip.

Hang on and I'll find my code...

0
RONSLOWCommented:
you send the control a TTM_SETMAXTIPWIDTH

more digging...

0
RONSLOWCommented:
BTW: Have a look at "Tiptoe Through the ToolTips With Our All-Encompassing ToolTip Programmer's Guide" in MSJ Apr 97 (also on MSDN).  It uses \n for multi-line tooltips.

0
jhanceAuthor Commented:
Like I said earlier, I tried using "\n" in the ToolTip text but just get a bold "|" in the tip.  I'm not sure how to send a TTM_SETMAXTIPWIDTH to the control since I never created a control.  I'm just calling EnableToolTips() and handling the WM_NEEDTEXT message.
0
RONSLOWCommented:
after enabling your tooltip with EnableToolTips you should be try doing this after EnableToolTips (assuming that is what you are doing)

  CToolTipCtrl* pToolTip = AfxGetThreadState()->m_pToolTip;
  if (pToolTip->GetSafeHwnd() != NULL) {
    pToolTip->SendMessage(TTM_SETMAXTIPWIDTH, 0, 160);
  }

change 160 to whatever size you want (in pixels).

0
jhanceAuthor Commented:
Well, this method does make the tool tip wider but I still am not able to get more than a single line.  Any other ideas?
0
RONSLOWCommented:
If you text is longer that required it will wrap.

Set the width to something smaller (eg. 80).

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RONSLOWCommented:
I havesample code now that uses EnableToolTips and the SetTipMaxWidth call .. and it works giving multi-line tooltips.

I started with the MSJ March 97 C++ article sample code (Virgil) and modified the OnGetTooltipText by adding the lines for getting the tooltip and setting its width.  NOTE: I couldn't do this at the point of the EnableToolTip's because the enabled tool tip control isn't created until it is first needed.  SO I set the width here instead

void CMainFrame::OnGetTooltipText(NMHDR* pNMHDR, LRESULT* plRes)
{
  CToolTipCtrl* pToolTip = AfxGetThreadState()->m_pToolTip;
  if (pToolTip->GetSafeHwnd() != NULL) {
    pToolTip->SendMessage(TTM_SETMAXTIPWIDTH, 0, 10);
  }
  TOOLTIPTEXT& ttt = *((TOOLTIPTEXT*)pNMHDR);
  strncpy(ttt.szText,
    GetWhoItIs(CPoint (pNMHDR->idFrom)),
    sizeof(ttt.szText));
}

0
jhanceAuthor Commented:
OK, I'll give that a try.  Thanks for hanging with me here.
0
RONSLOWCommented:
S'alright .. that's what I'm here for :-)

0
jhanceAuthor Commented:
That did it, thanks!
0
RONSLOWCommented:
Still not sure why \n is not allowed in Enable tool tips method .. but seems to work ok in manually created tooltip (I've run the sample code that uses \n in the tooltip and it seems to work fine). But at least you can make your tips look a little nicer now.

0
jhanceAuthor Commented:
I think I graded you too soon.  Or perhaps I wan't complete clear on what I was asking.  Anyway, I'm getting multi-line tooltips but I still only get the first 80 (79 actually) characters displayed regardless of how wide I make the tooltip.  Any other ideas?

Thanks...
0
jhanceAuthor Commented:
Hmmm, poking around in MFC source code produces the following revelation:

typedef struct tagNMTTDISPIFNOA {
    NMHDR hdr;
    LPSTR lpszText;
    char szText[80];
    HINSTANCE hinst;
    UINT uFlags;
    LPARAM lParam;
} NMTTDISPINFOA, FAR *LPNMTTDISPINFOA;

Note that szText is fixed at 80 characters!  Is it possible to provide my own structure instead of this stupid one?
0
RONSLOWCommented:
There is a limit on the length .. which you have found.  You never said you wanted really long tips, though.

I believe that you can put a pointer to a string into lpszText instead .. I don't think there is a limit if you do it that way .. and I think you CAN use '\n' in that case.


0
RONSLOWCommented:
Of course, that string need to not go out of scope after the function returns.  Make it a static (or a member var or whatever) .. as long as it doesn't disappear when the TTN_NEEDTEXT handler returns.

0
jhanceAuthor Commented:
It's interesting.  I _was_ using the lpszText pointer, BUT, I was loading the string from a string table resource instead of just a char *.  If I just point lpszText at a long char * string, I get multi-line AND the \n works.

Go figure.  Thanks again.
0
RONSLOWCommented:
Looks like you've got a few alternatives now and a better understand of the mysteries of tool tips (although some mysteries reamin just that .. like when \n works).

Glad to help.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.