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:
gToolTip::AddTip(hWndDlg,hInst,tip,IDC_EDIT1,TRUE);
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:
INITCOMMONCONTROLSEX icc;
HWND hwndTip;
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC = ICC_TAB_CLASSES ; InitCommonControlsEx(&icc);
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, 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);
ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
ti.uId =(UINT)GetDlgItem(hWnd,id);
ti.hwnd = hWnd;
ti.hinst = hInst;
ti.lpszText = Tip;
SendMessage(hwndTip,TTM_ACTIVATE,TRUE,0);
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
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; nfd.cbSize = sizeof(NOTIFYICONDATA);
nfd.hWnd = hWndDlg; wcscpy(nfd.szTip,L"Tip.. " ); wcscpy(nfd. szInfoTitle, _T(" szInfoTitle.")); wcscpy(nfd.szInfo, _T("szInfo")); nfd.uTimeout = TimeOut*1000;
nfd.uFlags = NIF_ICON | NIF_TIP | NIF_INFO;
nfd.dwInfoFlags =NIIF_INFO; nfd.hIcon = hIcon; if( !Shell_NotifyIcon(NIM_ADD,&nfd)) 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