我们在开发程序的时候,经常需要切分窗口。现在介绍一种方法:使用MFC的CSplitterWnd类切分窗口以抛出石头引出玉来。
在MainFrame.h
protected:
CSplitterWnd m_splitter; //引入 CSplitterWnd类
#include "afxext.h" //CEditView类所需要的头文件.
在MainFrame.cpp 文件中
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
if (!m_splitter.CreateStatic(this,1,2))
{
return false;
}
CRect rect;
GetClientRect(&rect);
m_splitter.CreateView(0,1,RUNTIME_CLASS(CSingleDocTestView),CSize(rect.Width()-rect.Width()/4,rect.Height()),pContext);
m_splitter.CreateView(0,0,RUNTIME_CLASS(CMyEditView),CSize(rect.Width()/4,rect.Height()),pContext);
return TRUE;
// return CFrameWnd::OnCreateClient(lpcs, pContext); //这个要注销掉,替换掉 return TRUE;
}
创建一个CEditView类的派生类
class CMyEditView : public CEditView
{
}
在 SingleDocView.cpp中.
void CSingleDocTestView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CRect rect;
GetClientRect(&rect);
if (m_Testdlg.m_hWnd)
{
m_Testdlg.MoveWindow(&rect);
}
}
void CSingleDocTestView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
m_Testdlg.Create(IDD_TESTDIALOG,this);
m_Testdlg.ShowWindow(SW_SHOW);
CRect rect;
GetClientRect(&rect);
if (m_Testdlg.m_hWnd)
{
m_Testdlg.MoveWindow(&rect);
}
}
创建一个 窗体对话框
CTestDlg m_Testdlg;