程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> MFC 窗口分割,mfc窗口分割

MFC 窗口分割,mfc窗口分割

編輯:C++入門知識

MFC 窗口分割,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);
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved