Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Tool Tip Class

4.95/5 (21 votes)
8 Oct 2007CPOL2 min read 1   5.2K  
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)