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),
   CSize(100,100), NULL);

 

3. 在CXDialog的OnInitDialog事件最后添加如下代码:

CRect cRect;

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


ScreenToClient(&cRect);

m_pMyFrame->MoveWindow(&cRect);
m_pMyFrame->ShowWindow(SW_SHOW);
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"

 

大功告成!