MFC 窗口分割

Posted 韵切

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC 窗口分割相关的知识,希望对你有一定的参考价值。

动态分割窗口:

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
    if (m_WndSplitter.Create(this, 2, 2, CSize(10, 10), 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,1);  //创建一个静态分栏窗口,分为3行一列

    m_WndSplitter1.CreateView(0, 0, RUNTIME_CLASS(C窗口分割2View), CSize(100, 100), pContext);  //连接0行0列窗格

    m_WndSplitter1.CreateView(1, 0, RUNTIME_CLASS(CView2), CSize(100, 100), pContext);  //用cview2连接1行0列窗格

    //将两行0列再分开1行2列
    if (!m_WndSplitter2.CreateStatic(&m_WndSplitter1, 1, 2, WS_CHILD | WS_VISIBLE, m_WndSplitter1.IdFromRowCol(2, 0)))
        return FALSE;

    m_WndSplitter2.CreateView(0, 0, RUNTIME_CLASS(CView3), CSize(400, 300), pContext);  //用cview3连接到第二个分行对象0行0列窗格

    m_WndSplitter2.CreateView(0, 1, RUNTIME_CLASS(CView4), CSize(400, 300), 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, 3, 1);

    m_WndSplitter1.CreateView(0, 0, RUNTIME_CLASS(C窗口分割3View), CSize(100, 100), pContext);//连接0行0列窗格

    m_WndSplitter1.CreateView(1, 0, RUNTIME_CLASS(CView2), CSize(100, 100), pContext);//用cview2连接1行0列窗格

                                                                                      //将两行0列再分开1行2列
    if (!m_WndSplitter2.CreateStatic(&m_WndSplitter1, 1, 2, WS_CHILD | WS_VISIBLE,
        m_WndSplitter1.IdFromRowCol(2, 0)))
        return FALSE;

    m_WndSplitter2.CreateView(0, 0, RUNTIME_CLASS(CView3), CSize(400, 300), pContext);//用cview3连接到第二个分行对象0行0列窗格

    m_WndSplitter2.CreateView(0, 1, RUNTIME_CLASS(CView4), CSize(400, 300), pContext);//用cview4连接到第二个分行对象0行1列窗格

    return TRUE;

//    return CFrameWnd::OnCreateClient(lpcs, pContext);
}

 

以上是关于MFC 窗口分割的主要内容,如果未能解决你的问题,请参考以下文章

MFC窗口分割相关链接

MFC 窗口分割(拆分)问题

窗口分割

MFC CFileDialog窗口问题

MFC单文档 添加 多视图

MFC 中嵌入的 opencv 窗口,错误