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

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

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