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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds