Click here to Skip to main content
11,635,705 members (76,640 online)
Click here to Skip to main content

CToolTipDialog class : a simple WTL class to enable tooltips in your dialogs

, 9 Nov 2003 CPOL 124.6K 3K 40
Rate this:
Please Sign up or sign in to vote.
Add this small class to your existing dialog inheritance list and get nice tooltips on controls and dialog.

Sample Image - TTDialogDemo.gif

Introduction

I am working on a WTL application with many dialogs, so I searched for some code implementing tooltips in dialogs in a straightforward way. I found much interesting code, but nothing was really what I needed, so I wrote this simple class which fits nearly all my needs. Hopefully it will fit yours.

Using the code

Before anything else

Before using the code you must include atlctrls.h and ToolTipDialog.h in your VC project. The best place to insert the code is the end of your stdafx.h file, otherwise you have to add it in each of your (existing and future) dialog file headers.

//
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
  ...
  ...
#include <span class="code-keyword"><atlctrls.h>
</span>
#include <span class="code-string">"ToolTipDialog.h"</span>

By the way if you want to use the nice balloon style, you must set the correct version of _WIN32_IE at the beginning of stdafx.h. The WTL Appwizard defaults it to 0x0400 and you would compile without this style.

//
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#define _WIN32_IE 0x0500

Very simple usage

This is what you will find in the sample About Dialog.

In any CDialogImpl derived class, for instance a CAboutDlg generated by appwizard or any existing CDialogImpl<MyDialog> of yours, do the following:

  • Add CToolTipDialog<CMyDialog> to the inheritance list
    //
    
    class CAboutDlg : public CDialogImpl<CAboutDlg>, 
        public CToolTipDialog<CAboutDlg>
        {
  • Add CHAIN_MSG_MAP(CToolTipDialog<CMyDialog>) on top of your dialog message map.
    //
    
        BEGIN_MSG_MAP(CAboutDlg)
            CHAIN_MSG_MAP(CToolTipDialog<CAboutDlg>)

You are nearly done.

From now, in the dialog initialization, a CToolTipCtrl will be created and all the dialog controls with an ID not equal to IDC_STATIC will get - if it exists in the string table - the string with same ID as tooltip. If there is no such string, you will have to use TTSetTxt (see later).

It is likely that most of your About dialog controls are static and have IDC_STATIC ID, so you may need to change their ID and create their tooltip string.

Go to the Dialog Editor, select the About dialog, select the application icon, go to properties, give the icon control a particular ID, say IDC_APPICON, and don't forget to check the notify property on. Same for the static text, let's call it IDC_APPTEXT.

Go to the string table editor and create the initial tooltip strings for the controls. This is what you can find in the sample and paste into your string table editor:

  • IDC_APPICON This is the Application Icon
  • IDC_APPTEXT This is the Application Copyright and Version

At last create a string table entry for the whole dialog with your dialog IDD_xxx :

  • IDD_ABOUTBOX This About Dialog is displaying information tips on itself

You are done. Build and enjoy.

Simple usage

This is what you will find in the sample Main Dialog.

As your dialog inherits from CToolTipDialog you can :

  • Activate or deactivate the ToolTip Control: void TTActivate( BOOL bActivate )
  • Set the ToolTip width: void TTSize( int nPixel )
  • Set a control or the whole dialog Tooltip text
    • by ID: void TTSetTxt( UINT idTool, _U_STRINGorID text )
    • by HWND: void TTSetTxt( HWND hTool, _U_STRINGorID text )

Both TTSetTxt methods will accept a resource ID or a LPTSTR as second parameter. You should use the HWND when you have a member for the control and for the whole dialog: it has no ID.

Here is the code in the sample which makes use of these methods :

LRESULT CMainDlg::OnBnClickedActivate(WORD /*wNotifyCode*/, 
                 WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{    
    TTActivate( IsDlgButtonChecked( wID ));
    return 0;
}

LRESULT CMainDlg::OnEnChangeEditTt(WORD /*wNotifyCode*/, 
               WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    ...  
    TTSetTxt( m_hWnd , txt );
    ...
}

LRESULT CMainDlg::OnNMReleasedcaptureSizeTt(int idCtrl, 
               LPNMHDR /*pNMHDR*/, BOOL& /*bHandled*/
{ 
    TTSize( 4 * SendDlgItemMessage(idCtrl,TBM_GETPOS,0,0)); 
    return 0; 
}

Advanced usage

The GetTT() member will return a reference to the CToolTipCtrl (or other, see later) through which you can perform any operation.

For instance:

//
...
CToolTipCtrl & rTT=GetTT();
rTT.SetDelayTime( TTDT_AUTOPOP, 10000 )// set the show delay to 10 seconds
... 

If you want to change the initial style of the ToolTip Control

you may change it in your dialog class constructor. Construct the ToolTip with different parameters, but remember that TTF_SUBCLASS is what makes it work. The constructor is:

CToolTipDialog::CToolTipDialog( UINT uTTSTyle= TTS_NOPREFIX | TTS_BALLOON ,
     UINT uToolFlags = TTF_IDISHWND | TTF_SUBCLASS )

If you dynamically add controls

you can associate them a tooltip with:

  • BOOL TTAdd( HWND hTool ) or
  • BOOL TTAdd( UINT idTool )

Both will create the tooltip and assign it the string of same control ID if it exists. They return TRUE on success, FALSE on failure.

If you dynamically remove controls

prudently use:

  • void TTRemove( HWND hTool ) or
  • void TTRemove( UINT idTool )

before deletion.

For any window with controls or children

which is not a dialog or property page, you will have to explicitly call void TTInit(). If you want a default ToolTip string, your class must have an IDD member (as dialog classes): simply insert in the class declaration

  • enum { IDD = IDD_MYWINDOW };

and put the relevant string in the string table.

Very advanced usage

You can use your own ToolTip class, simply declare it through the second template parameter:

//

class CMyDlg : public CDialogImpl<CMyDlg>, 
    public CToolTipDialog<CMyDlg, CMyToolTip>
    {

The GetTT() member will then return a reference to the CMyToolTip. You will not have to derive from CToolTipDialog if your tooltip control members expose the same signature, for instance if CMyToolTip is derived from CToolTipCtrl.

For the end

Finally, it is very easy to have ToolTips in dialogs, provided you don't try to manage all these TTN_xxx.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Alain Rist
France France
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionHelp add tooltip to controls created at runtime Pin
Chinbat5-Sep-11 17:42
memberChinbat5-Sep-11 17:42 
QuestionWhy are we using RelayEvent() and TTF_SUBCLASS? Pin
VinceV18-Jun-10 20:06
memberVinceV18-Jun-10 20:06 
AnswerRe: Why are we using RelayEvent() and TTF_SUBCLASS? Pin
Alain Rist18-Jun-10 20:59
memberAlain Rist18-Jun-10 20:59 
GeneralRe: Why are we using RelayEvent() and TTF_SUBCLASS? Pin
andrey_k219-Dec-12 1:50
memberandrey_k219-Dec-12 1:50 
QuestionWorks *almost* perfectly out of the box... [modified] Pin
TimMagee30-Jun-08 13:45
memberTimMagee30-Jun-08 13:45 
AnswerRe: Works *almost* perfectly out of the box... Pin
Alain Rist1-Jul-08 21:29
memberAlain Rist1-Jul-08 21:29 
GeneralRe: Works *almost* perfectly out of the box... Pin
TimMagee10-Jul-08 13:10
memberTimMagee10-Jul-08 13:10 
GeneralBug on ComboBox with 'Dropdown' style Pin
mn_lay30-Dec-07 3:24
membermn_lay30-Dec-07 3:24 
AnswerRe: Bug on ComboBox with 'Dropdown' style Pin
Alain Rist18-Jan-08 7:34
memberAlain Rist18-Jan-08 7:34 
QuestionNice class but I have a problem with a DLL/activeX Pin
donthaveanidea11-Oct-07 21:35
memberdonthaveanidea11-Oct-07 21:35 
AnswerRe: Nice class but I have a problem with a DLL/activeX Pin
Alain Rist12-Oct-07 11:32
memberAlain Rist12-Oct-07 11:32 
GeneralTooltips disappear Pin
lroels26-Sep-07 21:15
memberlroels26-Sep-07 21:15 
GeneralRe: Tooltips disappear Pin
Alain Rist26-Sep-07 21:43
memberAlain Rist26-Sep-07 21:43 
GeneralUse &quot;TTN_GETDISPINFO&quot; to support multi-line tooltip [modified] Pin
shaohao24-Aug-06 8:29
membershaohao24-Aug-06 8:29 
GeneralRe: Use &quot;TTN_GETDISPINFO&quot; to support multi-line tooltip Pin
Alain Rist24-Aug-06 8:56
memberAlain Rist24-Aug-06 8:56 
GeneralRe: Use &quot;TTN_GETDISPINFO&quot; to support multi-line tooltip Pin
shaohao25-Aug-06 2:56
membershaohao25-Aug-06 2:56 
GeneralRe: Use &quot;TTN_GETDISPINFO&quot; to support multi-line tooltip Pin
Alain Rist25-Aug-06 6:13
memberAlain Rist25-Aug-06 6:13 
GeneralRe: Use "TTN_GETDISPINFO" to support multi-line tooltip Pin
shaohao25-Aug-06 22:00
membershaohao25-Aug-06 22:00 
AnswerRe: Use &amp;amp;quot;TTN_GETDISPINFO&amp;amp;quot; to support multi-line tooltip [modified] Pin
Alain Rist26-Aug-06 6:43
memberAlain Rist26-Aug-06 6:43 
GeneralRe: Use TTN_GETDISPINFO to support multi-line tooltip Pin
mn_lay30-Dec-07 5:50
membermn_lay30-Dec-07 5:50 
GeneralDoesn't work in DLLs Pin
MarkWoodard5-May-06 10:37
memberMarkWoodard5-May-06 10:37 
GeneralRe: Doesn't work in DLLs Pin
Alain Rist5-May-06 20:11
memberAlain Rist5-May-06 20:11 
QuestionGarbage values being displayed in tooltip Pin
CodeFundu2-Apr-06 20:17
memberCodeFundu2-Apr-06 20:17 
AnswerRe: Garbage values being displayed in tooltip Pin
Alain Rist4-Apr-06 11:55
memberAlain Rist4-Apr-06 11:55 
GeneralRe: Garbage values being displayed in tooltip Pin
CodeFundu4-Apr-06 12:01
memberCodeFundu4-Apr-06 12:01 
GeneralRe: Garbage values being displayed in tooltip Pin
Alain Rist4-Apr-06 12:18
memberAlain Rist4-Apr-06 12:18 
QuestionUsing TTN_NEEDTEXT Pin
CodeFundu13-Mar-06 2:07
memberCodeFundu13-Mar-06 2:07 
AnswerRe: Using TTN_NEEDTEXT Pin
Alain Rist13-Mar-06 2:41
memberAlain Rist13-Mar-06 2:41 
GeneralRe: Using TTN_NEEDTEXT Pin
CodeFundu13-Mar-06 23:00
memberCodeFundu13-Mar-06 23:00 
GeneralFor non-static controls Pin
Sammy D'Souza16-Oct-04 20:14
memberSammy D'Souza16-Oct-04 20:14 
GeneralRe: For non-static controls Pin
Alain Rist16-Oct-04 20:48
memberAlain Rist16-Oct-04 20:48 
GeneralI for some reason can't figure out how to use your code Pin
win32newb12-Mar-04 8:54
memberwin32newb12-Mar-04 8:54 
GeneralRe: I for some reason can't figure out how to use your code Pin
Alain Rist12-Mar-04 20:30
memberAlain Rist12-Mar-04 20:30 
Questionwindows 2000? Pin
AlphaDog6-Feb-04 15:41
memberAlphaDog6-Feb-04 15:41 
AnswerRe: windows 2000? Pin
Alain Rist6-Feb-04 20:55
memberAlain Rist6-Feb-04 20:55 
GeneralRe: windows 2000? Pin
AlphaDog7-Feb-04 15:09
memberAlphaDog7-Feb-04 15:09 
GeneralRe: windows 2000? Pin
Alain Rist8-Feb-04 22:27
memberAlain Rist8-Feb-04 22:27 
GeneralSmall problem Pin
Philip Lee26-Nov-03 23:59
sussPhilip Lee26-Nov-03 23:59 
GeneralRe: Small problem Pin
Alain Rist27-Nov-03 0:51
memberAlain Rist27-Nov-03 0:51 
Generalnice, little bug Pin
Michal Mecinski11-Nov-03 0:45
memberMichal Mecinski11-Nov-03 0:45 
GeneralRe: nice, little bug Pin
Alain Rist11-Nov-03 2:07
memberAlain Rist11-Nov-03 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 10 Nov 2003
Article Copyright 2003 by Alain Rist
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid