分类: C/C++
2008-03-04 10:53:15
/************************************************
* *
* 树型控件操作实例 *
* *
************************************************/
#pragma once
#include "afxcmn.h"
#include "afxwin.h"
#include
using namespace std ;
// CTreeSampleDlg 对话框
typedef struct tagTreeItem{
char CatalogID[50] ;
char CatalogName[50] ;
char CatalogType[50] ;
int n1 ;
int n2 ;
}STreeItem ;
class CTreeSampleDlg : public CDialog
{
DECLARE_DYNAMIC(CTreeSampleDlg)
public:
CTreeSampleDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CTreeSampleDlg();
// 对话框数据
enum { IDD = IDD_DLG_TREESAMPLE };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
// 树型控件
CTreeCtrl m_tree;
public:
// 添加结点
CButton m_btnAdd;
public:
// 修改结点
CButton m_btnEdit;
public:
// 删除结点
CButton m_btnDel;
public:
// 目录ID
CEdit m_EditCatalogId;
public:
// 目录名称
CEdit m_EditCatalogName;
public:
// 目录类型
CEdit m_EditCatalogType;
public:
afx_msg void OnBnClickedBtnadd();
public:
afx_msg void OnBnClickedBtnedit();
public:
afx_msg void OnBnClickedBtndel();
public:
virtual BOOL OnInitDialog() ;
public:
//设置树型控件样式
void SetTreeStyle() ;
//添加树结点
void AddTreeItem(LPTVINSERTSTRUCT lpStruct) ;
//修改结点
void SetTreeItem(LPTVITEM lpItem) ;
//删除结点
void DelTreeItem(HTREEITEM hItem) ;
public:
afx_msg void OnNMDblclkTree(NMHDR *pNMHDR, LRESULT *pResult);
};
/////////////////////////////////////////////////////
// TreeSampleDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "MFC1.h"
#include "TreeSampleDlg.h"
// CTreeSampleDlg 对话框
IMPLEMENT_DYNAMIC(CTreeSampleDlg, CDialog)
CTreeSampleDlg::CTreeSampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTreeSampleDlg::IDD, pParent)
{
}
CTreeSampleDlg::~CTreeSampleDlg()
{
}
void CTreeSampleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TREE, m_tree);
DDX_Control(pDX, IDC_BTNADD, m_btnAdd);
DDX_Control(pDX, IDC_BTNEDIT, m_btnEdit);
DDX_Control(pDX, IDC_BTNDEL, m_btnDel);
DDX_Control(pDX, IDC_EDITCATALOGID, m_EditCatalogId);
DDX_Control(pDX, IDC_EDITCATALOGNAME, m_EditCatalogName);
DDX_Control(pDX, IDC_EDITCATALOGTYPE, m_EditCatalogType);
}
BEGIN_MESSAGE_MAP(CTreeSampleDlg, CDialog)
ON_BN_CLICKED(IDC_BTNADD, &CTreeSampleDlg::OnBnClickedBtnadd)
ON_BN_CLICKED(IDC_BTNEDIT, &CTreeSampleDlg::OnBnClickedBtnedit)
ON_BN_CLICKED(IDC_BTNDEL, &CTreeSampleDlg::OnBnClickedBtndel)
ON_NOTIFY(NM_DBLCLK, IDC_TREE, &CTreeSampleDlg::OnNMDblclkTree)
END_MESSAGE_MAP()
BOOL CTreeSampleDlg::OnInitDialog()
{
CDialog::OnInitDialog() ;
SetTreeStyle() ;
return true ;
}
// CTreeSampleDlg 消息处理程序
//添加结点
void CTreeSampleDlg::OnBnClickedBtnadd()
{
// TODO: 在此添加控件通知处理程序代码
CString sCatalogID("") ;
CString sCatalogName("") ;
CString sCatalogType("") ;
m_EditCatalogId.GetWindowText(sCatalogID) ;
m_EditCatalogName.GetWindowText(sCatalogName) ;
m_EditCatalogType.GetWindowText(sCatalogType) ;
STreeItem *itemData = (STreeItem*)malloc(sizeof(STreeItem)) ;
strcpy(itemData->CatalogID,sCatalogID.GetBuffer()) ;
strcpy(itemData->CatalogName,sCatalogName.GetBuffer()) ;
strcpy(itemData->CatalogType,sCatalogType.GetBuffer()) ;
char *szCatalogName = (char*)malloc(sizeof(char)*(sCatalogName.GetLength()+1));
strcpy(szCatalogName,sCatalogName.GetBuffer()) ;
HTREEITEM hCurItem = m_tree.GetSelectedItem() ;
TVINSERTSTRUCT tvStruct ;
TVITEM tvItem ;
tvItem.mask = TVIF_TEXT|TVIF_PARAM ;
tvItem.pszText = szCatalogName ;
tvItem.lParam = (LPARAM)itemData ;
STreeItem *id = (STreeItem*)tvItem.lParam ;
tvStruct.hInsertAfter = TVI_LAST ;
tvStruct.item = tvItem ;
if(hCurItem==NULL){
tvStruct.hParent = TVI_ROOT ;
}else{
tvStruct.hParent = hCurItem ;
}
m_tree.InsertItem(&tvStruct) ;
m_tree.Invalidate() ; //重画客户区
}
//修改结点
void CTreeSampleDlg::OnBnClickedBtnedit()
{
// TODO: 在此添加控件通知处理程序代码
CString sCatalogID("") ;
CString sCatalogName("") ;
CString sCatalogType("") ;
m_EditCatalogId.GetWindowText(sCatalogID) ;
m_EditCatalogName.GetWindowText(sCatalogName) ;
m_EditCatalogType.GetWindowText(sCatalogType) ;
STreeItem *itemData = (STreeItem*)malloc(sizeof(STreeItem)) ;
strcpy(itemData->CatalogID,sCatalogID.GetBuffer()) ;
strcpy(itemData->CatalogName,sCatalogName.GetBuffer()) ;
strcpy(itemData->CatalogType,sCatalogType.GetBuffer()) ;
char *szCatalogName = (char*)malloc(sizeof(char)*(sCatalogName.GetLength()+1));
strcpy(szCatalogName,sCatalogName.GetBuffer()) ;
HTREEITEM hCurItem = m_tree.GetSelectedItem() ;
TVITEM tvItem ;
tvItem.mask = TVIF_TEXT|TVIF_PARAM ;
tvItem.hItem = hCurItem ;
tvItem.pszText = szCatalogName ;
tvItem.lParam = (LPARAM)itemData ;
if(hCurItem!=NULL){
m_tree.SetItem(&tvItem) ;
m_tree.Invalidate() ; //重画客户区
}
}
//删除结点
void CTreeSampleDlg::OnBnClickedBtndel()
{
// TODO: 在此添加控件通知处理程序代码
HTREEITEM hItem = m_tree.GetSelectedItem() ;
if(hItem!=NULL){
m_tree.DeleteItem(hItem) ;
m_tree.Invalidate() ;
}
}
//设置树型控件样式
void CTreeSampleDlg::SetTreeStyle()
{
//TVS_HASLINES 在父/子结点之间绘制连线
//TVS_LINESATROOT 在根/子结点之间绘制连线
//TVS_HASBUTTONS 在每一个结点前添加一个按钮,用于表示当前结点是否已被展开
//TVS_EDITLABELS 结点的显示字符可以被编辑
//TVS_SHOWSELALWAYS 在失去焦点时也显示当前选中的结点
m_tree.ModifyStyle(0,TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS|TVS_EDITLABELS|TVS_SHOWSELALWAYS);
}
//添加树结点
void CTreeSampleDlg::AddTreeItem(LPTVINSERTSTRUCT lpStruct)
{
m_tree.InsertItem(lpStruct) ;
}
//修改结点
void CTreeSampleDlg::SetTreeItem(LPTVITEM lpItem)
{
m_tree.SetItem(lpItem) ;
}
//删除结点
void CTreeSampleDlg::DelTreeItem(HTREEITEM hItem)
{
m_tree.DeleteItem(hItem) ;
}
//双击事件
void CTreeSampleDlg::OnNMDblclkTree(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
int nCode = pNMHDR->code ;
HWND hWnd = pNMHDR->hwndFrom ;
unsigned int id = pNMHDR->idFrom ;
long nResult = (*pResult) ;
HTREEITEM hItem = m_tree.GetSelectedItem() ;
TVITEM curitem ;
curitem.mask = TVIF_HANDLE ;
curitem.hItem = hItem ;
if(m_tree.GetItem(&curitem)){
STreeItem *treeItem = (STreeItem*)curitem.lParam ;
m_EditCatalogId.SetWindowText(treeItem->CatalogID) ;
m_EditCatalogName.SetWindowText(treeItem->CatalogName) ;
m_EditCatalogType.SetWindowText(treeItem->CatalogType) ;
}
*pResult = 0;
}