Click here to Skip to main content
Email Password   helpLost your password?
  • Download demo project - 23 Kb
  • Download source files - 5 Kb
  • Sample Image - Multiline_Titletips.gif

    A titletip is similar to a tooltip, but is used to display data in a control that is otherwise not large enough to display the full text. We are all used to seeing cell titletips that display contents that are hidden by the width of the cell. These titletips typically use a single line. If the contents are greater than the single line, they were truncated.

    The code presents a titletip class that can show the complete contents of a cell regardless of its size. The titletip window will adjust itself up or down depending on the cell being displayed, and will take into account the size of the parent's client area, and that of the screen. It will do its utmost to show you that data!

    This article is based on code written by Zafir Anjum, but has been extensively reworked to add a great deal of new functionality.

    How to use CTitleTip

    Using the class is extremely easy. Just declare an object of type CTitleTip in your main windows' header, and then create the title tip window in your dialog's OnInitDialog, or your view's OnInitialUpdate:

    // dialog header file
    
    class CTitleTipDemoDlg : public CDialog
    {
    	...
    	
    	CTitleTip m_TitleTip;
    }
    
    BOOL CTitleTipDemoDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    	...
    	m_TitleTip.Create(this);
    	
    	return TRUE;  // return TRUE  unless you set the focus to a control
    
    }
    

    You then need to add a mouse over handler so that when the mouse cursor moves over controls that may contain large amounts of text, you can popup a title tip to display that text. For instance, in our demo dialog we have a static control (m_staticText) that displays a lot of text. Just add an OnMouseMove handler and add the following code:

    void CTitleTipDemoDlg::OnMouseMove(UINT nFlags, CPoint point) 
    {
    	// Get the dimensions of the static control
    
    	CRect rect;
    	m_staticText.GetWindowRect(rect);
    	ScreenToClient(rect);
    
    	// If the mouse is over the control, display the titletip. 
    
    	// Note: The title tip will only be displayed if the text
    
    	// in the static control is too large for the static control
    
    	if (rect.PtInRect(point))
    	{
    		CString str;
    		m_staticText.GetWindowText(str);
    		m_TitleTip.Show(rect, str, 0, 80);
    	}
    	
    	CDialog::OnMouseMove(nFlags, point);
    }

    The CTitleTip::Show function has the following syntax

    void Show(CRect rectTitle,            // Bounds of the control containing the text
    
              LPCTSTR lpszTitleText,      // The text to display
    
              int xoffset = 0,            // offset to display text within title tip
    
              int nMaxChars = -1,         // Maximum number of characters per line to display
    
              LPRECT lpHoverRect = NULL,  // Once the mouse moves outside of this
    
                                          //   rect, the title tip will disappear
    
              LPLOGFONT lpLogFont = NULL) // The font in which to display the text
    
    

    If nMaxChars is less than 0, then the text will only wrap where there is insufficient room to display the entire line.

    if lpHoverRect is NULL then it will be set as rectTitle.

    You must Sign In to use this message board.
     
     
    Per page   
     FirstPrevNext
    GeneralGood Job
    clxye
    16:50 13 Dec '07  
    Thank you! Smile
    GeneralCTitleTip Class not found
    123Asha
    21:40 4 Sep '07  
    When i use the above mentioned CTitleTip class, it is said tht

    CTitleTip' : missing storage-class or type specifiers
    GeneralTo display Custom tooltiptext in taskbar
    123Asha
    21:37 4 Sep '07  
    i need to display a tooltip text in a taskbar for an application other than appication's window text...

    can anyone help me for that
    GeneralCan this be used with app's taskbar button tooltip ???
    ana_v123
    14:01 11 Apr '06  
    How to use this control be used to replace app's taskbar button tooltip ???

    For MDI Application (MFC/VC++6.0), when we place mouse cursor over taskbar button of that application, ToolTip is displayed (showing application Title). How to modify toolTip text and font (For one MDI Application and not system-wide) ?

    Thanks for any idea or direction.
    Ana

    Ana_v123

    Ana_v123
    GeneralStrage behaviour
    iasen_st
    4:01 23 Aug '04  
    try this:
    move the object wih the titletip to the left or the right side ot the screen.
    and then do what you do to show the titletip.If it's the right side, then you'll notice that the titletip is shown somewhere out of the screenSmile
    If you choose the left side, the size is goin' crazy.
    Any ideas how to correct this?

    Great work though.
    Generalproblem! pls help!
    ns
    8:53 12 Nov '03  
    If I dblclick in the editbox thats currently showing the tooltip in the are not occupied by the tooltip, it freezes even when we go away from that area. How shall I get this to fix itself? Thanks,
    ns
    GeneralTrying to use an edit box
    ns
    5:22 7 Nov '03  
    If I add another static t ext to your sample code, it works great. But if I put an edit control, it doesnt show the tooltip (the edit ctrl I mean)
    Help!
    Thanks,
    ns
    Generalnice work...and bug?
    HighJack
    22:52 11 Feb '03  
    TitleTip.cpp line:148 [CTitleTip::Show(...)]
       m_rectDisplay.right = m_rectDisplay.left + size.cx /*+ xoffset*/;
    Probably, "xoffset" is unnecessary.

    TitleTip.cpp line:283 [CTitleTip::Show(...)]
       int nHeight = dc.DrawText(m_strTitle,&rectCalc,0 ....
    Before this, SelectObject(&Font) is required.

         if (lpLogFont)
              pOldFont = dc.SelectObject( &font );
         else
              pOldFont = dc.SelectObject( m_pParentWnd->GetFont() );

         int nHeight = dc.DrawText(m_strTitle,&rectCalc,0
                | DT_CALCRECT
                | DT_LEFT
                | DT_NOCLIP
                | DT_EDITCONTROL
                | DT_NOPREFIX
                | DT_WORDBREAK
                );
        
         pOldFont = dc.SelectObject( pOldFont );

    Thanks ChrisSmile
    GeneralExcellent
    JohnJ
    1:41 3 Jan '03  
    Very good - I was fiddling around trying to get a multiline tooltip to work on Tree control items - Problem solved!!

    Thanks ChrisBig Grin

    JohnJ
    Sleepy Sleepy Sleep(28800000); Sleepy Sleepy
    http://www.rainbow-innov.co.uk[^]
    GeneralHow can I use this titletip in CGridctrl.
    Anonymous
    19:32 3 Nov '02  
    How can I use this titletip in CGridctrl.
    Please Help..
    GeneralRe: How can I use this titletip in CGridctrl.
    Chopper
    1:21 18 Dec '02  
    hi,
    a bit off-topic, but since you got no answer here i could an answer by suggesting to visit www.tooltips.net where you can download tooltips attachable to anything.

    Regards,
    Vitaly Tomilov

    Generalnice work
    Kevin Smith
    10:44 26 Aug '02  
    excellent! and cool, I got this working
    GeneralSDI app
    Kevin Smith
    9:52 23 Aug '02  
    How can I use this so a tooltip appears when my mouse is over a column heading in my CListView? Sample code would be appreciated.

    Thanks
    GeneralCan't detect double click
    hierro
    4:17 30 Nov '01  
    I used your multiline titletip, works great. But after using it, the CRect object can't detect any double click messages. Instead it'll invoke the LButtonDown function which is in place also. Any idea how to circumvent that? Confused
    GeneralRe: Can't detect double click
    Roger Allen
    7:25 16 Apr '02  
    You can handle the double click like this in the OnLButtonDown()

     	static bool double_wait = false ; // must be static due to 	re-entrant function
    static bool handled = false ; // must be static due to re-entrant function
    MSG msg ;
    if (double_wait)
    {
    // this is a double click
    // do double click code here
    TRACE("Double click\n") ;
    handled = true ;
    return ;
    }
    else handled = false ;
    double_wait = true ;
    UINT time = GetDoubleClickTime() ;
    UINT slice = time / 10 ;

    for (int i = 0 ; i < 10 && !handled ; i++)
    {
    // break up the sleep into chuncks to give better response
    if (i == 9)
    Sleep(slice + time % 10) ;
    else Sleep(slice) ; // dispatch any messages that have come in, including any clicks on this button again.
    while (::PeekMessage(&msg, NULL, 0, 0, PM_NOYIELD | PM_REMOVE))
    {
    ::TranslateMessage(&msg) ;
    ::DispatchMessage(&msg) ;
    }
    }
    // time ran out,
    if (!handled)
    {
    // this is a single click
    // do single click code here
    TRACE("Single click\n") ;
    }
    double_wait = false ;


    Roger Allen
    Sonork 100.10016

    yet to be identified being from the planet Paltinmoriumbanfrettybooter
    GeneralAn easier way...
    R
    6:48 5 Aug '02  
    There is an easier way...

    1. Add CS_DBLCLKS to the WNDCLASS style so that the TitleTip receives double-click messages:

    In CTitleTip::CTitletip()
       wndcls.style = CS_SAVEBITS; changes to:    wndcls.style = CS_SAVEBITS | CS_DBLCLKS;

    2. Add WM_LBUTTONDBLCLK to the messages recognised by the PreTranslateMessage:

    In CTitleTip::PreTranslateMessage(MSG* pMsg)
    switch (pMsg->message)
    {
    case WM_LBUTTONDOWN:
    case WM_RBUTTONDOWN:
    case WM_MBUTTONDOWN:
    changes to: switch (pMsg->message)
    {
    case WM_LBUTTONDOWN:
    case WM_RBUTTONDOWN:
    case WM_MBUTTONDOWN:
    case WM_LBUTTONDBLCLK:

    and
    switch (pMsg->message)
    {
    case WM_LBUTTONDOWN:
    pMsg->message = WM_NCLBUTTONDOWN;
    break;
    case WM_RBUTTONDOWN:
    pMsg->message = WM_NCRBUTTONDOWN;
    break;
    case WM_MBUTTONDOWN:
    pMsg->message = WM_NCMBUTTONDOWN;
    break;
    }
    changes to: switch (pMsg->message)
    {
    case WM_LBUTTONDOWN:
    pMsg->message = WM_NCLBUTTONDOWN;
    break;
    case WM_RBUTTONDOWN:
    pMsg->message = WM_NCRBUTTONDOWN;
    break;
    case WM_MBUTTONDOWN:
    pMsg->message = WM_NCMBUTTONDOWN;
    break;
    case WM_LBUTTONDBLCLK:
    pMsg->message = WM_NCLBUTTONDBLCLK;
    }


    3. Handle the WM_LBUTTONDBLCLK message as normal in the titletip's parent.

    Doing it this way keeps it nice and neat, and handles the double-click in the same way as the single click. I believe this method will also work for other messages (e.g. Keydown's).


    Last Updated 6 Jan 2000 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010