Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / MFC

A Float Tree Control like the Parameter List Control in Visual Studio

Rate me:
Please Sign up or sign in to vote.
3.44/5 (6 votes)
9 Apr 20021 min read 134.9K   2.6K   25   7
In Visual Studio, you will find an auto-completion list when you type your code in the IDE. This is a similar control but using a tree.

Sample Image - FloatTreeControl.gif

Introduction

When we are typing in Visual Studio, there is an auto-completion list for us to complete the parameter information for the function we typed or function names for a class. I encountered a requirement that the user should be able to select some words from a tree control to complete the current place in the editor. So I created this control.

How Does It Work?

The control is publicly derived from CTreeCtrl. You can create it using CreateTree with the size, parent window and optional bitmap icons. When you need to display the control, simply use the member function ShowMe with default parameters. When you double-click on a tree item, the window of the tree control will be closed and it will send a message to the parent window with selected string.

Prototypes

C++
class CXMLTreeCtrl : public CTreeCtrl
{
public:
    // Create a new tree 
    // rect: size of tree control will be displayed.
    // parent: the parent windows
    // image: resource code for an image as icons will be displayed in the 
    //        tree control
    BOOL CreateTree(CRect rect,CWnd *parent,UINT image=0);
    
    // Display the tree control, auto wrapped will encounter the edge of 
    // parent window
    // pt: The top-left point of window, may be changed when edge of parent 
    // window encounter
    // show: SW_SHOW for show current tree, SW_HIDE to hide the control
    void ShowMe(CPoint &pt,int show=SW_SHOW);
    
    // Set the image resource as a serial of icons in the control
    // resource , the image resource.
    void SetImages(UINT resource);
    
    // To load XML as the tree item to initialize the tree control
    // strPathName: the file name saves the data of tree in XML format
    // bOptimizeMemory: How to draw the tree's item.
    BOOL loadXML(const CString &strPathName, 
                 const BOOL bOptimizeMemory /*= FALSE*/);

    // The following code is borrowed from Frank Le for XML tree 
    // construction
protected:
    BOOL populateNode(MSXML::IXMLDOMElement* node, const HTREEITEM& hItem);
    BOOL populateAttributes(MSXML::IXMLDOMElement *node, const HTREEITEM &hParent);
    HTREEITEM insertItem(MSXML::IXMLDOMElement* node, 
                         const CString &nodeName, 
                         int nImage, int nSelectedImage, 
                         HTREEITEM hParent = TVI_ROOT, 
                         HTREEITEM hInsertAfter = TVI_LAST);
    
    void deleteFirstChild(const HTREEITEM& hItem);
    void deleteAllChildren(const HTREEITEM& hItem);
    int getIconIndex(MSXML::IXMLDOMElement* node);
    CImageList m_theImageList;
    BOOL m_bOptimizeMemory;
   
    // Generated message map functions
protected:
    //{{AFX_MSG(CXMLTreeCtrl)
    afx_msg void OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult);
    // User select a item , close the control and return what he/she selected.
    afx_msg void OnDblclk(NMHDR* pNMHDR, LRESULT* pResult);
    // If user point out the control when it is shown, close it
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    //}}AFX_MSG

How to Use It?

Using this code is very simple. First, declare a member variable as CXMLTreeCtrl and Create it.

C++
void CVs_treectrlView::OnInitialUpdate() 
{
    CView::OnInitialUpdate();
    CRect rect(0,0,200,300);
    m_Tree.CreateTree (rect,this,IDB_BITMAP_TREE_ICONS);
    m_Tree.loadXML("catalog.xml",TRUE);
}

Secondly, override the WM_LBUTTONUP or some other stimulus to display the tree control using ShowMe function.

C++
void CVs_treectrlView::OnLButtonUp(UINT nFlags, CPoint point) 
{
    m_Tree.ShowMe (point);
    CView::OnLButtonUp(nFlags, point);
}

Finally, deal the message WM_FLOAT_CTRL to do what you have selected from tree control.

C++
LRESULT CVs_treectrlView::OnFloatCtrl(WPARAM wParam, LPARAM lParam)
{
    CString str = (BSTR)wParam;
    MessageBox(str);
    m_Tree.ShowWindow (SW_HIDE);
    return 0;
}

That's all! Enjoy it.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Software Developer (Senior)
China China
I'm write program from 1990. My research field is CAG,CAD and Image processing. I select C/C++, ASP, Java, XML as my usaully developing tools. Occasional , write code in Delphi and VB. I'm using Visual C++ from 1996. If you have anything unclear, e-mail to :zhou_cn123@sina.com Software Engineering and CAD is my mainly research program.

You also can reach me on msn: zhoujohnson@hotmail.com

Comments and Discussions

 
GeneralImplementation made for XERCES Pin
Rainer Schuster24-Feb-06 1:44
Rainer Schuster24-Feb-06 1:44 
GeneralLooks like you copy some ideas from Frank Le Pin
Anonymous21-Apr-03 7:55
Anonymous21-Apr-03 7:55 
GeneralSorry that you put credit in header file Pin
Anonymous21-Apr-03 8:13
Anonymous21-Apr-03 8:13 
GeneralBroken Download Link Pin
Giles10-Apr-02 8:49
Giles10-Apr-02 8:49 
GeneralRe: Fixed Download Link Pin
Chris Maunder10-Apr-02 12:34
cofounderChris Maunder10-Apr-02 12:34 
GeneralRe: Fixed Download Link Pin
Giles11-Apr-02 11:48
Giles11-Apr-02 11:48 
GeneralRe: Fixed Download Link Pin
Chris Maunder11-Apr-02 14:15
cofounderChris Maunder11-Apr-02 14:15 
I keep tabs on the new articles, but with 2000 of the gosh darn things it's hard to watch them all Wink | ;)

cheers,
Chris Maunder

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.