MFC:view文檔的分欄

來源:互聯網
上載者:User

  我們有時候想在單文檔的介面上面實現多視圖,實現的方法就是對原文檔的介面進行分割,以N行M列的形式分割,當然分割也可以遞迴進行,分割完一次以後,我們還可以在已經分割的其中一個試圖上再次分割。

下面舉個例子,把一個單一文件介面切分成4塊。效果如所示。


分割的步驟就是先分成1行2列,然後再把分好的第二列分成3行一列(注意的是這裡的行列標號都是從0開始的,我們說的第二列其實是1列,前面的是0列)

  下面看實現的方法。

  1 首先我們建立4個對話方塊,ID分別為IDD_TREE1 IDD_LIST1 IDD_TREE2 IDD_LIST2  注意對話方塊的style要選child。

    2 建立4個View的類,這裡我們讓這四個View的類繼承於FormView,(這樣,可以在上面添加控制項)。

      CList1View   繼承於FormView 關聯對話方塊 IDD_LIST1


CList2View   繼承於FormView  關聯對話方塊IDD_LIST2

     CTree1View   繼承於FormView  關聯對話方塊IDD_TREE1

     CTree2View   繼承於FormView  關聯對話方塊IDD_TREE2

   3  在CMainFrame類中添加兩個變數,類型為 CSplitterWnd(關於這類型的介紹請看MSDN)

      CSplitterWnd m_wndSplitter;
      CSplitterWnd m_wndSplitter1;

      然後在CMainFrame類中添加虛函數:

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

      在源檔案函數實現添加如下代碼:

   

    BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
 // TODO: Add your specialized code here and/or call the base class
 CRect cr;
 GetWindowRect( &cr );
 
 //建立一個靜態分欄視窗,分為一行二列
  if(m_wndSplitter.CreateStatic(this,1,2)==NULL)
        return FALSE;

    //將TreeView1串連到0行0列窗格上
 m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(TreeView1),CSize(cr.Width()/4, cr.Height()), pContext);
    if(m_wndSplitter1.CreateStatic(&m_wndSplitter,3,1,WS_CHILD|WS_VISIBLE, m_wndSplitter.IdFromRowCol(0, 1))==NULL)
        return FALSE; //將第0行1列再分開3行1列
    //將ListView1類串連到第二個分欄對象的0行0列
 m_wndSplitter1.CreateView(0,0,RUNTIME_CLASS(ListView1),CSize(3*cr.Width()/4,cr.Height()/3),pContext);
    //將TreeView2類串連到第二個分欄對象的1行0列
 m_wndSplitter1.CreateView(1,0,RUNTIME_CLASS(TreeView2),CSize(3*cr.Width()/4,cr.Height()/3),pContext);
 //將ListView2類串連到第二個分欄對象的2行0列
 m_wndSplitter1.CreateView(2,0,RUNTIME_CLASS(ListView2),CSize(3*cr.Width()/4,cr.Height()/3),pContext);
    return TRUE;

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

    並注意在次源檔案頭添加include檔案:

    #include "List1View.h"
    #include "List2View.h"
    #include "Tree1View.h"
    #include "Tree2View.h"

    然後運行程式,視窗就會實現分割。

轉載自http://blog.sina.com.cn/s/blog_760680fc0100pomn.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.