MFC 窗口分割

动态分割窗口:

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
if (m_WndSplitter.Create(this, , , CSize(, ), pContext))
{
return TRUE;
}
else
{
return false;
}
//return CFrameWnd::OnCreateClient(lpcs, pContext);
}

静态分割窗口:

为MainFrame类添加两个成员变量如下,并重载虚函数OnCreateClient,以实现窗口分割。

CSplitterWnd m_WndSplitter1;
CSplitterWnd m_WndSplitter2;
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);

以CView为基类创建CView2,CView3,CView4类。

在MainFrame.cpp包含所有视图头文件

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
CRect rect;
GetClientRect(&rect);
m_WndSplitter1.CreateStatic(this,,);  //创建一个静态分栏窗口,分为3行一列 m_WndSplitter1.CreateView(, , RUNTIME_CLASS(C窗口分割2View), CSize(, ), pContext);  //连接0行0列窗格 m_WndSplitter1.CreateView(, , RUNTIME_CLASS(CView2), CSize(, ), pContext);  //用cview2连接1行0列窗格 //将两行0列再分开1行2列
if (!m_WndSplitter2.CreateStatic(&m_WndSplitter1, , , WS_CHILD | WS_VISIBLE, m_WndSplitter1.IdFromRowCol(, )))
return FALSE; m_WndSplitter2.CreateView(, , RUNTIME_CLASS(CView3), CSize(, ), pContext);  //用cview3连接到第二个分行对象0行0列窗格 m_WndSplitter2.CreateView(, , RUNTIME_CLASS(CView4), CSize(, ), pContext);  //用cview4连接到第二个分行对象0行1列窗格 return TRUE;
}

固定分割窗口的分割线:

以 CSplitterWnd为基类,创建新的CFixSplitterWnd 类,并在CFixSplitterWnd 类中手动添加WM_LBUTTONDOWN、WM_MOUSEMOVE、WM_SETCURSOR消息的消息映射和消息响应函数。

CFixSplitterWnd 类代码如下:

class CFixSplitterWnd :
public CSplitterWnd
{
public:
CFixSplitterWnd(BOOL b_lock=TRUE);
virtual ~CFixSplitterWnd();
public:
BOOL m_block;
void Lock() { m_block = true; } //锁定分割线
void UnLock() { m_block = FALSE; } //不锁定分割线 DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
};
CFixSplitterWnd::CFixSplitterWnd(BOOL b_lock)
{
m_block = b_lock;
} CFixSplitterWnd::~CFixSplitterWnd()
{
}
BEGIN_MESSAGE_MAP(CFixSplitterWnd, CSplitterWnd)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
END_MESSAGE_MAP() void CFixSplitterWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (!m_block)
CSplitterWnd::OnLButtonDown(nFlags, point);
else
CWnd::OnLButtonDown(nFlags,point);
} void CFixSplitterWnd::OnMouseMove(UINT nFlags, CPoint point) //移动鼠标
{
// TODO: Add your message handler code here and/or call default
if (!m_block)
CSplitterWnd::OnMouseMove(nFlags, point);
else
CWnd::OnMouseMove(nFlags, point);
} BOOL CFixSplitterWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) //设置光标
{
// TODO: Add your message handler code here and/or call default
if (!m_block)
return CSplitterWnd::OnSetCursor(pWnd, nHitTest, message);
else
return CWnd::OnSetCursor(pWnd, nHitTest, message);
}

在CMainFrame类中 ,m_WndSplitter1、 m_WndSplitter2的基类由 CSplitterWnd 设置为 CFixSplitterWnd.重载的 OnCreateClient 代码如下.

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class m_WndSplitter1.Lock();
m_WndSplitter2.UnLock(); m_WndSplitter1.CreateStatic(this, , ); m_WndSplitter1.CreateView(, , RUNTIME_CLASS(C窗口分割3View), CSize(, ), pContext);//连接0行0列窗格 m_WndSplitter1.CreateView(, , RUNTIME_CLASS(CView2), CSize(, ), pContext);//用cview2连接1行0列窗格 //将两行0列再分开1行2列
if (!m_WndSplitter2.CreateStatic(&m_WndSplitter1, , , WS_CHILD | WS_VISIBLE,
m_WndSplitter1.IdFromRowCol(, )))
return FALSE; m_WndSplitter2.CreateView(, , RUNTIME_CLASS(CView3), CSize(, ), pContext);//用cview3连接到第二个分行对象0行0列窗格 m_WndSplitter2.CreateView(, , RUNTIME_CLASS(CView4), CSize(, ), pContext);//用cview4连接到第二个分行对象0行1列窗格 return TRUE; // return CFrameWnd::OnCreateClient(lpcs, pContext);
}
上一篇:「C语言」Windows+EclipseCDT下的C语言开发环境准备


下一篇:css中常用的选择器和选择器优先级