Create Tool Tip Using mfc ctooltipctrl

Tool-tips—those little yellow boxes that pop up with helpful text—add a touch of polish to your user interfaces. They guide users by providing context-sensitive information about buttons, text fields, and other controls. While MFC (Microsoft Foundation Classes) offers a built-in CToolTipCtrl class for tool-tips, let’s create a custom helper class to streamline their use and open up possibilities for future enhancements.

Understanding MFC Tool-Tips

Let’s refresh the basics:

  • Tool: A control within your MFC dialog (e.g., a button, edit box).
  • Tip: The descriptive text that appears in a yellow box when the user’s mouse hovers over the tool.
  • MFC’s CToolTipCtrl: The underlying MFC class that manages tracking mouse movement and displaying the tip.

Creating Our Helper Class

  1. New MFC Class (CToolTipCtrlExt)
    • Right-click your MFC project in Visual Studio and select “Add” -> “Class…”.
    • Name it CToolTipCtrlExt and make it derive from CToolTipCtrl.
  2. The Display_tooltip_text Function Inside CToolTipCtrlExt.h:
  3. C++
  4. void Display_tooltip_text(LPCTSTR display_text, CWnd* pWnd);

 

  1. Implementation (CToolTipCtrlExt.cpp)
  2. C++
  3. void CToolTipCtrlExt::Display_tooltip_text(LPCTSTR display_text, CWnd* pWnd)
  4. {
  5.     TOOLINFO ti;  
  6.     ti.cbSize = sizeof(TOOLINFO);
  7.     ti.lpszText = (LPSTR)display_text;  
  8.     ti.hwnd = pWnd->GetParent()->GetSafeHwnd(); // Parent of the control
  9.     ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
  10.     ti.uId = (UINT_PTR)pWnd->GetSafeHwnd(); // Handle of the tool
  11.     SendMessage(TTM_ADDTOOL, 0, (LPARAM)&ti);
  12. }

 

Using CToolTipCtrlExt in Your Dialog

  1. Include Header:
  2. C++
  3. #include “ToolTipCtrlExt.h” 

 

  1. Declare Member:
  2. C++
  3. CToolTipCtrlExt m_ctrl_tooltip_ext; 

 

  1. OnInitDialog Setup:
  2. C++
  3. BOOL CMyDialog::OnInitDialog() {
  4.     //  existing initialization 
  5.     m_ctrl_tooltip_ext.Create(this); // ‘this’ refers to your dialog 
  6.     m_ctrl_tooltip_ext.Display_tooltip_text(“Saves the Data”, &m_ctrl_btn_save);
  7.     //  add more tool-tips as needed 
  8.     return TRUE; // …  
  9. }

 

Let’s Test It! Run your project. Hover your mouse over the designated controls and see the tool-tips appear.

Customization Possibilities

Our CToolTipCtrlExt now encapsulates basic tool-tip setup. Think about enhancements:

  • Dynamic Text: Change tool-tip text based on user actions.
  • Styling:  Experiment with colors, borders, and even hyperlinks inside the tool-tip.