Tooltip Control (without MFC)

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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read