Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Create Tool-Tip Using MFC CToolTipCtrl

1. Introduction to MFC’s CToolTipCtrl

A ‘Tool-Tip’ is a flashing window that shows helpful information about the control. Microsoft Foundation Class (MFC) will display it when a cursor is standing on a control for few seconds. In this article, we will see how to create a reusable helper class for displaying the Tool-tip. We can use this helper class for any MFC dialog controls. We will derive this helper class from MFC’s CToolTipCtrl.

2. About CToolTipCtrl Example

The below screen-shot shows the example which we will create to learn how to display the Tool-Tip. In the following section we will create a helper class which one can use across any MFC projects.

MFC Tool-Tip Example - Defines a custom class derived from CToolTipCtrl

MFC Tool-Tip Example – Defines a custom class derived from CToolTipCtrl

In our example, the dialog is the parent for the Tool. A ‘Tool’ is nothing but the controls like a push button, edit box, combo box, etc. A ‘Tip’ is a yellow rectangle which shows a descriptive message about the Tool or we can say a control.

In the above screen-shot, MFC displays the Tip Text for the Tool when the mouse cursor enters the Tool and stays there for few seconds. This means, MFC Tool-Tip control should track the mouse movement and display Tip Text when the mouse move stops inside the control for few seconds.

MFC Framework provides a class called ‘CToolTipCtrl’ for displaying the Tool-Tip for any controls. In this article, we will extend this CToolTipCtrl class, to make the display of Tool-Tip with a single simple function call. OK, let us start.

3. Extending CToolTipCtrl of MFC

First, we need to create a dialog-based application by accepting all the defaults and name the application as ‘TooltipExp’. Once the application is created, we will add a class to the project and name the class as ‘CToolTipCtrlExt’. This class should derive from the ‘MFC’ Class ‘CToolTipCtrl’. In this example, we will display the standard Tool-Tip, using the derived class. However, one can add more functionality to it. Say, for example, setting a blue border around the Tip-Text or displaying a hyper-link within the Tool-Tip etc.,

To see how this new class added to the project, have a look at the below video:

Video: Extending MFC CToolTipCtrl (No Audio)

After adding the class using Visual Studio Interactive Development Environment (IDE), we lay out the dialog for this example with two text boxes and one push button. First, we take out the default OK and Cancel button from it. When the dialog is ready, we add control variables to the dialog class. The below video shows how to add control variables for this example:

Adding Control Variables (No Audio)

4. Code Explanation for CToolTipCtrlExt

4.1 Tool-Tip Custom Function & Parameter

First, we declare a public function which takes two parameters. The first parameter is the Tool-Tip text and the second parameter is a window which displays the Tool-Tip. Below is the code for the declaration:

//Sample 01: Declare a function to display tool tip 
//text for a control
void Display_tooltip_text(LPCTSTR display_text, 
    CWnd* pWnd);

4.2 TOOLINFO Structure & MFC’s TTM_ADDTOOL Message

MFC defined a structure called ‘TOOLINFO’ and we will populate this structure with all the required information for displaying the Tool-Tip. Once, the TOOLINFO structure is ready, we send ‘TTM_ADDTOOL’ message to the target window. The structure we populated goes as a parameter for TTM_ADDTOOL message. Below is the function:

//Sample 02: Display the tooltip text for a given window
void CToolTipCtrlExt::Display_tooltip_text(
    LPCTSTR display_text, CWnd* pWnd)
{
    //2.1: Declare the Structure
    TOOLINFO ti;
 
    //2.2: Specify the tooltip text and window handle 
    //to the tool that requires tooltip
    ti.cbSize = sizeof(TOOLINFO);
    ti.lpszText = (LPSTR) display_text;
    ti.hwnd = pWnd->GetParent()->GetSafeHwnd();
    ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    ti.uId = (unsigned int) pWnd->GetSafeHwnd();
 
    //2.3: Send a window message to the control's 
    //window procedure (CToolTipCtrl will take care)
    SendMessage(TTM_ADDTOOL, 0, (LPARAM) &ti);
}

Note, the ‘hwnd’ member of the ‘TOOLINFO’ is a Window Handle of the parent of the Tool Window. In our example, the dialog class is the parent for all Tool Windows like Text Boxes, Command Button, etc. We store child Window Handle in the ‘uId’ member. In the ‘uFlags’ member, we set the flags ‘TTF_IDISHWND’ and ‘TTF_SUBCLASS’. The flag ‘TTF_IDISHWND’ specifies that the ‘uId’ member of ‘TOOLINFO’ structure is holding the Window Handle of the Tool which will display the Tool-Tip. ‘TTF_SUBCLASS’ flag requests the MFC framework to subclass the window. The sub-classed window will track the mouse enter and leave and displays the Tool-Tip when there is no mouse movement in it.

5. Adding Tool-Tip to Dialog Controls

5.1 Include the Header File

Our derived class was ready in the previous section. Here, we will use it for our dialog controls. First, we include the header file of the extended control in our dialog class. Actually, In the extended class, we did not extend the basic Tool-Tip functionality of MFC. But, we introduced a helper function that makes life easier.

//Sample 03: Include the extended tooltip
#include "TooltipCtrlExt.h"

5.2 Declare Extended Control

After including the header file of our helper class, we declare a public data member of the extended control class in the dialog class. This is shown below:

//Sample 04: Declare a member of our extended 
//tooltip control
CToolTipCtrlExt m_ctrl_tooltip_ext;

5.3 Hook Tool-Tip Text With Dialog Control

In the ‘OnInitDialog’ dialog initialization function, first we set up our Tool-Tip control object and pass our dialog as a parameter. Next, we call the ‘Display_tooltip_text()’ function to register the Tool-Tip text with the dialog controls.

// TODO: Add extra initialization here
//Sample 05: Register the controls for tooltip display
m_ctrl_tooltip_ext.Create(this);
m_ctrl_tooltip_ext.Display_tooltip_text(
    "Saves the Data", &m_ctrl_btn_save);
m_ctrl_tooltip_ext.Display_tooltip_text(
    "First Name of the Person", &m_ctrl_edit_fname);
m_ctrl_tooltip_ext.Display_tooltip_text(
    "Last Name of the Person", &m_ctrl_edit_lname);

Now we can run the application and place the mouse cursor over the text boxes or on the button control. This will show the tool tip near the control. As we extended the MFC class, one can add additional functionality to the class to improve the Tool-Tip display. Say, for example, alternating between two tool-tip texts when the mouse pointer stays on the control, providing a border around the Tool-Tip text, etc.

Source Code : Download MFC Tool-Tip Example from Google Drive

Categories: MFC

Tags: , , ,

Do you like this Example? Share your thoughts!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.