Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / WTL
Article

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

Rate me:
Please Sign up or sign in to vote.
4.83/5 (15 votes)
9 Nov 2003CPOL4 min read 166K   3.5K   41   41
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 <atlctrls.h>
#include "ToolTipDialog.h"

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)


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHelp add tooltip to controls created at runtime Pin
late_starter5-Sep-11 17:42
late_starter5-Sep-11 17:42 
QuestionWhy are we using RelayEvent() and TTF_SUBCLASS? Pin
Vince Valenti18-Jun-10 20:06
Vince Valenti18-Jun-10 20:06 
AnswerRe: Why are we using RelayEvent() and TTF_SUBCLASS? Pin
Alain Rist18-Jun-10 20:59
Alain Rist18-Jun-10 20:59 
GeneralRe: Why are we using RelayEvent() and TTF_SUBCLASS? Pin
andrey_k219-Dec-12 1:50
andrey_k219-Dec-12 1:50 
QuestionWorks *almost* perfectly out of the box... [modified] Pin
TimMagee30-Jun-08 13:45
TimMagee30-Jun-08 13:45 
AnswerRe: Works *almost* perfectly out of the box... Pin
Alain Rist1-Jul-08 21:29
Alain Rist1-Jul-08 21:29 
GeneralRe: Works *almost* perfectly out of the box... Pin
TimMagee10-Jul-08 13:10
TimMagee10-Jul-08 13:10 
GeneralBug on ComboBox with 'Dropdown' style Pin
Anton Lesnichenko30-Dec-07 3:24
Anton Lesnichenko30-Dec-07 3:24 
AnswerRe: Bug on ComboBox with 'Dropdown' style Pin
Alain Rist18-Jan-08 7:34
Alain Rist18-Jan-08 7:34 
QuestionNice class but I have a problem with a DLL/activeX Pin
donthaveanidea11-Oct-07 21:35
donthaveanidea11-Oct-07 21:35 
AnswerRe: Nice class but I have a problem with a DLL/activeX Pin
Alain Rist12-Oct-07 11:32
Alain Rist12-Oct-07 11:32 
GeneralTooltips disappear Pin
lroels26-Sep-07 21:15
lroels26-Sep-07 21:15 
GeneralRe: Tooltips disappear Pin
Alain Rist26-Sep-07 21:43
Alain Rist26-Sep-07 21:43 
GeneralUse &quot;TTN_GETDISPINFO&quot; to support multi-line tooltip [modified] Pin
shaohao24-Aug-06 8:29
shaohao24-Aug-06 8:29 
GeneralRe: Use &quot;TTN_GETDISPINFO&quot; to support multi-line tooltip Pin
Alain Rist24-Aug-06 8:56
Alain Rist24-Aug-06 8:56 
Hi,

Why do you want to make this change?

cheers,
AR
GeneralRe: Use &quot;TTN_GETDISPINFO&quot; to support multi-line tooltip Pin
shaohao25-Aug-06 2:56
shaohao25-Aug-06 2:56 
GeneralRe: Use &quot;TTN_GETDISPINFO&quot; to support multi-line tooltip Pin
Alain Rist25-Aug-06 6:13
Alain Rist25-Aug-06 6:13 
GeneralRe: Use "TTN_GETDISPINFO" to support multi-line tooltip Pin
shaohao25-Aug-06 22:00
shaohao25-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
Alain Rist26-Aug-06 6:43 
GeneralRe: Use TTN_GETDISPINFO to support multi-line tooltip Pin
Anton Lesnichenko30-Dec-07 5:50
Anton Lesnichenko30-Dec-07 5:50 
GeneralDoesn't work in DLLs Pin
MarkWoodard5-May-06 10:37
MarkWoodard5-May-06 10:37 
GeneralRe: Doesn't work in DLLs Pin
Alain Rist5-May-06 20:11
Alain Rist5-May-06 20:11 
QuestionGarbage values being displayed in tooltip Pin
CodeFundu2-Apr-06 20:17
CodeFundu2-Apr-06 20:17 
AnswerRe: Garbage values being displayed in tooltip Pin
Alain Rist4-Apr-06 11:55
Alain Rist4-Apr-06 11:55 
GeneralRe: Garbage values being displayed in tooltip Pin
CodeFundu4-Apr-06 12:01
CodeFundu4-Apr-06 12:01 

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

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