Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C++

Tool Tip Class

Rate me:
Please Sign up or sign in to vote.
4.95/5 (21 votes)
8 Oct 2007CPOL2 min read 83.5K   5.2K   44   15
Simple Tool Tip class that will help to show the Balloon and Simple tool tip for controls and the in System Tray window.

Introduction

Recently I was working on a project that was based on win32 dialog based application. I needed to provide a tooltip for that. I searched a lot for that on CodeProject and other sites. But I only got the help for MFC applications. So I made my own class to use tool tip easily. And I think it's a good idea to put things on CodeProject site so that others can also use it. I have made this classes to help in displaying the tooltip for controls. It supports both simple and Balloon tooltip. Below is the screenshot of the demo application.

Image 1

Using the Code

I have made simple class gToolTip class which contains all the code to display the tooltip for the control you want. To use the classes in your application, just include gToolTip.cpp and gToolTip.h in your project. Then simply call the AddTip function with the arguments and tooltip will be displayed for the control.

Here is the code that shows how to call the AddTip function. To assign the tool tip to any control:

C++
//AddTip( HWND,HINSTANCE,TCHAR*,UINT,BOOL = FALSE);

 //HwndDlg = Handle of the Window(in my case it's a handle to Dialog)
 //hInst   = Instance of the Window.
 //tip     = The Tool tip you wish to have on you control
 //UINT    = ID of the Control for which you want the tool tip to be displayed
 //TRUE    = Balloon Tool tip will be displayed
  
 //this will show balloon tool tip for the IDC_EDIT1 Control
 gToolTip::AddTip(hWndDlg,hInst,tip,IDC_EDIT1,TRUE);

 //This will show the normal Tool tip for the IDC_EDIT1
 gToolTip::AddTip(hWndDlg,hInst,tip,IDC_EDIT1);

Inside the gTooltip Class

Tooltip for Controls

Tooltip is a common control defined in the Commctrl.h file. So include this header file and comctl32.lib in your project.

Now before you use any of the common controls, you need to initialize them. Below is the line of the code that will initialize the Tooltip Common Control:

C++
//You need to initialize the structure before you use any of the common controls
INITCOMMONCONTROLSEX icc;       
HWND hwndTip;
icc.dwSize =    sizeof(INITCOMMONCONTROLSEX);
icc.dwICC =     ICC_TAB_CLASSES ;        	//Load tab and ToolTip control classes
InitCommonControlsEx(&icc);          	//Will initialize the common control

Ok. Here you have initialized the Tooltip control. Now you need to make the window for tooltip whose class is Tooltip class. Below is the code that will show how to do it:

C++
hwndTip =CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
                        WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP |TTS_BALLOON,
                      CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
                         hWnd, 	//Handle of the Parent window.
				//(i.e. Window for which you want Tooltip)
                         NULL,
                         hInst, 
                         NULL);

If you want a simple tool tip, do not use the TTS_BALLON style in the above code.

Now just initialize the Tool information (TOOLINFO) structure and add the tooltip. Below is the code that will show how to do this:

C++
TOOLINFO        ti;

ti.cbSize = sizeof(TOOLINFO);
//Flags TTF_IDISHWND show that the uID is the handle to control
ti.uFlags =  TTF_IDISHWND | TTF_SUBCLASS;  

//Handle of the Control for which you want to Tooltip to be displayed
ti.uId    =(UINT)GetDlgItem(hWnd,id);

//Handle of the parent window (The window in which the Control resides)
ti.hwnd   = hWnd;
ti.hinst  = hInst;
ti.lpszText  = Tip;         //Text you want as a Tooltip
	//Below is the Rectangle for ToolTip. But It will be ignored 
	//If you use TTF_IDISHWND in Flag
//ti.rect.left = ti.rect.top = ti.rect.bottom = ti.rect.right = 0; 

//Will Activate the tooltip window
SendMessage(hwndTip,TTM_ACTIVATE,TRUE,0);  

//will Add the Tooltip Window
if(!SendMessage(hwndTip,TTM_ADDTOOL,0,(LPARAM)&ti)){
    MessageBox(NULL,L"Couldn't create the ToolTipcontrol.",L"Error",MB_OK);
    }

Ballloon Tooltip for Systemtray Icon

Screenshot - Taskbar_ToolTip.gif

The Balloon tool ip in the Taskbar Icon is supported by Shell32.dll versions 5.0 and later. So to activate the Balloon tooltip member in NOTIFYICONDATA structure, include the following in your structure:

C++
#define _WIN32_IE 0x0500  // I have used this in my code
#define _WIN32_IE 0x0600

Now below is the line of code that describes how to put the application in the taskbar and show balloon Tooltip.

C++
NOTIFYICONDATA nfd;                   //Structure that you need to initialize
nfd.cbSize = sizeof(NOTIFYICONDATA);
nfd.hWnd    =    hWndDlg;                   //Handle of the Window
wcscpy(nfd.szTip,L"Tip.. " );    //Normal Tool Tip you want to display
wcscpy(nfd. szInfoTitle, _T(" szInfoTitle."));      //Will display Title for
                    //Tooltip as show in Image
wcscpy(nfd.szInfo, _T("szInfo"));//The Text will appear below Title in
            //Balloon Tool Tip
//Time for which the Balloon should appear.The Value is in milliseconds.
//1000*10 < uTimeout < 1000*30. Only this values will work
 nfd.uTimeout = TimeOut*1000;

// I haven't used the below things.But if you want write down
// the User Message you want to handle.
//nfd.uCallbackMessage

nfd.uFlags =    NIF_ICON | NIF_TIP | NIF_INFO;
nfd.dwInfoFlags =NIIF_INFO;                 //Icon you want for your balloon
nfd.hIcon  = hIcon;                         //Icon that you want to show
                //in System Tray
if( !Shell_NotifyIcon(NIM_ADD,&nfd))        //Will add the Icon in System Tray
    MessageBox(NULL,L"Not able to add Icon in system Tray",L"Error" ,0);

History

  • 03 October 2007: First submitted article
  • 08 October 2007: Updated to described the gTooltip class

License

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


Written By
Tester / Quality Assurance
India India
His name is Gaurang Shah. He has completed his bachelor in computer science.Has worked as a System programmer. Currently working as QA Analyst in Pune.

Comments and Discussions

 
QuestionNot displaye Tooltip TaskbarIcon on Table mode window 7! Pin
elitemod22-Dec-16 19:38
elitemod22-Dec-16 19:38 
QuestionCouldn't create the tooltip control Pin
Member 115789928-Apr-15 3:01
Member 115789928-Apr-15 3:01 
AnswerRe: Couldn't create the tooltip control Pin
Tarmo Pikaro1-Feb-18 3:31
Tarmo Pikaro1-Feb-18 3:31 
GeneralMy vote of 5 Pin
zhanglipeng19-Dec-12 14:54
zhanglipeng19-Dec-12 14:54 
GeneralMy vote of 5 Pin
Michael Haephrati20-Oct-12 4:09
professionalMichael Haephrati20-Oct-12 4:09 
GeneralMy vote of 5 Pin
Rich In Soquel13-Jan-11 6:40
Rich In Soquel13-Jan-11 6:40 
QuestionPlease show me some code to show a tooltip with multi-lines! Pin
fantasy121528-Jul-08 23:16
fantasy121528-Jul-08 23:16 
GeneralThis is a good job. Pin
kalinahugova8-Oct-07 12:21
kalinahugova8-Oct-07 12:21 
GeneralRe: This is a good job. Pin
ThatsAlok17-Oct-07 1:40
ThatsAlok17-Oct-07 1:40 
GeneralInternals of the class Pin
Ali Rafiee5-Oct-07 11:06
Ali Rafiee5-Oct-07 11:06 
It would also be nice if you would explain a little bit about the internals of you code. How does it do what it does?

AliR.
Visual C++ MVP

GeneralRe: Internals of the class Pin
GauranG Shah8-Oct-07 1:55
GauranG Shah8-Oct-07 1:55 
Questionscreenshot anywhere ? Pin
toxcct3-Oct-07 3:07
toxcct3-Oct-07 3:07 
AnswerRe: screenshot anywhere ? Pin
GauranG Shah3-Oct-07 3:55
GauranG Shah3-Oct-07 3:55 
GeneralRe: screenshot anywhere ? Pin
toxcct3-Oct-07 4:14
toxcct3-Oct-07 4:14 
GeneralRe: screenshot anywhere ? Pin
GauranG Shah3-Oct-07 18:27
GauranG Shah3-Oct-07 18:27 

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.