Custom ToolTip for Tree Control

Environment:VC6 SP4, NT4 SP3

MFC Tree controls can be put in resizable dialog boxes. However, ToolTips don't show up for items that are not fully visible when the dialog is shrunk and only a part of the Tree control is displayed. This is because ToolTips are constructed for items whose text length exceeds the visible client area of the Tree control when the control was initialized. This article shows you how to create a tree control that shows ToolTips of all its items, no matter what. There are files called PCTreeCtrl.h and PCTreeCtrl.cpp that contain the source for this; a sample project using this new Tree Control class is also included.

How to Use This Control

Follow these steps to create your application.

  1. Create a MFC dialog-based application.
  2. Drag and drop a Tree control in the dialog.
  3. A Default ID for the TreeCtrl, called IDC_TREE1, is generated.
  4. Add a member variable for this control using the class wizard; for example, m_TreeCtrl.
  5. Be sure to turn off the MFC TreeCtrl's ToolTip checkbox in the resouce editor.
  6. Also be sure that you set the Dialog border as resizing in the Dialog properties window opened in the resource editor.
  7. Now double-click the dialog. This takes you to the Dialog's Implementation file.
  8. In the BOOL CYOURDialogClass::OnInitDialog() function definition, add the following lines of code.
  9. // TODO: Add extra initialization here
    //MEGA+ - BEGIN
    //Now add the lengthy Items in the Tree Control..
    CString strTemp,strInt;
    strTemp = "Test";
    for(int k =0;k<10; k++)
     strTemp+= strTemp+strInt;
    //MEGA+ - END

  10. Now go to your dialog's header file and add the following line:
    #include "PCTreeCtrl.h" //Add the Tree Control Header File., MEGA+
    Comment out the following line:
    CTreeCtrl  m_TreeCtrl;
    And beneath that add the following line:
    CPCTreeCtrl  m_TreeCtrl; //Create an Instance of 
                             //our tree Control., MEGA+
    Your resultant header file may look something like this.
    //////////// CYOURDialogClass.h : header file - BEGIN ///////////////
    #if !defined(AFX_CYOURDIALOGCLASS_H__2D5F1155_D7EC_4252_BEFA_2CA98929
    #define AFX_CYOURDIALOGCLASS_H__2D5F1155_D7EC_4252_BEFA_2CA98929EC4B_
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    #include "PCTreeCtrl.h" //Add the Tree Control Header File., MEGA+
    // CYOURDialogClass dialog
    class CYOURDialogClass : public CDialog
    // Construction
      CYOURDialogClass(CWnd* pParent = NULL);  // standard constructor
    // Dialog Data
      //CTreeCtrl  m_TreeCtrl;
      CPCTreeCtrl  m_TreeCtrl; //Create an Instance of 
                               //our tree Control., MEGA+
      // ClassWizard generated virtual function overrides
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    // Implementation
      HICON m_hIcon;
      // Generated message map functions
      virtual BOOL OnInitDialog();
      afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
    // Microsoft Visual C++ will insert additional declarations 
    // immediately before the previous line.
    #endif // !defined(AFX_CYOURDIALOGCLASS_H__2D5F1155_D7EC_4252_BEFA_
           // 2CA98929EC4B__INCLUDED_)
    ///////////// CYOURDialogClass.h : header file - END  ////////////
  11. Now resize the dialog so that only a part of Tree control is visible. Move the cursor over any partially visible text. You should be able to see the ToolTips for all the items irrespective of text size.


Download demo project -- 32 KB
Download source -- 3 KB


  • Good, but...

    Posted by Legacy on 02/08/2003 12:00am

    Originally posted by: Leon Ren

    It's wonderful! However, I think you should explain more about how your classes and functions work, not only told us how to make an example.
    Best Regards.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • Power and Automation Equipment makers face constrained CAPEX, equipment interoperability issues, and process inefficiencies. However, they can take advantage of secure Internet of Things (IoT) intelligent devices and systems to make better business decisions around asset utilization and performance, while simultaneously optimizing equipment interoperability and services efficiency. Watch this brief video to see how you can unlock the full potential of your assets and value chain.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date