1 新建MFC单文档工程SingleDocument
2 在工程中点右键选择类向导,添加类右边选择MFC类
输入类名,选择基类CTreeView
3 在CLeftTreeView里面添加引用和成员变量
#include <afxcview.h>
CImageList m_ImageList;
4 为CLeftTreeView重载OnInitialUpdate()函数
void CLeftTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
if (m_ImageList.m_hImageList == NULL)
{
m_ImageList.Create(16, 16, ILC_COLOR24 | ILC_MASK, 2, 2);
HICON hIcon;
hIcon = ExtractIcon(AfxGetInstanceHandle(), "Shell32.dll", 15);
m_ImageList.Add(hIcon);
hIcon = ExtractIcon(AfxGetInstanceHandle(), "Shell32.dll", 7);
m_ImageList.Add(hIcon);
CTreeCtrl& treeCtrl = GetTreeCtrl();
treeCtrl.ModifyStyle(0, TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT);
treeCtrl.SetImageList(&m_ImageList, TVSIL_NORMAL);
HTREEITEM hRootItem = treeCtrl.InsertItem("我的电脑", 0, 0, TVI_ROOT);
treeCtrl.InsertItem("本地磁盘(D:)", 1, 1, hRootItem);
treeCtrl.InsertItem("本地磁盘(E:)", 1, 1, hRootItem);
treeCtrl.InsertItem("本地磁盘(F:)", 1, 1, hRootItem);
treeCtrl.InsertItem("本地磁盘(G:)", 1, 1, hRootItem);
treeCtrl.Expand(hRootItem, TVE_EXPAND);
}
}
5 在CSingleDocumentView里面添加下面的引用和成员变量
#include "SingleDocumentDoc.h"
CString m_strPrintText;
6 修改CSingleDocumentView的OnDraw方法
void CSingleDocumentView::OnDraw(CDC* pDC)
{
CSingleDocumentDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
CRect rect;
GetClientRect(&rect);
pDC->FillSolidRect(rect, RGB(192, 192, 192));
CFont font;
font.CreatePointFont(240, "impact");
CFont* pOldFont = (CFont*)pDC->SelectObject(&font);
pDC->SetTextColor(RGB(255, 0, 0));
pDC->TextOut(100, 100, m_strPrintText);
pDC->SelectObject(pOldFont);
}
7 在CMainFrame里面添加成员变量
CSplitterWnd m_wndSplitter;
8 重载CMainFrame的OnCreateClient()方法
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: 在此添加专用代码和/或调用基类
if (m_wndSplitter.CreateStatic(this,1,2))
{
m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftTreeView), CSize(200, 0), pContext);
m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CSingleDocumentView), CSize(200, 0), pContext);
}
return true;
}
9 运行即可