Click here to Skip to main content
Click here to Skip to main content

Tool Tip Class

, 8 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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:

//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:

//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:

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:

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:

#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.

    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)

Share

About the Author

GauranG Shah
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

 
GeneralMy vote of 5 Pinmemberzhanglipeng19-Dec-12 15:54 
GeneralMy vote of 5 PinmemberMichael Haephrati מיכאל האפרתי20-Oct-12 5:09 
GeneralMy vote of 5 PinmemberRich In Soquel13-Jan-11 7:40 
QuestionPlease show me some code to show a tooltip with multi-lines! Pinmemberfantasy121529-Jul-08 0:16 
GeneralThis is a good job. Pinmemberkalinahugova8-Oct-07 13:21 
GeneralRe: This is a good job. PinmemberThatsAlok17-Oct-07 2:40 
GeneralInternals of the class PinmemberAli Rafiee5-Oct-07 12:06 
GeneralRe: Internals of the class PinmemberGauranG338-Oct-07 2:55 
Questionscreenshot anywhere ? Pinmvptoxcct3-Oct-07 4:07 
AnswerRe: screenshot anywhere ? PinmemberGauranG333-Oct-07 4:55 
GeneralRe: screenshot anywhere ? Pinmvptoxcct3-Oct-07 5:14 
GeneralRe: screenshot anywhere ? PinmemberGauranG333-Oct-07 19:27 

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
Web03 | 2.8.141216.1 | Last Updated 8 Oct 2007
Article Copyright 2007 by GauranG Shah
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid