Tooltip Control (without MFC)

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Environment: Visual C++ 6 SP3

MFC Tool tip control(CToolTipCtrl) is easy to use with a window, such as a child window, or an application-defined rectangular area within a window's client area. However, in some situations you may not know in advance for which controls or rectangle area you will be displaying the tool tip. In this case, you have to dynamically display the tool tip depending on where your mouse position is. For e.g. you may have a window which display some graphics and you may want to display the tool tip on the graphics depending on where the mouse position is.

I developed a MFC look alike tool tip control, which is easy to use in this kind of scenarios. Just create the control, set the text and call the show method and see it working.

Steps to use the tool tip control in your code?

  1. Create tool tip by calling Create() method
  2. Set tool tip test by calling SetText() method
  3. Call Show() to display tool tip

When to use this control?

For most purposes MFC tool tip control should satisfy your need. Use this control only when you need to dynamically display tool tip at a given coordinate.

Enhancement

This is a bare minimum tool tip control and the following additional functionalities can be easily added to make it more useful and production ready.
  • Support for multi line tool tip text
  • Automatically killing tool tip after few seconds(Hint: Use timer)
  • Positioning tool tip so that it won't go beyond desk top window

Sample Code Demonstrating how to use CToolTip2

File: YourWnd.h

#include "tooltip2.h"
CYourWnd::public CWnd
{
DECLARE_MESSAGE_MAP()
//Other code
private:
CToolTip2 m_ToolTip;
protected:
//{{AFX_MSG(CYourWnd)
 afx_msg void OnMouseMove( UINT nFlags, CPoint point );
 //}}AFX_MSG
}

File: YourWnd.cpp

#include "YourWnd.h"
BEGIN_MESSAGE_MAP(CYourWnd, CWnd)
 //{{AFX_MSG_MAP(CYourWnd)
 ON_WM_MOUSEMOVE()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

//Constructor
CYourWnd::CYourWnd()
{
//Other initialization
m_ToolTip.Create(this);
}

CYourWnd::OnMouseMove ( UINT nFlags, CPoint point ))
{
m_ToolTip .SetText(GetDescription(point));
m_ToolTip.Show();
CWnd::OnMouseMove(nFlags, point);
}

//NOTE: Implement your own GetDescription.
//This is provided just for illustration.
CString CYourWnd::GetDescription(CPoint point)
{
if ( point.x > 100 && point.x < 200 ) && (point.y > 100 && point.y < 300)
 return "Daddy";
else if ( point.x > 200 && point.x < 275) && (point.y > 130 && point.y < 280)
 return "Mummy";
else
 return "Kiddy";
}

Downloads

Download source - 3 Kb


Comments

  • thanks

    Posted by prasaath on 07/19/2012 03:49am

    Thanks for your class it helps more..........

    Reply
  • is this your "without MFC"????

    Posted by jithinpg007 on 07/08/2008 07:45am

    hey dude, you hav captioned ur topics as tooltiptext without mfc and you hav all used the whole program in MFC only... toooooooooooo bad.......... plz specify how to set a tooltip text in pure VC++ not MFC

    Reply
  • How all this can be done with the standard tooltip control

    Posted by Legacy on 04/20/2003 12:00am

    Originally posted by: Jose Insa

    All this can be done with the standard Tooltip ctrl, search in MSDN for article "Tiptoe Through the ToolTips", this article explains the use of tooltips very well.
    However, I still appreciate your contribution and respect everyone who shares their code or findings here. I don't understand people who make negative statements without helping in any way others...pointless

    • problem with compex data

      Posted by DieterHammer on 07/12/2005 04:02am

      The MSDN article is very fine, for my application it has only one problem. It needs a hittest for every MouseMove. If I have a very complex CAD document, this speeds down the mouse. I have to create my own event "Mouse doesn't move" and only then check hittest.

      Reply
    Reply
  • Simple Multiline Text

    Posted by Legacy on 08/17/2002 12:00am

    Originally posted by: Paul Shore

    The following changes in DisplayToolTip() will enable multiline tips.
    
    

    //original CSize size = pDC->GetTextExtent(m_szText);
    //original pDC->LPtoDP(&size);
    CRect lpRect; //new
    pDC->DrawText(m_szText, lpRect, // new
    DT_LEFT|DT_NOPREFIX|DT_EXTERNALLEADING|DT_CALCRECT); //new
    CSize size(lpRect.Width(), lpRect.Height()); // new

    //original pDC->SetTextAlign(TA_LEFT);
    //original pDC->TextOut(3,1, m_szText);
    pDC->DrawText(m_szText, CRect(3,1,size.cx+3, size.cy+1), // new
    DT_LEFT|DT_NOPREFIX|DT_EXTERNALLEADING); // new

    Call like this:

    m_ToolTip.SetText(_T("Some Tip Text\r\nOn Two Lines"));

    Reply
  • Stop Bitching

    Posted by Legacy on 04/16/2002 12:00am

    Originally posted by: John Knight

    I often wonder why people bother contributing to this site when I see people moaning or trying to prove that they can do it better.

    Anyone who contributes to this site deserves respect. They have tried their best and are willing to share their ideas to HELP others.

    You don't have to use their code, you can draw lots of ideas from it.

    Give the programmers a break!

    Reply
  • Optimisations

    Posted by Legacy on 03/01/2002 12:00am

    Originally posted by: Amn

    Thanks a lot for your help... i hate Windows, but i shall have to program a bit more for this platform... everything is damn upside down ... can't believe i have spent 4 hours fixing a tool tip control that is to be built from scratch, since the CToolTip wrapper can't do most of the things i need.. talk about extendability...

    Well, anyway your code saved me :)

    I know how to make it 2) smaller (no members :), and 2) faster (about 20 - 30%)

    here it goes:

    remove the CPaintDC dc(this) initialisation in OnPaint since you don't need it anyway (you perform all the drawing in the DisplayToolTip function).

    use the CS_OWNDC class style to have an exclusive device context (you won't need to release it, and the tool tips are so small Windows won't mind have an exclusive device context to your tooltips, and since there can be only one at a time, CS_OWNDC is perfect for the job.

    Thanks again for the code !

    Amn.
    Norway.

    Reply
  • It's still MFC based !

    Posted by Legacy on 01/14/2002 12:00am

    Originally posted by: andy k.

    Think your title is wrong, if u use CWnd, and include stdafx.h, you carry all the MFC in your program. Searched a "pure Win32" ballon application? Your one isn't !

    Reply
  • On subsequent mouse moves it does not update the tool tip

    Posted by Legacy on 11/14/2001 12:00am

    Originally posted by: Jaky

    The tool tip that is displayed only appears at the first point that the mousemove supplies. On subsequent mouse moves it does not update the tool tip, it does not disapear and just stays displayed at the first point.
    What modifications need to be made to make it be displayed under the cursor as it moves?

    Reply
  • Cool idea! I may hijack it!

    Posted by Legacy on 11/10/2001 12:00am

    Originally posted by: David Lindauer

    I could've used this earlier... at least the idea. I went through major contortions to get the UI's tooltip control to do this and it still doesn't work quite right. I don't have access to MFC or even C++ right now so I may hack it up...

    Reply
  • For Cursor & Timing

    Posted by Legacy on 05/16/2001 12:00am

    Originally posted by: Fabrizio

    Very cool code... the MFC CToolTipCtrl is more complex and... not very functionaly as this. 
    
    Compliments!

    For Cursor Position and Timing, I modified your code
    with this (m_nTimes is a private numeric property of CToolTip2):

    BOOL CToolTip2::Show(const CPoint& rCurrentPoint)
    {
    ASSERT(this != NULL );
    ASSERT(m_hWnd != NULL );

    if (m_szText.IsEmpty())
    {
    // MODIFIED : Otherwise don't close if Showing...
    Close();
    return FALSE;
    }

    if (m_bShowStatus)
    {
    if (m_ptCurrent == rCurrentPoint)
    return FALSE;

    // MODIFIED : Close and reshow with new Position..
    Close();
    return Show(rCurrentPoint);
    }
    else
    {
    // MODIFIED : Show if cursor don't move 10 times
    if (m_ptCurrent == rCurrentPoint)
    m_nTimes++;

    if (m_nTimes < 10)
    {
    m_ptCurrent = rCurrentPoint;
    return FALSE;
    }
    }

    m_ptCurrent = rCurrentPoint;
    m_bShowStatus = TRUE;
    //Show tool tip
    DisplayToolTip(rCurrentPoint);
    return TRUE;
    }

    //Close the tooltip
    void CToolTip2::Close()
    {
    ASSERT(this != NULL );
    ASSERT(m_hWnd != NULL );

    ShowWindow(SW_HIDE); //Hide tooltip
    m_bShowStatus = FALSE;

    // MODIFIED : Reset Timing counter....
    m_nTimes = 0;
    }


    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date