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



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 ‘


’ for displaying the Tool-Tip for any controls. In this article, we will extend this


 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 ‘


’. This class should derive from the ‘MFC’ Class ‘


’. 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:

4.2 TOOLINFO Structure & MFC’s TTM_ADDTOOL Message

MFC defined a structure called ‘


’ and we will populate this structure with all the required information for displaying the Tool-Tip. Once, the


structure is ready, we send ‘


’ message to the target window. The structure we populated goes as a parameter for


message. Below is the function:

Note, the ‘


’ member of the ‘


’ 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 ‘


’ member. In the ‘


’ member, we set the flags ‘


’ and ‘


’. The flag ‘


’ specifies that the ‘uId’ member of ‘


’ structure is holding the Window Handle of the Tool which will display the Tool-Tip. ‘


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

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:

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.

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? Please comment about it for others!!

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