Tooltip Control (without MFC) | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 25, 2000
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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";
}
Advertisement

Downloads

Download source - 3 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.