MFC中Frame-Splitter-Splitter模型
新建SDI工程,名为ss。
添加4个MFC类,
CLeftView : public CTreeView
CRightFrame : public CFrameWnd
CTopView : public CListView
CBottomView : public CHtmlView
在App类中的ss.h中,添加
CLeftView* m_pLView;
CRightFrame *m_pRFrame;
CTopView *m_pTView;
CBottomView *m_pBView;
在4个派生类中的构造函数中初始化以上的指针
CTopView::CTopView()
{
theApp.m_pTView = this;
}
CLeftView::CLeftView()
{
theApp.m_pLView = this;
}
CRightFrame::CRightFrame()
{
theApp.m_pRFrame = this;
}
CBottomView::CBottomView()
{
theApp.m_pBView = this;
}
修改CMainFrame的头文件,添加 CSplitterWnd m_split; 去掉 ChildView 和 cpp中与它相关的代码。
添加代码
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { // TODO: 在此添加专用代码和/或调用基类 if (!m_split.CreateStatic(this, 1, 2)) return FALSE; m_split.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(150, 0), NULL); m_split.CreateView(0, 1, RUNTIME_CLASS(CRightFrame), CSize(0, 0), NULL); return TRUE; // return CFrameWnd::OnCreateClient(lpcs, pContext); }
在CRightFrame类的头文件中添加一个分割器变量,CSplitterWnd m_Split;
添加代码
添加以下虚函数 BOOL CRightFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { // TODO: 在此添加专用代码和/或调用基类 if (!m_split.CreateStatic(this, 2, 1)) return FALSE; m_split.CreateView(0, 0, RUNTIME_CLASS(CTopView), CSize(0, 300), NULL); m_split.CreateView(1, 0, RUNTIME_CLASS(CBottomView), CSize(0, 0), NULL); return TRUE; // return CFrameWnd::OnCreateClient(lpcs, pContext); } BOOL CRightFrame::PreCreateWindow(CREATESTRUCT& cs) { // TODO: 在此添加专用代码和/或调用基类 CFrameWnd::PreCreateWindow(cs); cs.dwExStyle = WS_EX_DLGMODALFRAME; return TRUE; // return CFrameWnd::PreCreateWindow(cs); } void CLeftView::OnInitialUpdate() { CTreeView::OnInitialUpdate(); // TODO: 在此添加专用代码和/或调用基类 CTreeCtrl& tree = GetTreeCtrl(); if (tree.GetChildItem(NULL)) return; ModifyStyle(0, TVS_HASLINES | TVS_HASBUTTONS | TVS_SHOWSELALWAYS | TVS_LINESATROOT); HTREEITEM hItem = tree.InsertItem(L"Outlook Express"); tree.InsertItem(_T("收件箱"), hItem); tree.InsertItem(_T("发件箱"), hItem); tree.InsertItem(_T("已发送邮件"), hItem); tree.InsertItem(_T("已删除邮件"), hItem); tree.InsertItem(_T("草稿"), hItem); tree.Expand(hItem, TVE_EXPAND); } void CTopView::OnInitialUpdate() { CListView::OnInitialUpdate(); // TODO: 在此添加专用代码和/或调用基类 if (GetStyle() & LVS_REPORT) return; ModifyStyle(0, LVS_REPORT | LVS_SHOWSELALWAYS); CListCtrl& list = GetListCtrl(); list.SetExtendedStyle(LVS_EX_FULLROWSELECT); list.InsertColumn(0, _T("发件人"), 0, 200); list.InsertColumn(1, _T("主题"), 0, 200); list.InsertColumn(2, _T("发送时间"), 0, 200); list.InsertItem(0, _T("Microsoft Outlook Express 开发组")); list.SetItemText(0, 1, _T("欢迎使用 Outlook Express 6")); list.SetItemText(0, 2, _T("2019-10-24")); } void CTopView::OnLvnItemchanged(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); // TODO: 在此添加控件通知处理程序代码 HD_NOTIFY* p = (HD_NOTIFY*)pNMHDR; CListCtrl& list = GetListCtrl(); CString str = _T("about:blank"); if (list.GetSelectedCount()) { TCHAR s[64] = { 0 }; ::GetWindowsDirectory(s, sizeof(s) / sizeof(s[0])); str.Format(_T("file://%s/web/tip.htm"), s); } //str = _T("https://baidu.com"); theApp.m_pBView->Navigate2(str, NULL, NULL); *pResult = 0; }
结果:
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。