分割窗口

来源:互联网 发布:关于淘宝的书籍 编辑:程序博客网 时间:2024/05/20 10:13

首先,创建一个单文档工程,我命名为Split。

然后,插入两个类,由于我要建的是上下两个窗口,所以我把两个类分别命名为CViewup,CViewunder,它们的基类是CScrollView。

然后,在MainFrm.h 中添加CSplitterWnd m_splitterwnd;

现在开始在MainFrm.cpp中重载OnCreateClient,并把函数修改为以下代码:

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
// TODO: Add your specialized code here and/or call the base class
//创建一个矩形变量用于控制分割窗口的大小
CRect rect;
  GetWindowRect(&rect);
//将窗口分割成上下两个窗口
  m_splitterwnd.CreateStatic(this,2,1);
//分别对每个窗口进行分配类
  m_splitterwnd.CreateView(0,0,RUNTIME_CLASS(CViewup),CSize(rect.Width(),rect.Height()-rect.Height()/10),
  pContext);
  m_splitterwnd.CreateView(1,0,RUNTIME_CLASS(CViewunder),CSize(rect.Width(),rect.Height()/10),
  pContext);
//设置滚动条
m_splitterwnd.SetScrollStyle( WS_HSCROLL | WS_VSCROLL);
  m_splitterwnd.SetColumnInfo(0,500,10);
  return TRUE;
//return CFrameWnd::OnCreateClient(lpcs, pContext);

}

然后我又重载了ActivateFrame,因为我希望运行的时候能直接最大化

void CMainFrame::ActivateFrame(int nCmdShow) 
{
// TODO: Add your specialized code here and/or call the base class
//运行时最大化
nCmdShow= SW_SHOWMAXIMIZED;


CFrameWnd::ActivateFrame(nCmdShow);
}

这个时候分割的窗口就建立好了!(写于2012.11.29)

 

注:我之前一直在纠结于为什么不能用工程自己的窗口类,比如这里的CSplitView,去创建窗口,每次我写RUNTIME_CLASS(CSplitView),的时候都会报错,今天我看了一篇文章,才发现原来要用工程的窗口类去创建窗口不是那样写的,应该写pContext->m_pNewViewClass。这样就可以创建了。(写于2012.12.13)

原创粉丝点击