天天看點

在基于對話框的應用程式中嵌入CSplitterWnd

MFC-在基于對話框的應用程式中嵌入CSplitterWnd

2008-11-04 21:54

在CodeGuru中搜尋了一下,找到三篇文章,其中一篇好使。

http://www.codeguru.com/cpp/w-d/splitter/tutorials/article.php/c4705/

步驟如下:(CXDialog是你從CDialog繼承下來的類)

1. 在CXDialog中添加兩個成員 :

CSplitterWnd m_cSplitter;

CFrameWnd* m_pMyFrame;

2. 在CXDialog的OnCreate事件CDialog::OnCreate後添加如下代碼:

// Create the frame window with "this" as the parent

m_pMyFrame = new CFrameWnd;

m_pMyFrame->Create(NULL,_T(""), WS_CHILD,

   CRect(0,0,1,1), this);

m_pMyFrame->ShowWindow(SW_SHOW);

m_pMyFrame->MoveWindow(0,0,300,300);

// and finally, create the splitter with the frame as

// the parent

m_cSplitter.CreateStatic(m_pMyFrame,1, 2);

m_cSplitter.CreateView(0,0, RUNTIME_CLASS(CEditView),

   CSize(100,100), NULL);

m_cSplitter.CreateView(0,1, RUNTIME_CLASS(CListView),

3. 在CXDialog的OnInitDialog事件最後添加如下代碼:

CRect cRect;

GetDlgItem(IDC_CUTSOM_WINDOW)->GetWindowRect(&cRect);

ScreenToClient(&cRect);

m_pMyFrame->MoveWindow(&cRect);

m_cSplitter.MoveWindow(0,0, cRect.Width(), cRect.Height());

m_cSplitter.ShowWindow(SW_SHOW);

return TRUE;

4. 注意到以上的GetDlgItem(IDC_CUTSOM_WINDOW)函數中的IDC_CUTSOM_WINDOW,你需要在對話框設計器中添加一個不可見的,disable的按鈕,并将其ID設定為IDC_CUTSOM_WINDOW。另外,整個framewnd都将會建立在這個button的 clientrect中,是以,你要将這個button弄得大點。

5.添加頭檔案 #include   "afxcview.h"

繼續閱讀