Click here to Skip to main content
15,868,292 members
Articles / Desktop Programming / MFC
Article

A multiline extended tooltip control

Rate me:
Please Sign up or sign in to vote.
4.42/5 (20 votes)
7 Dec 1999CPOL 328.1K   9.5K   75   51
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)


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.

Comments and Discussions

 
Questioncan we use COXToolTipCtrl to produce multi-line tooltip for grids? Pin
17-May-02 4:57
suss17-May-02 4:57 
Generalmodeless dialog Pin
Steve Palmer25-Aug-00 12:10
Steve Palmer25-Aug-00 12:10 
GeneralRe: modeless dialog Pin
29-Jan-01 14:24
suss29-Jan-01 14:24 
QuestionBug when using CRect in Add function? Pin
donaldsa17-Jul-00 11:45
donaldsa17-Jul-00 11:45 
AnswerUpdated version of COXToolTipCtrl::HitTest() Pin
donaldsa18-Jul-00 11:52
donaldsa18-Jul-00 11:52 
GeneralRe: Updated version of COXToolTipCtrl::HitTest() Pin
29-Jan-01 15:14
suss29-Jan-01 15:14 
GeneralUsing COXToolTipCtrl in an ActiveX Pin
Laurent30-May-00 2:53
Laurent30-May-00 2:53 
GeneralUsing COXToolTipCtrl in a CView Pin
Wolfram Rösler20-Jan-00 4:35
Wolfram Rösler20-Jan-00 4:35 
I have a CView-derived class that uses a CToolTipCtrl to display tool tips for different areas
of the window. I defined these areas by calling MyToolTipCtrl.AddTool(this,"Text",Rect,Id)
from one of my CView's member functions. Id is a number that's incremented for every new
tool added, and Rect is the area for which I want "Text" to be displayed as a tool tip.
This all works perfectly with CToolTipCtrl, but when using COXToolTipCtrl instead, nothing
happens - it compiles fine, but no tool tips show up.

Any idea what's to be done
GeneralRe: Using COXToolTipCtrl in a CView Pin
28-Jan-01 4:51
suss28-Jan-01 4:51 
GeneralRe: Using COXToolTipCtrl in a CView Pin
29-Jan-01 14:23
suss29-Jan-01 14:23 
GeneralRe: Using COXToolTipCtrl in a CView Pin
28-May-01 16:27
suss28-May-01 16:27 
AnswerRe: html tooltips? Pin
9-Dec-00 11:41
suss9-Dec-00 11:41 
AnswerRe: html tooltips? Pin
Chopper3-Dec-02 20:28
Chopper3-Dec-02 20:28 

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.