在这里插入代码片
一:首先需要建两个类,目的是到时候定义划分的窗口种类
点击项目:项目图片再点击类向导
鼠标移动至添加类,点击添加mfc类, 类名命名为FrameList ,基类选 CListView, 类名命名为FrameEdit 基类选 CEditView。
随后点击类向导,添加消息函数WM_Create.再在消息函数中添加如下代码
CCreateContext ctxList, ctxEdit;
ctxList.m_pNewViewClass = RUNTIME_CLASS(FrameEdit);
ctxList.m_pCurrentDoc = NULL;
ctxList.m_pNewDocTemplate = NULL;
ctxList.m_pLastView = NULL;
ctxList.m_pCurrentFrame = NULL;
ctxEdit.m_pNewViewClass = RUNTIME_CLASS(FrameEdit);
ctxEdit.m_pCurrentDoc = NULL;
ctxEdit.m_pNewDocTemplate = NULL;
ctxEdit.m_pLastView = NULL;
ctxEdit.m_pCurrentFrame = NULL;
// Because the CFrameWnd needs a window class, we will create
// a new one. I just copied the sample from MSDN Help.
// When using it in your project, you may keep CS_VREDRAW and
// CS_HREDRAW and then throw the other three parameters.
CString strMyClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW, ::LoadCursor(NULL, IDC_ARROW), (HBRUSH)::GetStockObject(WHITE_BRUSH), ::LoadIcon(NULL, IDI_APPLICATION));
// Create the frame window with "this" as the parent
m_pFrameWnd = new CFrameWnd();
m_pFrameWnd->Create(strMyClass, _T(""), WS_CHILD, CRect(0, 0, 50, 50), this);
m_pFrameWnd->ShowWindow(SW_SHOW);
m_pFrameWnd->MoveWindow(0, 0, 500, 400);
// and finally, create the splitter with the frame as
// the parent
m_wndSplitter.CreateStatic(m_pFrameWnd, 1, 2);//建立第一层构架
//m_wndSplitter.CreateStatic(this, 1, 2);//建立第一层构架
m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(FrameList), CSize(200,200), &ctxList);
m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(FrameEdit), CSize(200, 200), &ctxEdit);
m_wndSplitter2.CreateStatic(&m_wndSplitter, 3, 1, WS_CHILD | WS_VISIBLE | WS_BORDER,
m_wndSplitter.IdFromRowCol(0,0));//建立第二层构架
m_wndSplitter.DeleteView(0,0);//动态拆分
m_wndSplitter2.CreateView(0, 0, RUNTIME_CLASS(FrameList), CSize(100, 100), &ctxList);
m_wndSplitter2.CreateView(1, 0, RUNTIME_CLASS(FrameList), CSize(100,100), &ctxList);
m_wndSplitter2.CreateView(2, 0, RUNTIME_CLASS(FrameList), CSize(100, 100), &ctxList);
m_wndSplitter3.CreateStatic(&m_wndSplitter2, 1, 2, WS_CHILD | WS_VISIBLE | WS_BORDER,
m_wndSplitter2.IdFromRowCol(0, 0));
m_wndSplitter3.CreateView(0, 0, RUNTIME_CLASS(FrameList), CSize(100, 100), &ctxList);
return 0;
至此,主题部分写完。随后,我们需要在消息处理函数OnInitDialog中添加如下代码
// TODO: 在此添加额外的初始化代码
CRect rect;
// Get the rectangle of the custom window. The custom window
// is just a a big button that is not visible and is disabled.
// It's a trick to not use coordinates directly.
//GetDlgItem(IDD_THREECUT_DIALOG)->
GetWindowRect(&rect);
// Move the splitter
ScreenToClient(&rect);
m_pFrameWnd->MoveWindow(&rect);
m_pFrameWnd->ShowWindow(SW_SHOW);
//m_wndSplitter.MoveWindow(0, 0, rect.Width(), rect.Height());
//m_wndSplitter.ShowWindow(SW_SHOW);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
写成这样还是会报错,因为很多变量没有定义,我们需要在 XXXDlg.h头文件中定义如下变量
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
CFrameWnd *m_pFrameWnd;
CSplitterWnd m_wndSplitter;
CSplitterWnd m_wndSplitter2;
到此,所有代码全部写完。运行时会有个中断,我也没有弄清原因,点忽略就会看到想要的结果。