新建一个对话框
resource.h 和 MyWtl.rc 中分别定义
IDB_TREE1
IDB_TREE2
MainDlg.h 中定义:CButton m_btn;CTreeViewCtrl m_Tree;
MainDlg.cpp 中,替换掉生成的Ok按钮的单击处理函数
LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
RECT cRect;
cRect.left = 20;
cRect.top = 20;
cRect.bottom = 80;
cRect.right = 80;
m_btn.Create(m_hWnd,&cRect,_T("Stop"),WS_CHILD | WS_VISIBLE,0,2008); //生成一个按钮
RECT cRect2;
cRect2.left = 20;
cRect2.top = 100;
cRect2.bottom = 300;
cRect2.right = 250;
m_Tree.Create(m_hWnd,&cRect2,NULL,WS_CHILD | WS_VISIBLE | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT, WS_EX_CLIENTEDGE,2009); //生成树
HBITMAP oBitmap[4];
oBitmap[0]=AtlLoadBitmapImage(IDB_TREE1);
oBitmap[1]=AtlLoadBitmapImage(IDB_TREE2);
CImageList *m_imagelist=new CImageList();
m_imagelist->Create(16,16,0,7,7);
for(int n=0;n<2;n++) {
m_imagelist->Add(oBitmap[n]);
}
m_Tree.SetImageList(m_imagelist->m_hImageList,TVSIL_NORMAL);
HTREEITEM hitem ;
hitem = m_Tree.InsertItem(_T("中国"),0,1,TVI_ROOT,TVI_ROOT);
HTREEITEM HITEM11 = m_Tree.InsertItem(_T("山东"),TVI_ROOT,TVI_LAST);
HTREEITEM HITEM12 = m_Tree.InsertItem(_T("北京"),hitem,TVI_LAST);
HTREEITEM HITEM13 = m_Tree.InsertItem(_T("上海"),hitem,TVI_LAST);
HTREEITEM HITEM14 = m_Tree.InsertItem(_T("天津"),HITEM13,TVI_LAST);
HTREEITEM HITEM15 = m_Tree.InsertItem(_T("大连"),HITEM13,TVI_LAST);
HTREEITEM HITEM16 = m_Tree.InsertItem(_T("陕西"),HITEM13,TVI_LAST);
HTREEITEM HITEM17 = m_Tree.InsertItem(_T("山西"),HITEM13,TVI_LAST);
HTREEITEM HITEM18 = m_Tree.InsertItem(_T("河南"),HITEM12,TVI_LAST);
HTREEITEM HITEM19 = m_Tree.InsertItem(_T("福建"),HITEM12,TVI_LAST);
HTREEITEM HITEM110 = m_Tree.InsertItem(_T("广东"),HITEM12,TVI_LAST);
HTREEITEM HITEM111 = m_Tree.InsertItem(_T("广西"),HITEM12,TVI_LAST);
HTREEITEM HITEM112= m_Tree.InsertItem(_T("浙江"),HITEM12,TVI_LAST);
HTREEITEM HITEM113 =m_Tree.InsertItem(_T("湖南"),HITEM12,TVI_LAST);
HTREEITEM HITEM114 = m_Tree.InsertItem(_T("湖北"),HITEM12,TVI_LAST);
HTREEITEM HITEM115 = m_Tree.InsertItem(_T("新疆"),HITEM11,TVI_LAST);
HTREEITEM HITEM116 = m_Tree.InsertItem(_T("江西"),HITEM11,TVI_LAST);
HTREEITEM HITEM117 = m_Tree.InsertItem(_T("江苏"),HITEM115,TVI_LAST);
HTREEITEM HITEM118 = m_Tree.InsertItem(_T("内蒙古"),HITEM115,TVI_LAST);
//m_Tree.SetBkColor(RGB(128,128,128));
//m_Tree.SetTextColor(RGB(0,0,255));
return 0;
}
生成的效果