1 新建SDI,取消文档视图结构
2 删除CChildView
3 必须通过向导创建MFC视图类继承CTreeView
#pragma once
#include <afxcview.h>
// CLeftView 视图
class CLeftView : public CTreeView
{
DECLARE_DYNCREATE(CLeftView)
protected:
CLeftView(); // 动态创建所使用的受保护的构造函数
virtual ~CLeftView();
public:
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
DECLARE_MESSAGE_MAP()
public:
virtual void OnInitialUpdate();
CTreeCtrl& m_tree;
};
// CLeftView.cpp: 实现文件
//
#include "pch.h"
#include "SC.h"
#include "CLeftView.h"
// CLeftView
IMPLEMENT_DYNCREATE(CLeftView, CTreeView)
CLeftView::CLeftView():m_tree(GetTreeCtrl())
{
theApp.m_pLeft = this;
}
CLeftView::~CLeftView()
{
}
BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
END_MESSAGE_MAP()
// CLeftView 诊断
#ifdef _DEBUG
void CLeftView::AssertValid() const
{
CTreeView::AssertValid();
}
#ifndef _WIN32_WCE
void CLeftView::Dump(CDumpContext& dc) const
{
CTreeView::Dump(dc);
}
#endif
#endif //_DEBUG
// CLeftView 消息处理程序
void CLeftView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
static BOOL bFirstTime = TRUE;
if (bFirstTime)
{
bFirstTime = FALSE;
m_tree.ModifyStyle(0, TVS_FULLROWSELECT | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT);
HTREEITEM hItem = m_tree.InsertItem(_T("本地文件夹"));
m_tree.InsertItem(_T("收件箱"), hItem);
m_tree.InsertItem(_T("发件箱"), hItem);
m_tree.InsertItem(_T("已发送邮件"), hItem);
m_tree.InsertItem(_T("已删除邮件"), hItem);
m_tree.InsertItem(_T("草稿邮件"), hItem);
m_tree.Expand(hItem, TVE_EXPAND);
}
// TODO: 在此添加专用代码和/或调用基类
}
4 必须通过向导创建MFC视图类继承CFrameWnd ,该Frame实现右边上下的分割
#pragma once
// CRightFrame 框架
class CRightFrame : public CFrameWnd
{
public:
CSplitterWnd m_split;
DECLARE_DYNCREATE(CRightFrame)
protected:
CRightFrame(); // 动态创建所使用的受保护的构造函数
virtual ~CRightFrame();
protected:
DECLARE_MESSAGE_MAP()
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
};
// CRightFrame.cpp: 实现文件
//
#include "pch.h"
#include "SC.h"
#include "CRightFrame.h"
#include "CTopView.h"
#include "CBottomView.h"
// CRightFrame
IMPLEMENT_DYNCREATE(CRightFrame, CFrameWnd)
CRightFrame::CRightFrame()
{
}
CRightFrame::~CRightFrame()
{
}
BEGIN_MESSAGE_MAP(CRightFrame, CFrameWnd)
END_MESSAGE_MAP()
// CRightFrame 消息处理程序
BOOL CRightFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: 在此添加专用代码和/或调用基类
m_split.CreateStatic(this, 2, 1);
m_split.CreateView(0, 0, RUNTIME_CLASS(CTopView), CSize(0, 200), NULL);
m_split.CreateView(1, 0, RUNTIME_CLASS(CBottomView), CSize(0, 0), NULL);
return CFrameWnd::OnCreateClient(lpcs, pContext);
}
5 必须通过向导创建MFC视图类继承CListView
#pragma once
#include <afxcview.h>
// CTopView 视图
class CTopView : public CListView
{
DECLARE_DYNCREATE(CTopView)
protected:
CTopView(); // 动态创建所使用的受保护的构造函数
virtual ~CTopView();
public:
CListCtrl& m_list;
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
DECLARE_MESSAGE_MAP()
public:
virtual void OnInitialUpdate();
};
// CTopView.cpp: 实现文件
//
#include "pch.h"
#include "SC.h"
#include "CTopView.h"
// CTopView
IMPLEMENT_DYNCREATE(CTopView, CListView)
CTopView::CTopView():m_list(GetListCtrl())
{
theApp.m_pTop = this;
}
CTopView::~CTopView()
{
}
BEGIN_MESSAGE_MAP(CTopView, CListView)
END_MESSAGE_MAP()
// CTopView 诊断
#ifdef _DEBUG
void CTopView::AssertValid() const
{
CListView::AssertValid();
}
#ifndef _WIN32_WCE
void CTopView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
#endif
#endif //_DEBUG
// CTopView 消息处理程序
void CTopView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: 在此添加专用代码和/或调用基类
static BOOL bFirstTime = TRUE;
if (bFirstTime)
{
bFirstTime = FALSE;
m_list.ModifyStyle(0, LVS_SHOWSELALWAYS | LVS_REPORT);
m_list.ModifyStyleEx(0, LVS_EX_GRIDLINES);
m_list.InsertColumn(0, _T("发件人"), 0, 180);
m_list.InsertColumn(1, _T("主题"), 0, 180);
m_list.InsertColumn(2, _T("接收时间"), 0, 180);
}
}
6 修改CMainFrame代码
// MainFrm.h: CMainFrame 类的接口
//
#pragma once
class CMainFrame : public CFrameWnd
{
public:
CMainFrame() noexcept;
protected:
DECLARE_DYNAMIC(CMainFrame)
// 特性
public:
CSplitterWnd m_split;
// 操作
public:
// 重写
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
// 实现
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected: // 控件条嵌入成员
CToolBar m_wndToolBar;
CStatusBar m_wndStatusBar;
// 生成的消息映射函数
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSetFocus(CWnd *pOldWnd);
DECLARE_MESSAGE_MAP()
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
};
// MainFrm.cpp: CMainFrame 类的实现
//
#include "pch.h"
#include "framework.h"
#include "SC.h"
#include "MainFrm.h"
#include "CLeftView.h"
#include "CRightFrame.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_SETFOCUS()
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // 状态行指示器
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
// CMainFrame 构造/析构
CMainFrame::CMainFrame() noexcept
{
// TODO: 在此添加成员初始化代码
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("未能创建工具栏\n");
return -1; // 未能创建
}
if (!m_wndStatusBar.Create(this))
{
TRACE0("未能创建状态栏\n");
return -1; // 未能创建
}
m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));
// TODO: 如果不需要可停靠工具栏,则删除这三行
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
}
// CMainFrame 诊断
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame 消息处理程序
void CMainFrame::OnSetFocus(CWnd* /*pOldWnd*/)
{
// 将焦点前移到视图窗口
/*m_wndView.SetFocus();*/
}
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// 让视图第一次尝试该命令
/*if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;*/
// 否则,执行默认处理
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: 在此添加专用代码和/或调用基类
m_split.CreateStatic(this,1, 2);
m_split.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(200, 0), NULL);
m_split.CreateView(0, 1, RUNTIME_CLASS(CRightFrame),CSize(0,0),NULL);
return CFrameWnd::OnCreateClient(lpcs, pContext);
}