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++)
    {
     strInt.Format("%d",k);
     strTemp+= strTemp+strInt;
     m_TreeCtrl.InsertItem(strTemp);
    }
    //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
    EC4B__INCLUDED_)
    #define AFX_CYOURDIALOGCLASS_H__2D5F1155_D7EC_4252_BEFA_2CA98929EC4B_
    _INCLUDED_
    
    #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
    public:
      CYOURDialogClass(CWnd* pParent = NULL);  // standard constructor
    
    // Dialog Data
      //{{AFX_DATA(CYOURDialogClass)
      enum { IDD = IDD_CUSTOMTOOLTIPTREE_DIALOG };
      //CTreeCtrl  m_TreeCtrl;
    
      CPCTreeCtrl  m_TreeCtrl; //Create an Instance of 
                               //our tree Control., MEGA+
      
      //}}AFX_DATA
    
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CYOURDialogClass)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
      //}}AFX_VIRTUAL
    
    // Implementation
    protected:
      HICON m_hIcon;
    
      // Generated message map functions
      //{{AFX_MSG(CYOURDialogClass)
      virtual BOOL OnInitDialog();
      afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
    };
    
    //{{AFX_INSERT_LOCATION}}
    // 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.

Downloads

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


Comments

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

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

Top White Papers and Webcasts

  • Data center, IT and Operations Architects can now secure their web applications whether they are on-premise, in a virtual environment or in the most popular public cloud, Amazon Web Services (AWS). This blueprint document provides guidance on architecting security for cloud-based web applications.

  • Are you truly leading your team or simply managing them? Organizations need leaders and your team needs someone to follow. With some ongoing development, you could become that leader. Learn the top leadership qualities that inspire others to want to follow you and the direction of your company.

Most Popular Programming Stories

More for Developers

RSS Feeds

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