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


Comments

  • What would exhort existence settle current to be more colorful of Studio

    Posted by motherdhmm on 06/04/2013 01:41pm

    [url=http://www.headphonescheaponlineaustralia.com/]beats by dre[/url] Starting with the to the fore complete, it is distinctly that the caricature agreeable does not shoulder the ¡°tm¡± (trademark) hieroglyph under the dope ¡°studio¡± on the bum radical management corner of the strike (may be a midget under age, but you can conjure up that a lilliputian second of reader is missing from the picture on the fitting). At the back of the slug, you can conscious of that the pictures of the headphones are slightly different. They are positioned slightly differently. Then there¡¯s also the scarcity of the ¡± tm ¡°sign beneath the¡± studio ¡°gen at the bottom left corner.¡± tm ¡°is also missing on the vertical verse on the radical of the crate underground the word¡± dre ¡°, and is also missing from the pattern hire of the model paragraph of text. [url=http://www.headphonescheaponlineaustralia.com/dre-beats-studio-c-66_68_71.html]beats studio[/url] Some people think that to travesty Jordan, Kobe Bryant, this is not pretend, but if the theory of the high-quality of their own accessories, and Bryant suffer with their own unique vision, which has a mammoth relationship and he was Europe¡¯s fashion cap Milan grew up, from slight monasteriesBryant has been wearing all kinds of form sunglasses, and every so often there is a negligible diamond stud earrings, there is a pompously ¡°Monster¡± regard cups in their culmination ¨C even the Monster headphones. he intent also own different colors, standard corpse-like, there are violet and gold with the Lakers. [url=http://blog.cheapbeatsbydre.co.nz/beats-by-dre-headphones]beats by dre headphones[/url] Eyesore iBeats Headphones with ControlTalk In-Ear Headphones ,has Advanced engineering reproduces high judgement sound from your iPhone or iPod for the treatment of a listening taste other buds are unequipped to deliver. Chic driver fashion pumps out the deepest bass, soaring highs, and ultra-clear midrange so you can end hearing the music and start listening to it. Lightweight for listening while exercising or when you¡¯re on the go. Sedulous chrome earphone finishes for hard durability and permanent shine. Precision-machined single-billet metal housing prevents vibrations and unwelcome sonic artifacts from tainting your listening experience.

    Reply
  • ghd glattejern populære familie, som skal produktet

    Posted by motherdhmm on 05/30/2013 02:37pm

    [url=http://www.buy-beatsdrdre.com/]beats by dre headphones[/url] Ligegyldigt hvilken slags hår fladjern du vil købe, hvordan det glatte hår er også meget vigtigt. Men du må hellere bruge bedre Billige GHD Glattejern for dit hår, bare for at holde dit hår er health.So, så tøv ikke mere, lige kommet til den officielle online butik for GHD glattejern. [url=http://www.blog.cheapbeatsbydre.co.nz/]beats by dre nz[/url] vedrørende alle vigtige ingredienser inde i make-up middel som er som kan tage i de store uddannelse din energi og arbejde desuden [url=http://www.buy-beatsdrdre.com/category/monster-beats-australia]monster beats australia[/url] Det har længe været bevist at ghd utvivlsomt er markedsledende med verdens bedste glattejern. Vi har hele sortimentet / udvalget af ghd glattejern til de bedste priser og her hos NiceHair.dk får du også leveret dit ghd glattejern direkte til døren helt og aldeles gratis.

    Reply
  • 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
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • According to a recent Forrester total economic impact (TEI) study, enterprises can see a significant reduction in total cost of ownership by accessing Oracle Database in the cloud with a pay-as-you-go subscription model. This subscription service gives businesses the ability to scale up application environments for rapid prototyping, with far less time devoted to procuring licenses and deploying IT infrastructure. Read this study to learn how three different companies use Oracle Database in the cloud and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds