Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

Multiline Titletips

,
Rate me:
Please Sign up or sign in to vote.
4.53/5 (7 votes)
5 Jan 2000CPOL 113.6K   3.2K   55   17
A class that allows you to display data for a control that is otherwise not large enough to display the full text
  • 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.

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Founder CodeProject
    Canada Canada
    Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

    In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

    In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionCGridCtrl version 2.27 has not AddPoint Method Pin
    Aydin Homay26-Mar-13 3:41
    Aydin Homay26-Mar-13 3:41 
    GeneralGood Job Pin
    clxye13-Dec-07 15:50
    clxye13-Dec-07 15:50 
    GeneralCTitleTip Class not found Pin
    123Asha4-Sep-07 20:40
    123Asha4-Sep-07 20:40 
    GeneralTo display Custom tooltiptext in taskbar Pin
    123Asha4-Sep-07 20:37
    123Asha4-Sep-07 20:37 
    QuestionCan this be used with app's taskbar button tooltip ??? Pin
    ana_v12311-Apr-06 13:01
    ana_v12311-Apr-06 13:01 
    GeneralStrage behaviour Pin
    iasen_st23-Aug-04 3:01
    iasen_st23-Aug-04 3:01 
    Generalproblem! pls help! Pin
    ns12-Nov-03 7:53
    ns12-Nov-03 7:53 
    GeneralTrying to use an edit box Pin
    ns7-Nov-03 4:22
    ns7-Nov-03 4:22 
    Questionnice work...and bug? Pin
    HighJack11-Feb-03 21:52
    HighJack11-Feb-03 21:52 
    GeneralExcellent Pin
    JohnJ3-Jan-03 0:41
    JohnJ3-Jan-03 0:41 
    QuestionHow can I use this titletip in CGridctrl. Pin
    Anonymous3-Nov-02 18:32
    Anonymous3-Nov-02 18:32 
    AnswerRe: How can I use this titletip in CGridctrl. Pin
    Chopper18-Dec-02 0:21
    Chopper18-Dec-02 0:21 
    Generalnice work Pin
    Kevin Smith26-Aug-02 9:44
    Kevin Smith26-Aug-02 9:44 
    GeneralSDI app Pin
    Kevin Smith23-Aug-02 8:52
    Kevin Smith23-Aug-02 8:52 
    GeneralCan't detect double click Pin
    hierro30-Nov-01 3:17
    hierro30-Nov-01 3:17 
    GeneralRe: Can't detect double click Pin
    Roger Allen16-Apr-02 6:25
    Roger Allen16-Apr-02 6:25 
    GeneralAn easier way... Pin
    R5-Aug-02 5:48
    R5-Aug-02 5:48 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.