Application of tree control
He Zhidan
Below is some applications of the tree control, since it is used for demonstration, the structure is unreasonable. The effect is as shown in the figure.
Proceed as follows:
1, Insert -> New class, insert a new class CMYCTRL, the base class is CTREECTRL.
2. Use the class wizard to add a WM_PANIT response function to CMYTREE.
code show as below:
Void CMYTREE :: onpaint ()
{
CPAINTDC DC (this);
CWnd :: DefWindowProc (WM_Paint, (WPARAM) dc.getsafehdc (), 0);
HTREEITEM HITEM = this-> getfirstvisibleItem ();
While (NULL! = HITEM)
{
UINT SELFLAG = TVIS_DROPHILITED | TVIS_SELECTED;
IF (GetItemState (Hitem, Selflag) & Selflag
{
HITEM = GetNextVisibleItem (HITEM);
CONTINUE;
}
CRECT R;
GetItemRect (HITEM, & R, TRUE);
{// Erase old content
ColorRef col = dc.getbkcolor ();
Cbrush brush (col);
CPEN PEN (PS_NULL, 1, COL);
CBRUSH * POLDBRUSH = DC.SelectObject (& brush);
CPEN * POLDPEN = DC.SelectObject (& Pen);
Dc.Rectangle (& R);
Dc.selectObject (Poldbrush);
Dc.selectObject (Poldpen);
}
CString strText = GetItemText (HITEM);
// Set color
Dc.SetBkcolor (getsyscolor (color_window));
Dc.SetTextColor (RGB (255, 0, 0));
/ / Change the font
Logfont logfont;
GetFont () -> getLogfont (& logfont);
Logfont.lfitalic = TRUE;
CFONT font;
Font.createFontIndirect (& logfont);
Dc.selectObject (& font);
Dc.TextOut (R.Left 2, R.Top 1, Strtext);
HITEM = GetNextVisibleItem (HITEM);
}
}
3. Create a dialog-based program, add a tree control, Ctrl W Open the class wizard, add an associated variable m_tree_ctrl to it.
4. Right click on the tree control, select the properties, and select the HAS Buttons, Has Lines, and Lines At root.
5, manually put ctreectrl m_tree_ctrl; change to cMytree M_tree_ctrl; don't forget to include header files,
#include "mytree.h"
6. Modify the oninitdialog of the dialog
BOOL CTESTREEDLG :: OnNitDialog ()
{
... ..
HtreeItem HrootItem = m_tree_ctrl.insertitem ("Warai!");
M_tree_ctrl.insertitem ("Guan Yu", HrootItem);
M_tree_ctrl.insertitem ("Zhang Fei", HrootItem);
M_tree_ctrl.insertitem ("Zhao Yun", HrootItem); M_Tree_ctrl.insertItem ("Ma Chao", HrootItem);
M_Tree_ctrl.insertitem ("Huang Zhong", HrootItem);
}
7, Ctrl F5. Effect is shown.
8. Add a bitmap in the resource manager, ID is IDB_STATE.
9. Add member variables in CMYTREE CIMAGELIST M_IMAGES;
10. Modify the response function of the CMYTree's PresubClassWindow.
Void CMYTREE :: PRESUBCLASSWINDOW ()
{
SetBkcolor (RGB (255, 255, 255);
M_Images.create (IDB_State, 18, 4, RGB (128, 0, 0));
SetimageList (& m_images, tvsil_normal);
Setwindowlong (M_HWND, GWL_STYLE, TVS_SHOWSELALWAYS | GetWindowlong (M_HWND, GWL_STYLE));
CTREECTRL :: PRESUBCLASSWINDOW ();
}
11, increase the response function of cmytree class = nm_click.
Void CMYTREE :: OnClick (nmhdr * pnmhdr, lresult * pRESULT)
{
DWORD DW = GetMessagePos ();
Cpoint PT (Loword (DW), HiWord (DW)); // Mouse screen coordinate
/ / Convert to the coordinates of opposing tree controls
Cpoint pt1 = pt;
CRECT RECT;
GetClientRect (Rect);
Clienttoscreen (& Re);
Pt1.x - = Rect.Left;
Pt1.y - = Rect.top;
// Judgment position in the tree control
TVHittestinfo thittestinfo;
THITTESTINFO.pt = Pt1;
Hittest (& thittestinfo);
IF (TVHT_ONITEMICON == THITTESTINFO.FLAGS)
{
Int Nimage, NSELECTIMAGE;
GetItemImage (THTTESTINFO.HITEM, NIMAGE, NSELECTIMAGE);
IF (0 == nimage)
SetItemImage (THITTESTINFO.HITEM, 3, 3);
ELSE IF (3 == Nimage)
SETITEMIMAGE (THITTESTINFO.HITEM, 0, 0);
}
* PRESULT = 0;
}
12. Change the dialog title to "Please select the array of arms".
13, BOOL CTESTREEDLG :: OnNitDialog ()
{
... ..
HtreeItem HITEM;
HITEM = M_Tree_ctrl.insertitem ("Guan Yu");
m_tree_ctrl.setItemImage (HITEM, 1, 1);
m_tree_ctrl.insertitem ("Zhang Fei");
m_tree_ctrl.insertitem ("Zhao Yun");
m_tree_ctrl.insertitem ("Ma Chao");
HITEM = m_tree_ctrl.insertitem ("Huang Zhong");
m_tree_ctrl.setitemimage (HITEM, 2, 2);
}
14. Cancel the HAS LINE of the tree control.
15. Replace a good icon for the dialog.
16, the result is as the most in front.