Tree Control 控件的使用Dlg.cpp
上传用户:by888888
上传日期:2019-03-04
资源大小:1854k
文件大小:9k
源码类别:
TreeView控件
开发平台:
Visual C++
- // Tree Control 控件的使用Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Tree Control 控件的使用.h"
- #include "Tree Control 控件的使用Dlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class 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
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_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_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTreeControlDlg dialog
- CTreeControlDlg::CTreeControlDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTreeControlDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTreeControlDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CTreeControlDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTreeControlDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTreeControlDlg, CDialog)
- //{{AFX_MSG_MAP(CTreeControlDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
- ON_NOTIFY(TVN_ENDLABELEDIT, IDC_TREE1, OnEndlabeleditTree1)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTreeControlDlg message handlers
- BOOL CTreeControlDlg::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 dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- ////////////////////////////////////////////////////////////////////
- static char *color[]={"white","blank","red","blue","yellow","cyan","purple","green"};
- HICON hIcon[8];
- int n;
- m_list.Create(16,16,0,8,8);
- hIcon[0]=AfxGetApp()->LoadIcon(IDI_ICON_WHITE);
- hIcon[1]=AfxGetApp()->LoadIcon(IDI_ICON_BLANK);
- hIcon[2]=AfxGetApp()->LoadIcon(IDI_ICON_RED);
- hIcon[3]=AfxGetApp()->LoadIcon(IDI_ICON_BLUE);
- hIcon[4]=AfxGetApp()->LoadIcon(IDI_ICON_YELLOW);
- hIcon[5]=AfxGetApp()->LoadIcon(IDI_ICON_CYAN);
- hIcon[6]=AfxGetApp()->LoadIcon(IDI_ICON_PURPLE);
- hIcon[7]=AfxGetApp()->LoadIcon(IDI_ICON_GREEN);
- for(n=0;n<8;n++)
- m_list.Add(hIcon[n]);
- ///////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////
- CTreeCtrl *pTree=(CTreeCtrl *) GetDlgItem(IDC_TREE1);
- pTree->SetImageList(&m_list,TVSIL_NORMAL); //设置图片列表
- TV_INSERTSTRUCT tvinsert; //创建待加入的TV_INSERTSTRUCT的结构
- tvinsert.hParent=NULL; //无父节点
- tvinsert.hInsertAfter=TVI_LAST; //插入到本层最后
- tvinsert.item.mask=TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT;
- //掩码,图标、选选中图标、文字
- tvinsert.item.hItem=NULL;//句柄为空
- tvinsert.item.state=0; //状态
- tvinsert.item.stateMask=0;//状态为掩码
- tvinsert.item.cchTextMax=6;//最大文字长度
- tvinsert.item.iSelectedImage=1;//选中图标索引
- tvinsert.item.cChildren=0; //唯有子节点
- tvinsert.item.lParam=0; //自定义数据
- //创建第一层
- tvinsert.item.iImage=2; //一般图标
- tvinsert.item.pszText="father";//插入第一层的一个节点“father”
- HTREEITEM hDad=pTree->InsertItem(&tvinsert);
- tvinsert.item.pszText="mother";//插入第一层的第二个节点“mother”
- HTREEITEM hMom=pTree->InsertItem(&tvinsert);
- //创建第二层
- tvinsert.hParent=hDad; //父节点为"father"
- tvinsert.item.iImage=3; //一般图标
- tvinsert.item.pszText="david"; // 插入第二层father的第一个节点son
- pTree->InsertItem(&tvinsert);
- tvinsert.item.pszText="lili"; // 插入第二层father的第一个节点son
- pTree->InsertItem(&tvinsert);
- tvinsert.item.pszText="kate"; // 插入第二层father的第一个节点son
- pTree->InsertItem(&tvinsert);
- tvinsert.item.pszText="jime"; // 插入第二层father的第一个节点son
- pTree->InsertItem(&tvinsert);
- tvinsert.hParent=hMom; //父节点为hMom
- tvinsert.item.iImage=4; //
- tvinsert.item.pszText="david"; // 插入第二层mother的第一个节点son
- pTree->InsertItem(&tvinsert);
- tvinsert.item.pszText="lili"; // 插入第二层mother的第一个节点son
- pTree->InsertItem(&tvinsert);
- tvinsert.item.pszText="kate"; // 插入第二层mother的第一个节点son
- pTree->InsertItem(&tvinsert);
- tvinsert.item.pszText="jime"; // 插入第二层mother的第一个节点son
- HTREEITEM hOther=pTree->InsertItem(&tvinsert);
- //创建第三层
- tvinsert.hParent=hOther; //父节点为jime
- tvinsert.item.iImage=7;
- tvinsert.item.pszText="tom"; // 插入第二层hOther的第一个节点tom
- pTree->InsertItem(&tvinsert);
- tvinsert.item.pszText="baidi"; // 插入第二层hOther的第一个节点baidu
- pTree->InsertItem(&tvinsert);
- ///////////////////////////////////////////////////////////////
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CTreeControlDlg::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 CTreeControlDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int 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 icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CPaintDC dc(this);
- CPen pen;
- CRect rc;
- GetClientRect(&rc); //获取客户其大小
- for(int i=0;i<rc.Height();i++)
- {
- pen.CreatePen(PS_SOLID,50,RGB(100,200,200)); //创建画笔 通过改变RGB()函数的参数可以改变背景颜色,经过复杂的算法可以画出很复杂的背景图
- CPen *pOldPen=dc.SelectObject(&pen);
- dc.MoveTo(0,i); //将比尖移到起点
- dc.LineTo(rc.right,i); //画线到终点
- dc.SelectObject(pOldPen);
- pen.DeleteObject();
- }
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CTreeControlDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CTreeControlDlg::OnOK()
- {
- // TODO: Add extra validation here
- CDialog::OnOK();
- }
- void CTreeControlDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- CDialog::OnCancel();
- }
- void CTreeControlDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
- CTreeCtrl *pTree=(CTreeCtrl *)GetDlgItem(IDC_TREE1);
- HTREEITEM hSelected=pNMTreeView->itemNew.hItem;
- if(hSelected!=NULL)
- {
- char text[31];
- TV_ITEM item;
- item.mask=TVIF_HANDLE|TVIF_TEXT;
- item.hItem=hSelected;
- item.pszText=text;
- item.cchTextMax=30;
- VERIFY(pTree->GetItem(&item));
- SetDlgItemText(IDC_STATIC_TREE,text);
- }
- *pResult = 0;
- }
- void CTreeControlDlg::OnEndlabeleditTree1(NMHDR* pNMHDR, LRESULT* pResult)
- {
- TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
- // TODO: Add your control notification handler code here
- TV_ITEM item=pTVDispInfo->item;
- CString str=item.pszText;
- str.TrimLeft();
- str.TrimRight();
- if(str.GetLength()>0)
- {
- CTreeCtrl *pTree=(CTreeCtrl *)GetDlgItem(IDC_TREE1);
- pTree->SetItemText(item.hItem,item.pszText);
- }
- *pResult = 0;
- }