Click here to Skip to main content
Click here to Skip to main content
Go to top

A multiline extended tooltip control

, 7 Dec 1999
Rate this:
Please Sign up or sign in to vote.
A drop-in multiline extendable tooltip control

sample image 1  sample image 2 

COXToolTipCtrl Overview

DundasThis class is a free sample from Dundas Software's Ultimate Toolbox. Copyright © Dundas Software Ltd. 1997-1999, All Rights Reserved

COXToolTipCtrl is an extended tooltip control that allows multiline tooltips, plus extended tooltip text. Extended tooltip text is extra text that is displayed if the user clicks on the tooltip window. If the tooltip contains extended text (as well as a standard tooltip string) then the info window will contain a small arrow that prompts the user to click on the window. Once the window is clicked, the extended text is shown. If the window is clicked again then the window reduces to showing just the standard text.

The maximum width of the tooltips can be specified, and if the info text is too big to fit within these bounds then the text will be wrapped over multiple lines. The control also allows you to specify a different text and background colors for the tooltips, and the display font can also be changed.

This class is a direct replacement for the CToolTipCtrl class. It incorporates the entire API of the standard CToolTipCtrl, and introduces new features not found in the standard tooltip.

The control is used just like any other tooltip control. To use the tool simply call Create(...) and specify the parent window of the tool, then add tools to the control using the AddTool(...) member function. eg. (In a formview or dialog)

tooltip.Create(this)
tooltip.AddTool(GetDlgItem(IDC_CONTROL), 
               _T("Tooltip text\rThis is the extended\ntooltip text"));

where ID_CONTROL is the ID of a control.

To specify extended text for a tooltip, simply append a '\r' after your tooltip text, and then append the extended tooltip info.

As with the standard tooltip control you can specify the actual text for the tool at creation time (as shown above), or you can specify the LPSTR_TEXTCALLBACK value and provide a TTN_NEEDTEXT handler to return the text dynamically at runtime.

To handle the TTN_NEEDTEXT message, you will need to add a message handler in the parent window, and an entry in the message map, eg. in you view or form

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
... 
ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipNotify)
END_MESSAGE_MAP()

BOOL CMyDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    tooltip.Create(this);
    tooltip.AddTool(GetDlgItem(IDC_CONTROL), LPSTR_TEXTCALLBACK);
    ...
}

BOOL CMyDlg::OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
{ 
    TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR; 
    UINT nID = pNMHDR->idFrom;

    if (nID == IDC_CONTROL) // Fill in the text buffer
    {
        _tcscpy(pTTT->szText, _T("Tooltip text\rExtended tooltip text"));
        return TRUE;
    }

    return FALSE;
}

You can also supply text two alternate ways, either by supplying a string resource

pTTT->lpszText = MAKEINTRESOURCE(nID);
pTTT->hinst = AfxGetResourceHandle(); 
return TRUE;

or by supplying a pointer to the text:

pTTT->lpszText = _T("Tooltip text\rExtended tooltip text");
return TRUE;

Newline characters ('\n') can be embedded anywhere within the text or extended text to produce a multiline tooltip. If the width of the tooltip window is specified using SetMaxTipWidth() then the tooltip text will be wrapped to this length, and if necessary displayed on more than one line.

To change the font of the tooltips simply use the SetFont() member function.

The GetToolInfo/SetToolInfo functions, and the HitTest functions are very similar to the CToolTipCtrl versions except that they use a OXTOOLINFO structure instead of a TOOLINFO structure. This structure is defined as

struct 
OXTOOLINFO : public TOOLINFO {
#if
(_WIN32_IE <  0x0300)
    LPARAM lParam; //Application defined value that is associated with 
                   //the tool
#endif
    int nWidth; //Width of box, or 0 for default
    COLORREF clrTextColor; //text color
    COLORREF clrBackColor; //background color
}

and so is very similar to the standard TOOLINFO, and is used in the same way, with the exception that the uFlags member is not (yet) used.

To change the color of an individual tip, use the GetToolInfo/SetToolInfo functions

OXTOOLINFO ToolInfo;
if (m_toolTip.GetToolInfo(ToolInfo, GetDlgItem(IDC_CONTROL)))
{
    ToolInfo.clrBackColor = RGB(255, 255, 255);
    ToolInfo.clrTextColor = RGB( 0, 0, 255);
    m_toolTip.SetToolInfo(&ToolInfo);
}

License

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

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
 
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
 
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
 
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
BugRE: Not working in VS2008 Pinmembermla15429-Apr-13 9:07 
AnswerRe: RE: Not working in VS2008 Pinmembermla15429-Apr-13 9:25 
QuestionIs possibility to add an image to tool tip? Pinmembermrowca15-Jan-10 9:29 
GeneralMy vote of 1 PinmemberNisheeth White15-Oct-09 0:39 
GeneralNot necessary for multiline tool tips Pinmemberrichardwhitehead17-Oct-07 3:08 
GeneralHandling Overlapping Objects... PinmemberembeddedDude15-Feb-07 5:35 
Questioncompile time error in my library file Pinmembermailtochandra2000@yahoo.com8-Jan-07 22:23 
QuestionAny way to have tooltips longer than 80 characters long with this? Pinmemberthready3-Nov-06 14:02 
QuestionHow to use as tooltip for MDI App toolbars ? Pinmemberana_v12311-Apr-06 22:50 
How to use as tooltip for MDI App toolbars ?
Anyone tried? Any idea or direction ?
 
Thanks
Ana
 
Ana_v123
QuestionCan this be used with app's taskbar button tooltip ??? Pinmemberana_v12311-Apr-06 13:00 
GeneralLink on tooltip Pinmemberscvarz29-Mar-05 22:04 
Generalproblem with WS_EX_TOPMOST Pinmemberrpar10-Mar-05 21:12 
Generalmissing UpdateTipText PinmemberSGarratt28-Oct-04 14:33 
GeneralJust another bugfix PinmemberMarek Konopka20-Feb-04 11:30 
GeneralAnother Bug Fix PinmemberRussH5-Jan-04 10:08 
GeneralRe: Another Bug Fix Pinmemberziyoo30-Mar-04 16:18 
GeneralBug Fix PinmemberPJ Arends2-Nov-03 3:43 
GeneralRe-Position ToolTip Text PinmemberBrian Krzys21-Oct-03 4:36 
GeneralFeature Request/Consideration Pinmemberalan9316-Oct-03 9:31 
Generalresize Pinmemberbotsjeh14-Oct-03 10:32 
QuestionWhat does it mean PinmemberKaЯl16-Sep-03 23:01 
AnswerRe: What does it mean PinadminChris Maunder17-Sep-03 3:17 
GeneralThanks for your answer PinmemberKaЯl17-Sep-03 3:30 
GeneralProblems with resize controls PinmemberKondriSergey27-Jun-03 3:00 
QuestionHow to Hook TTN_NEEDTEXT? PinmemberZhanglu27-Apr-03 22:39 
GeneralUnicode PinmemberSveta Rizel23-Mar-03 20:37 
GeneralRe: Unicode PinmemberChopper21-May-03 6:01 
GeneralRe: Unicode PinadminChris Maunder21-May-03 6:27 
GeneralRe: Unicode PinmemberChopper21-May-03 6:37 
QuestionHow to use this class to display? Pinmemberxuchuangeng24-Dec-02 21:52 
GeneralBehavior differs from CToolTipCtrl PinmemberDave Aebi30-Oct-02 11:10 
Generaltooltip without control PinsussAnonymous8-Sep-02 23:50 
GeneralRe: tooltip without control PinadminChris Maunder9-Sep-02 2:33 
GeneralRe: tooltip without control PinsussDmitri8-Nov-02 9:53 
GeneralRe: tooltip without control PinmemberChopper18-Dec-02 0:16 
GeneralToolBar Support PinmemberJawache4-Sep-02 2:41 
Questioncan we use COXToolTipCtrl to produce multi-line tooltip for grids? Pinmemberlei17-May-02 4:57 
Generalmodeless dialog PinsussSteve Palmer25-Aug-00 12:10 
GeneralRe: modeless dialog PinmemberRick Morris29-Jan-01 14:24 
QuestionBug when using CRect in Add function? PinsussDon Sanders17-Jul-00 11:45 
AnswerUpdated version of COXToolTipCtrl::HitTest() PinsussDon Sanders18-Jul-00 11:52 
GeneralRe: Updated version of COXToolTipCtrl::HitTest() PinmemberRick Morris29-Jan-01 15:14 
GeneralUsing COXToolTipCtrl in an ActiveX PinsussLaurent30-May-00 2:53 
GeneralUsing COXToolTipCtrl in a CView PinsussWolfram Rösler20-Jan-00 4:35 
GeneralRe: Using COXToolTipCtrl in a CView PinmemberRick Morris28-Jan-01 4:51 
GeneralRe: Using COXToolTipCtrl in a CView PinmemberRick Morris29-Jan-01 14:23 
GeneralRe: Using COXToolTipCtrl in a CView PinmemberAnonymous28-May-01 16:27 
GeneralToolTip on CScrollView PinsussZuccaro Gianfranco12-Jan-00 21:05 
Questionhtml tooltips? PinsussUwe Keim11-Jan-00 19:32 
AnswerRe: html tooltips? PinmemberRuss Freeman9-Dec-00 11:41 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 8 Dec 1999
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid