Since coming to our forum, I have known many friends and programming experts and learned a lot from them. I have seen many people asking questions about tree control in the Forum. Therefore, we will write down some questions about tree control for your reference. It will also serve as a summary of the Learning in the recent period.
This article describes how to add a controlbar to multiple documents and how to add a tree control method to controlbar. This is also a commonly used interface in software systems, hoping to help you.
Running result of this example
First, create a multi-document project, copy sizecbar. cpp and sizecbar. H to the file add of the project, and add the new file to the project. In this way, a new class csizingcontrolbar is added to the project. Then, use Class Wizard to generate a new class mybar Based on ctoolbar. Change the base class to csizingcontrolbar.
Add onupdatecmdui (cframewnd * pTARGET, bool bdisableifnohndler) to the mybar class)
The Code is as follows:
Void mybar: onupdatecmdui (cframewnd * pTARGET, bool bdisableifnohndler)
{
Updatedialogcontrols (pTARGET, bdisableifnohndler );
}
Then add the member variable m_ctrlbar of mybar to cmainframe, and add the following code to the oncreate function of cmainframe:
If (! M_ctrlbar.create ("data browsing bar", this, csize (200,200), true, 123 ))
{
Trace0 ("failed to create mybar/N ");
Return-1;
// Fail to create
}
M_ctrlbar.setbarstyle (m_ctrlbar.getbarstyle () |
Cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic );
M_ctrlbar.enabledocking (cbrs_align_any );
Enabledocking (cbrs_align_any );
Dockcontrolbar (& m_ctrlbar, afx_idw_dockbar_left );
Run the program and you will see that mybar has been added to the project. A Tree View control is added to mybar.
Create a new class mytree Based on ctreectrl. Then add the member variable mytree m_treectrl to mybar;
Reload the message function wm_create from mybar. Add the following code.
M_treectrl.create (ws_visible | ws_tabstop | ws_child | ws_border
| Tvs_hasbuttons | tvs_linesatroot | tvs_haslines
| Tvs_disabledragdrop | tvs_editlabels | tvs_trackselect,
Crect (10, 10,300,100), this, id_treectrl );
// M_treectrl.setimagelist (& m_images, tvsil_normal );
// Set "normal" Gui-font
Cfont * font = cfont: fromhandle (hfont): getstockobject (default_gui_font ));
M_treectrl.setfont (font );
Htreeitem root = m_treectrl.insertitem ("vckbase", 0, 0 );
Htreeitem subroot1 = m_treectrl.insertitem ("Programmer community", 1, 1, root );
Htreeitem subroot2 = m_treectrl.insertitem ("document center", 1, 1, root );
Htreeitem subroot3 = m_treectrl.insertitem ("code repository", 1, 1, root );
Htreeitem subroot4 = m_treectrl.insertitem ("tools and resources", 1, 1, root );
Htreeitem subroot5 = m_treectrl.insertitem ("customer service center", 1, 1, root );
Htreeitem subroot6 = m_treectrl.insertitem ("personal column", root );
M_treectrl.insertitem ("Development Forum", 1, 1, subroot1 );
M_treectrl.insertitem ("Development Alliance", 1, 1, subroot1 );
M_treectrl.insertitem ("Talent Exchange", 1, 1, subroot1 );
M_treectrl.insertitem ("User Registration", 1, 1, subroot1 );
M_treectrl.insertitem ("vcity", 1, 1, subroot1 );
M_treectrl.insertitem ("online magazine", 1, 1, subroot2 );
M_treectrl.insertitem ("personal album", subroot2 );
M_treectrl.insertitem ("Forum essence", subroot2 );
M_treectrl.insertitem ("Cool Database", subroot2 );
M_treectrl.insertitem ("all articles", 1, 1, subroot2 );
M_treectrl.insertitem ("all codes", 1, 1, subroot3 );
M_treectrl.insertitem ("toolkit", subroot4 );
M_treectrl.insertitem ("toolkit", subroot5 );
M_treectrl.insertitem ("User Registration", 1, 1, subroot5 );
M_treectrl.insertitem ("Modify information", 1, 1, subroot5 );
M_treectrl.insertitem ("retrieve password", 1, 1, subroot5 );
M_treectrl.insertitem ("answers", subroot5 );
M_treectrl.insertitem ("contribution", subroot5 );
M_treectrl.insertitem ("Contact Us", 1, 1, subroot5 );
M_treectrl.selectitem (Root );
M_treectrl.expand (root, tve_expand );
Load the wm_size message function of mybar and add the following code to it:
Crect RC;
Getclientrect (& rc );
M_treectrl.movewindow (& rc );
In cctreecontrolbarview, add the member variable m_title and add the member function drawrect (CDC * PDC );
The Code is as follows:
Void cctreecontrolbarview: drawrect (CDC * PDC)
{
Crect rect;
PDC-> rectangle (100,100,400,400 );
Rect. Top = 100;
Rect. Left = 100;
Rect. Rights = 250;
Rect. bottomtom = 250;
PDC-> drawtext (m_title, rect, dt_center );
}
Call this function in the ondraw function;
Then add the pointer of the View class to the application class, pview; reload the oninitupdate () function in the view, where pview is initialized. The Code is as follows:
Void cctreecontrolbarview: oninitialupdate ()
{
Cview: oninitialupdate ();
// Todo: add your specialized code here and/or call the base class
Cctreecontrolbarapp * PAPP = (cctreecontrolbarapp *) afxgetapp ();
PAPP-> pview = this;
}
In the mytree class, add the left-click function onlbuttondown (). In the function, click the node in the tree control column to obtain the text title of the corresponding node. The Code is as follows:
Htreeitem hitem = hittest (point, & m_uflags );
// Perform operations and the result is 1 if the binary number is 1.
If (m_uflags & tvht_onitemlabel ))
{
Cctreecontrolbarapp * PAPP = (cctreecontrolbarapp *) afxgetapp ();
PAPP-> pview-> m_title = getitemtext (hitem );;
PAPP-> pview-> invalidate (true );
Selectitem (hitem );
}
The project is designed here. After running the project, you can click the node to display the text annotation of the corresponding node in the view.