zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    hdu 1263 水题一道
    hdu 1115 多边形重心
    hdu 4054 字符串处理
    梦想与实验:向阿里巴巴学习
    美国研究员开发电脑芯片模拟人脑工作
    阿里巴巴网站运营模式
    中文Alexa排名专业术语解释
    欧美两大3G标准正式成为我国行业标准
    网站优化的十大奇招妙技
    有志赚钱创业者 从这八个步骤开始
  • 原文地址:https://www.cnblogs.com/yunqie/p/6050520.html
Copyright © 2011-2022 走看看