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?
- Create tool tip by calling Create() method
- Set tool tip test by calling SetText() method
- 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";
}

Comments
thanks
Posted by prasaath on 07/19/2012 03:49amThanks for your class it helps more..........
Replyis this your "without MFC"????
Posted by jithinpg007 on 07/08/2008 07:45amhey 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
ReplyHow all this can be done with the standard tooltip control
Posted by Legacy on 04/20/2003 12:00amOriginally 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
-
Replyproblem with compex data
Posted by DieterHammer on 07/12/2005 04:02amThe 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.
ReplySimple Multiline Text
Posted by Legacy on 08/17/2002 12:00amOriginally posted by: Paul Shore
Reply
Stop Bitching
Posted by Legacy on 04/16/2002 12:00amOriginally 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!
ReplyOptimisations
Posted by Legacy on 03/01/2002 12:00amOriginally 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:00amOriginally 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 !
ReplyOn subsequent mouse moves it does not update the tool tip
Posted by Legacy on 11/14/2001 12:00amOriginally 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.
ReplyWhat modifications need to be made to make it be displayed under the cursor as it moves?
Cool idea! I may hijack it!
Posted by Legacy on 11/10/2001 12:00amOriginally 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...
ReplyFor Cursor & Timing
Posted by Legacy on 05/16/2001 12:00amOriginally posted by: Fabrizio
ReplyLoading, Please Wait ...