Click here to Skip to main content
15,892,697 members
Articles / Desktop Programming / MFC
Article

CXInfoTip - Information Tooltip

Rate me:
Please Sign up or sign in to vote.
4.85/5 (21 votes)
21 Sep 20012 min read 130.4K   4.4K   65   21
CXInfoTip is an enhanced information tooltip control.

Sample Image - XInfotip.jpg

Introduction

If you use Windows 2000, you may have noticed a special tooltip that is used in some instances. At least one spot where it is displayed is after making a dial-up network connection.

CXInfoTip is a simple control that attempts to reproduce this tooltip. It supports multi-line text (separated with a '\n') and an icon. The control can be used as a normal tooltip for controls or as an immediate popup data tip. It is not a direct replacement for CTooltip. The control itself is in the source files: CXInfoTip.h and CXInfoTip.cpp.

To use the control, create it and either call the immediate Show() method or add tools using AddTool(). If AddTool() is used then the RelayEvent() method must be called from the parent window's overridden PreTranslateMessage().

To use the control, add a CXInfoTip member variable to the class.

CXInfoTip m_Tip;

For dialogs, create it in OnInitDialog(). For other windows create it in OnCreate().

m_Tip.Create(this);

To show an immediate tooltip, call the Show() method.

m_Tip.Show(_T("This is a toolip!"), CPoint(100, 100));

To show normal tooltips, use AddTool() for each control and call RelayEvent() from PreTranslateMessage().

m_Tip.AddTool(GetDlgItem(IDOK), 
        _T("This is the OK button!"), m_TooltipIcon);
....
BOOL CInfoTipTestDlg::PreTranslateMessage(MSG* pMsg) 
{
    m_Tip.RelayEvent(pMsg);
    ...
}

Methods

  • CInfoTip::Create

    BOOL Create (CWnd * pParentWnd)

    Creates the tooltip control.

    Return value

    TRUE on success, FALSE otherwise

    Parameters

    • pParentWnd

      Pointer to the parent window.

  • CInfoTip::AddTool

    void AddTool(CWnd *pWnd, LPCTSTR szTooltipText, HICON hIcon = NULL)

    Adds a tool.

    Return value

    None

    Parameters

    • pWnd

      Pointer to the tool window.

    • szTooltipText

      Text to display in the tooltip for the window. Separate multiple lines with a '\n'.

    • hIcon

      Icon to display in the tooltip or NULL for no icon.

  • CInfoTip::RemoveTool

    void RemoveTool(CWnd *pWnd)

    Removes a tool.

    Return value

    None

    Parameters

    • pWnd

      Pointer to the tool window to remove.

  • CInfoTip::SetIcon

    void SetIcon(HICON hIcon)

    Sets the icon to show for immediate tooltips.

    Return value

    None

    Parameters

    • hIcon

      Handle to the icon to display.

  • CInfoTip::SetShowDelay

    void SetShowDelay(int nDelay)

    Adjusts the delay before a tooltip is displayed.

    Return value

    None

    Parameters

    • nDelay

      Delay in milliseconds.

  • CInfoTip::SetFont

    void SetFont(CFont *pFont)

    Sets the tooltip text font.

    Return value

    None

    Parameters

    • pFont

      Pointer to the font.

  • CInfoTip::RelayEvent

    void RelayEvent(LPMSG lpMsg)

    Relays mouse events to the control. If AddTool() is used, then this method must be called from PreTranslateMessage().

    Return value

    None

    Parameters

    • lpMsg

      Pointer to the message structure.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

 
QuestionTextBox? Pin
ba062324-Jan-14 20:34
ba062324-Jan-14 20:34 
GeneralThanks Pin
Member 1031637822-Oct-13 19:26
Member 1031637822-Oct-13 19:26 
GeneralMy vote of 5 Pin
zhanglipeng19-Dec-12 16:35
zhanglipeng19-Dec-12 16:35 
Generalyou are a gread fellow Pin
xfxtianyi30-Dec-09 3:39
xfxtianyi30-Dec-09 3:39 
GeneralThank you, very usefull. Pin
weeyu26-Apr-09 3:30
weeyu26-Apr-09 3:30 
Questionbug or feature? Pin
alex__b22-Mar-09 7:29
professionalalex__b22-Mar-09 7:29 
QuestionHow can i use CXInfoTip for displaying tooltips for toolbar buttons? Pin
hitman_guy16-May-07 19:30
hitman_guy16-May-07 19:30 
QuestionCopy/paste from balloon Pin
evegnibmw3-Aug-06 21:15
evegnibmw3-Aug-06 21:15 
GeneralThank you, very usefull. Pin
caesten28-Apr-06 9:01
caesten28-Apr-06 9:01 
GeneralMany thanks Pin
jrivero13-Nov-05 14:19
jrivero13-Nov-05 14:19 
GeneralSmall bug Pin
tézé23-Feb-05 19:25
tézé23-Feb-05 19:25 
GeneralReally nice work Pin
John Norris1-Jul-04 4:14
John Norris1-Jul-04 4:14 
GeneralUpdate RelayEvent(LPMSG lpMsg) Pin
tpchen12-Apr-04 21:20
tpchen12-Apr-04 21:20 
QuestionHow to make this work in ATL? Pin
Arvind Ganesan30-Jul-03 4:40
Arvind Ganesan30-Jul-03 4:40 
GeneralNice work and how to use on TrayIcon tooptips Pin
Anonymous22-Sep-02 14:39
Anonymous22-Sep-02 14:39 
GeneralNice work but small problem Pin
27-Sep-01 21:44
suss27-Sep-01 21:44 
GeneralRe: Nice work but small problem Pin
Kurt M. S.12-Aug-02 9:28
Kurt M. S.12-Aug-02 9:28 
First, I would like to say that I have found this class extremely useful, and a very simple and clear example of drawing custom non-rectangular windows.

Displaying the bubble to the right when it would display off the screen is a relatively easy fix. The only problem with the solution I am giving is that you will see a flicker of a left bubble before it is redrawn as a right bubble since I use the results of the initial region computation to determine whether the bubble will be clipped. This was done for efficiency, otherwise I would have to recompute the window region with every mouse move. (My application "floats" the bubble as the mouse moves over the screen and I call Show() with every mouse move to make the bubble follow the cursor). You can re-arrange the code I have given if this doesn't work for you.

First, add two new data members to CXInfoTip:

BOOL m_bLeftBubble;
CSize m_WindowSize;

Second, change the references to the local variable WindowSize to use the class variable m_WindowSize to store the computed size of the window (in the Show() method and the OnTimer() method).

Third, after getting the cursor position in the Show() method, determine whether the type of bubble. Basically, we will draw a left bubble if the origin offset by window size width yield a positive x coordinate, otherwise draw a right bubble.

m_bLeftBubble = m_ptOrigin.x - m_WindowSize.cx >= 0;

Fourth, change the triangle leader based on whether we are drawing a left or right bubble:

// Calculate the leader triangle coordinates

if ( m_bLeftBubble )
{
ptLeader[0].x = rcWnd.Width() - CX_ROUNDED;
ptLeader[0].y = rcWnd.Height() - CY_ROUNDED;

ptLeader[1].x = ptLeader[0].x;
ptLeader[1].y = ptLeader[0].y + CY_LEADER;

ptLeader[2].x = ptLeader[0].x - CX_LEADER;
ptLeader[2].y = rcWnd.Height() - CY_ROUNDED;
}
else
{
ptLeader[0].x = CX_ROUNDED;
ptLeader[0].y = rcWnd.Height() - CY_ROUNDED;

ptLeader[1].x = ptLeader[0].x;
ptLeader[1].y = ptLeader[0].y + CY_LEADER;

ptLeader[2].x = ptLeader[0].x + CX_LEADER;
ptLeader[2].y = rcWnd.Height() - CY_ROUNDED;
}

Finally, when showing the bubble, set the window position based on whether we are positioning the triangle leader for a left or right bubble in the OnTimer() function (replace the call to SetWindowPos to the conditional call within the timerShow case):

if ( m_bLeftBubble )
SetWindowPos(&wndTop, m_ptOrigin.x - m_WindowSize.cx + CX_ROUNDED, m_ptOrigin.y - m_WindowSize.cy + CY_ROUNDED, m_WindowSize.cx, m_WindowSize.cy, SWP_NOACTIVATE | SWP_SHOWWINDOW);
else
SetWindowPos(&wndTop, m_ptOrigin.x - CX_ROUNDED, m_ptOrigin.y - m_WindowSize.cy + CY_ROUNDED, m_WindowSize.cx, m_WindowSize.cy, SWP_NOACTIVATE | SWP_SHOWWINDOW);

That's it. It took me all of 15 minutes to do this, testament to the class author's good design. I am sure it would be equally simple to check for the top-screen clipping as well.

Regards,
Kurt

Kurt M. S.
GeneralRe: Nice work but small problem Pin
shawkins31-May-06 14:25
shawkins31-May-06 14:25 
GeneralTwo minor bugs & Fixes Pin
Jason De Arte25-Sep-01 9:13
Jason De Arte25-Sep-01 9:13 
GeneralCompile falure on CMAP Pin
Bob Eastman23-Sep-01 11:14
Bob Eastman23-Sep-01 11:14 
GeneralRe: Compile falure on CMAP Pin
Paolo Messina23-Sep-01 15:29
professionalPaolo Messina23-Sep-01 15:29 

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.