vc treectrl

来源:互联网 发布:淘宝助手怎么创建宝贝 编辑:程序博客网 时间:2024/04/29 15:19
p164
// Ex040203Dlg.cpp : implementation file//#include "stdafx.h"#include "Ex040203.h"#include "Ex040203Dlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CEx040203Dlg dialogCEx040203Dlg::CEx040203Dlg(CWnd* pParent /*=NULL*/): CDialog(CEx040203Dlg::IDD, pParent){//{{AFX_DATA_INIT(CEx040203Dlg)//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CEx040203Dlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CEx040203Dlg)DDX_Control(pDX, IDC_TREE1, m_treeCtrl);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CEx040203Dlg, CDialog)//{{AFX_MSG_MAP(CEx040203Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, OnButton1)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CEx040203Dlg message handlersBOOL CEx040203Dlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog.  The framework does this automatically//  when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);// Set big iconSetIcon(m_hIcon, FALSE);// Set small iconHTREEITEM hItem;//记录节点位置hItem=m_treeCtrl.InsertItem("文学爱好");//插在根节点m_treeCtrl.InsertItem("古诗",hItem);//插在hItem节点m_treeCtrl.InsertItem("散文",hItem);//插在hItem节点hItem=m_treeCtrl.InsertItem("体育爱好");//插在根节点m_treeCtrl.InsertItem("乒乓球",hItem);//插在hItem节点m_treeCtrl.InsertItem("足球",hItem);//插在hItem节点// TODO: Add extra initialization herereturn TRUE;  // return TRUE  unless you set the focus to a control}void CEx040203Dlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}}// If you add a minimize button to your dialog, you will need the code below//  to draw the icon.  For MFC applications using the document/view model,//  this is automatically done for you by the framework.void CEx040203Dlg::OnPaint() {if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags//  the minimized window.HCURSOR CEx040203Dlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}void CEx040203Dlg::OnButton1() {// TODO: Add your control notification handler code here}void CEx040203Dlg::OnOK() {// TODO: Add extra validation hereCString strMess="你的爱好有\n------------";HTREEITEM hItem=m_treeCtrl.GetFirstVisibleItem();//取得tree的第一个节点while (NULL!=hItem)//遍历所有节点{if (m_treeCtrl.GetCheck(hItem))//获取节点选中状态{strMess+="\n";strMess+=m_treeCtrl.GetItemText(hItem);//取得本节点的文字}hItem=m_treeCtrl.GetNextVisibleItem(hItem);//取得下一个节点}AfxMessageBox(strMess);}

派生类class CCheckTreeCtrl : public CTreeCtrl,为此类添加了WM_LBUTTONDOWN和WM_KEYDOWN消息,使得子节点可以响应父节点的选中于取消选中
// CheckTreeCtrl.cpp : implementation file//#include "stdafx.h"#include "Ex040203.h"#include "CheckTreeCtrl.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CCheckTreeCtrlCCheckTreeCtrl::CCheckTreeCtrl(){}CCheckTreeCtrl::~CCheckTreeCtrl(){}BEGIN_MESSAGE_MAP(CCheckTreeCtrl, CTreeCtrl)//{{AFX_MSG_MAP(CCheckTreeCtrl)ON_WM_KEYDOWN()ON_WM_LBUTTONDOWN()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CCheckTreeCtrl message handlersvoid CCheckTreeCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {// TODO: Add your message handler code here and/or call defaultCTreeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);if (nChar==' ')//使空格键选中或取消{HTREEITEM hItem=GetSelectedItem();//取得当前选择的项目TravelChild(hItem);//取得当前选择的项目}}void CCheckTreeCtrl::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCTreeCtrl::OnLButtonDown(nFlags, point);HTREEITEM hItem =HitTest(point,NULL);//取得当前鼠标选择的项目TravelChild(hItem);}void CCheckTreeCtrl::TravelChild(HTREEITEM hItem){if (NULL==hItem) return;bool bCheck=GetCheck(hItem);//取得当前项的当前选择状态HTREEITEM hChildItem=GetChildItem(hItem);//取得当前项的子项while (NULL!=hChildItem){SetCheck(hChildItem,bCheck);//选中或不选TravelChild(hChildItem);//递归当前项hChildItem=GetNextSiblingItem(hChildItem);//取得下一项}}

1.添加一个treectrl控件,为其关联CtreeCtrl类型的变量(对象)m_treeCtrl
2.使用m_treeCtrl.InsertItem添加节点
3.
使用m_treeCtrl.GetFirstVisibleItem();取得第一个节点
使用m_treeCtrl.GetItemText(hItem);取得某个节点的text
使用m_treeCtrl.GetNextVisibleItem(hItem);取得下一个可视节点
使用m_treeCtrl.GetCheck(hItem)取得节点的状态等
4.添加派生类class CCheckTreeCtrl : public CTreeCtrl,为此类添加了WM_LBUTTONDOWN和WM_KEYDOWN消息,使得子节点可以响应父节点的选中于取消选中
5.将对象m_treeCtrl的类型改为CCheckTreeCtrl

CTreeCtrl Class Members

Construction
Attributes
Operations

Construction

CTreeCtrlConstructs a CTreeCtrl object.CreateCreates a tree view control and attaches it to a CTreeCtrl object.

Attributes

GetCountRetrieves the number of tree items associated with a tree view control.GetIndentRetrieves the offset (in pixels) of a tree view item from its parent.SetIndentSets the offset (in pixels) of a tree view item from its parent.GetImageListRetrieves the handle of the image list associated with a tree view control.SetImageListSets the handle of the image list associated with a tree view control.GetNextItemRetrieves the next tree view item that matches a specified relationship.ItemHasChildrenReturns nonzero if the specified item has child items.GetChildItemRetrieves the child of a specified tree view item.GetNextSiblingItemRetrieves the next sibling of the specified tree view item.GetPrevSiblingItemRetrieves the previous sibling of the specified tree view item.GetParentItemRetrieves the parent of the specified tree view item.GetFirstVisibleItemRetrieves the first visible item of the specified tree view item.GetNextVisibleItemRetrieves the next visible item of the specified tree view item.GetPrevVisibleItemRetrieves the previous visible item of the specified tree view item.GetSelectedItemRetrieves the currently selected tree view item.GetDropHilightItemRetrieves the target of a drag-and-drop operation.GetRootItemRetrieves the root of the specified tree view item.GetItemRetrieves the attributes of a specified tree view item.SetItemSets the attributes of a specified tree view item.GetItemStateReturns the state of an item.SetItemStateSets the state of an item.GetItemImageRetrieves the images associated with an item.SetItemImageAssociates images with an item.GetItemTextReturns the text of an item.SetItemTextSets the text of an item.GetItemDataReturns the 32-bit application-specific value associated with an item.SetItemDataSets the 32-bit application-specific value associated with an item.GetItemRectRetrieves the bounding rectangle of a tree view item.GetEditControlRetrieves the handle of the edit control used to edit the specified tree view item.GetVisibleCountRetrieves the number of visible tree items associated with a tree view control.GetToolTipsRetrieves the handle to the child ToolTip control used by a tree view control.SetToolTipsSets a tree view control's child ToolTip control.GetBkColorRetrieves the current background color of the control.SetBkColorSets the background color of the control.GetItemHeightRetrieves the current height of the tree view items.SetItemHeightSets the height of the tree view items.GetTextColorRetrieves the current text color of the control.SetTextColorSets the text color of the control.SetInsertMarkSets the insertion mark in a tree view control.GetCheckRetrieves the check state of a tree control item.SetCheckSets the check state of a tree control item.GetInsertMarkColorRetrieves the color used to draw the insertion mark for the tree view.SetInsertMarkColorSets the color used to draw the insertion mark for the tree view.

Operations

InsertItemInserts a new item in a tree view control.DeleteItemDeletes a new item in a tree view control.DeleteAllItemsDeletes all items in a tree view control.ExpandExpands, or collapses, the child items of the specified tree view item.SelectSelects, scrolls into view, or redraws a specified tree view item.SelectItemSelects a specified tree view item.SelectDropTargetRedraws the tree item as the target of a drag-and-drop operation.SelectSetFirstVisibleSelects a specified tree view item as the first visible item.EditLabelEdits a specified tree view item in-place.HitTestReturns the current position of the cursor related to the CTreeCtrl object.CreateDragImageCreates a dragging bitmap for the specified tree view item.SortChildrenSorts the children of a given parent item.EnsureVisibleEnsures that a tree view item is visible in its tree view control.SortChildrenCBSorts the children of a given parent item using an application-defined sort function.
http://download.csdn.net/detail/luck_good/3787056
原创粉丝点击