《VC中CTreeView控件的使用(6页).doc》由会员分享,可在线阅读,更多相关《VC中CTreeView控件的使用(6页).doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-VC中CTreeView控件的使用-第 6 页VC中CTreeView控件的使用1.清空TreeView里的所有内容,并重新填充刚开始的时候,能用DeleteAllItems()方法把节点都给删除,但是留下了一片空白,重新添加的内容就看不到了。这个问题郁闷了很久,但思想无非是一样的,删除节点,重新填充。偶然间发现别人的代码里用到了SetRedraw()。哈,问题解决。HTREEITEM hSelectedItem=m_TreeCtrl.GetSelectedItem();CString strSelectedItemText=m_TreeCtrl.GetItemText(hSelectedI
2、tem);HTREEITEM hParentItem=m_TreeCtrl.GetParentItem(hSelectedItem);TV_ITEM tvItem;_variant_t vClass;vClass= m_pRecordset-GetCollect(名称);tvItem.mask=TVIF_TEXT|TVIF_PARAM;CString temp;temp=(char*)_bstr_t(vClass);tvItem.pszText=(LPTSTR)(LPCTSTR)temp;TV_INSERTSTRUCT tvInsert;tvItem.cchTextMax=2;tvInsert
3、.hParent=TVI_ROOT;/当作根节点tvInsert.hInsertAfter=TVI_LAST;tvInsert.item=tvItem;HTREEITEM hRoot=m_TreeCtrl.InsertItem(&tvInsert);这段代码从数据库中读取“名称”字段来填充控件。最值得注意的就是红色的那句话,这里吧CString的字符串添加到TreeView 中需要进行复杂的转化,CSDN中有篇文章详细地讲述了CString 的操作。地址是:这个是英文的这个是中文的对于上面操作的解释:当你向控件写数据时,文本指针实际上被当成 LPCTSTR,而当你从控件读数据 时,你必须有一个
4、可写的字符串。这个结构无法区分它是用来读还是用来写。因此,你会常常在我的代码中看到如下的用法:tvi.item.pszText = (LPTSTR)(LPCTSTR)s;它把 CString 强制类型转化成 LPCTSTR,也就是说先获得改字符串的地址,然后再强制类型转化成 LPTSTR,以便可以对之进行赋值操作。 注意这只有在使用 Set 或 Insert 之类的方法才有效!如果你试图获取数据,则不能这么做。本文来自CSDN博客,转载请标明出处:CTreeViewExt是MFC使用者可重用的类,该类由CTreeView派生,可以用在文档-视结构的应用程序中,并且支持Drag&Drop。使用者
5、所需要做的仅仅是将文件加到AppWizard产生的工程中,(CTreeView为基类)并且将CTreeView替换为CTreeViewExt。并重载三个Virtual Function。(WenYY:这很简单吧,下面是源代码,我会在必要的地方加上注释,但由于原作者并未加,所以如果有出入请原谅,作者定义了三处虚拟函数,其作用是让使用者重载后,加入自己的判断条件和结果处理的功能,很巧妙的思想: CopyItemProperties IsItemCanBeDroppedOn(HTREEITEM hSource, HTREEITEM hTarget); BOOL ItemCanBeDragged(HT
6、REEITEM hItem);/检查是否可以对该ITEM实施Drag&Drop)Header file#if !defined(AFX_TREEVIEWEXT_H_99D8F6F8_79F0_11D1_8DC6_0000E8125FE5_INCLUDED_)#define AFX_TREEVIEWEXT_H_99D8F6F8_79F0_11D1_8DC6_0000E8125FE5_INCLUDED_#if _MSC_VER = 1000#pragma once#endif / _MSC_VER = 1000/ TreeViewExt.h : header file/ CTreeViewExt
7、viewclass CTreeViewExt : public CTreeViewprotected: CTreeViewExt(); / protected constructor used by dynamic creation DECLARE_DYNCREATE(CTreeViewExt)/ Attributespublic:/ Operationspublic:/ Overrides / ClassWizard generated virtual function overrides /AFX_VIRTUAL(CTreeViewExt) protected: virtual void
8、OnDraw(CDC* pDC); / overridden to draw this view /AFX_VIRTUAL/ Implementationprotected: HTREEITEM m_hDraggedItem; BOOL m_bDraggingNow; /标记 CImageList *m_pDragImageList; virtual CTreeViewExt();#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;#endif / Generated
9、 message map functionsprotected: virtual void CopyItemProperties(HTREEITEM hNewItem, HTREEITEM hDraggedItem); virtual BOOL IsItemCanBeDroppedOn(HTREEITEM hSource, HTREEITEM hTarget); virtual BOOL ItemCanBeDragged(HTREEITEM hItem);/检查是否可以对该ITEM实施Drag&Drop /AFX_MSG(CTreeViewExt) afx_msg void OnBegindr
10、ag(NMHDR* pNMHDR, LRESULT* pResult);/必须重载的函数 afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); /AFX_MSG DECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Developer Studio will insert additional declarations immediately before the previous l
11、ine.#endif / !defined(AFX_TREEVIEWEXT_H_99D8F6F8_79F0_11D1_8DC6_0000E8125FE5_INCLUDED_)Implementation file/ TreeViewExt.cpp : implementation file/#include stdafx.h#include TreeViewExt.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CTreeViewExtIMPLEMENT_DYNC
12、REATE(CTreeViewExt, CTreeView)CTreeViewExt:CTreeViewExt() m_bDraggingNow = FALSE; m_hDraggedItem = NULL; m_pDragImageList = NULL;CTreeViewExt:CTreeViewExt()BEGIN_MESSAGE_MAP(CTreeViewExt, CTreeView) /AFX_MSG_MAP(CTreeViewExt) ON_NOTIFY_REFLECT(TVN_BEGINDRAG, OnBegindrag) ON_WM_MOUSEMOVE() ON_WM_LBUT
13、TONUP() /AFX_MSG_MAPEND_MESSAGE_MAP()/ CTreeViewExt drawingvoid CTreeViewExt:OnDraw(CDC* pDC) CDocument* pDoc = GetDocument(); / TODO: add draw code here/ CTreeViewExt diagnostics#ifdef _DEBUGvoid CTreeViewExt:AssertValid() const CTreeView:AssertValid();void CTreeViewExt:Dump(CDumpContext& dc) const
14、 CTreeView:Dump(dc);#endif /_DEBUG/ CTreeViewExt message handlersvoid CTreeViewExt:OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult) NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; if (!m_bDraggingNow) /先检查是否正在过程中 if (ItemCanBeDragged(pNMTreeView-itemNew.hItem) /查询条件,是否允许Drag&Drop CTreeCtrl& tree = GetT
15、reeCtrl(); tree.SetCapture(); m_bDraggingNow = TRUE; m_hDraggedItem = pNMTreeView-itemNew.hItem;/保存变量 tree.Select(m_hDraggedItem, TVGN_CARET); m_pDragImageList = tree.CreateDragImage(m_hDraggedItem); m_pDragImageList-DragEnter(&tree, pNMTreeView-ptDrag); m_pDragImageList-BeginDrag(0, CPoint(0, 0); *
16、pResult = 0;void CTreeViewExt:OnMouseMove(UINT nFlags, CPoint point) if (m_bDraggingNow) CTreeCtrl& tree = GetTreeCtrl(); m_pDragImageList-DragEnter(&tree, point); m_pDragImageList-DragMove(point); CTreeView:OnMouseMove(nFlags, point);void CTreeViewExt:OnLButtonUp(UINT nFlags, CPoint point) if (m_bD
17、raggingNow) ReleaseCapture(); m_bDraggingNow = FALSE; m_pDragImageList-EndDrag(); delete m_pDragImageList; m_pDragImageList = NULL; CTreeCtrl& tree = GetTreeCtrl(); UINT flags; HTREEITEM hTargetItem = tree.HitTest(point, &flags);/得到目标 if (hTargetItem != NULL & IsItemCanBeDroppedOn(m_hDraggedItem, hT
18、argetItem) /查询是否可以成功完成,条件是开始时选中的Item和结束使选中的ITEM是否满足你的条件 HTREEITEM hNewItem = tree.InsertItem(Untitled, hTargetItem); CopyItemProperties(hNewItem, m_hDraggedItem);/进行处理 if (nFlags != MK_CONTROL) tree.DeleteItem(m_hDraggedItem); m_hDraggedItem = NULL; CTreeView:OnLButtonUp(nFlags, point);BOOL CTreeViewExt:ItemCanBeDragged(HTREEITEM hItem)/作用为决定现在是否能开始,可以作为一种运行时的选项 return FALSE;BOOL CTreeViewExt:IsItemCanBeDroppedOn(HTREEITEM hSource, HTREEITEM hTarget)/决定现在结束条件是否正常,如不正常则放弃这次操作 return FALSE;void CTreeViewExt:CopyItemProperties(HTREEITEM hNewItem, HTREEITEM hDraggedItem)/按照你的需要对两个ITEM进行处理。