拆分窗口类CSplitterWnd在对话框中的应用及拆分子窗口间的通信

来源:互联网 发布:淘宝联盟推广位名称 编辑:程序博客网 时间:2024/05/01 01:37
分类: MFC
  当你在GOOGLE或者百度键入"如何在对话框中使用CSplitterWnd“时,搜索出来的帖子大多千篇一律,鲜有真正是基于对话框的CSplitterWnd应用,大多是基于单文档框架的说明。至于具有参考价值的一篇博文,请参见Codeguru。

本文将告诉你如何在对话框中使用CSplitterWnd将主窗口拆分成两个子窗口,并且只与视图类有关,不涉及文档类。重载OnSize(), 使得窗口的大小可随主窗口的变化而改变。

步骤如下:

1,在主窗口类(某对话框)头文件中分别声明一个框架类以及一个CSplitterWnd拆分窗口类的指针类型成员变量

[cpp] view plaincopyprint?
  1. private:  
  2.     CFrameWnd*      m_pFrameSplit;    // 分隔窗口  
  3.     CSplitterWnd    m_wndSpliter;     // 左右分隔  

2,在主窗口类实现文件中的OnInitDialog()方法或OnCreate()事件中进行初始化工作,注册框架类及初始CSplitterWnd

[cpp] view plaincopyprint?
  1. // 取得主窗口区域   
  2. CRect rc;  
  3. GetDlgItem(IDC_CHILDWND)->GetWindowRect(rc);  //IDC_CHILDWND是一个PictureBox的ID,表示要拆分的区域  
  4. ScreenToClient(&rc);  
  5.   
  6. // 注册框架类   
  7. CString sClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,  
  8.     ::LoadCursor(NULL, IDC_ARROW), (HBRUSH)::GetStockObject(WHITE_BRUSH),  
  9.     ::LoadIcon(NULL, IDI_APPLICATION));  
  10.   
  11. // 创建分隔窗口框架   
  12. m_pFrameSplit = new CFrameWnd;  
  13. m_pFrameSplit->Create(sClass, _T(""), WS_CHILD, CRect(0,0,0,0), this);  
  14. m_pFrameSplit->MoveWindow(rc);  
  15. m_pFrameSplit->ShowWindow(SW_SHOW);  
  16.   
  17. // 创建分割窗口   
  18. m_wndSpliter.CreateStatic(m_pFrameSplit, 1, 2);  
  19. m_wndSpliter.CreateView(0, 0, RUNTIME_CLASS(CEditView), CSize((int)(rc.Width() * 0.265), 0), NULL);   // 左窗口所占比例26.5%  
  20. m_wndSpliter.CreateView(0, 1, RUNTIME_CLASS(CEditView), CSize(0,0), NULL);  
  21. m_wndSpliter.MoveWindow(0, 0, rc.Width(), rc.Height());  
  22. m_wndSpliter.ShowWindow(SW_SHOW);  

注意倒数第三、四行。这里是要拆分的两个窗口对应的视图类,为了方便演示CSplitterWnd类拆分的效果,可以先直接用CEditView代替。

运行结果,主窗口的PictureBox区域被拆分成两个EditBox,中间有可移动的分隔栏用以调节大小。


3.重载OnSize(),使得窗口及控件自适应大小。传控件指针或ID两种方式均可。

[cpp] view plaincopyprint?
  1. void CMainWndDlg::OnSize(UINT nType, int cx, int cy)  
  2. {  
  3.     CDialog::OnSize(nType, cx, cy);  
  4.   
  5.     static int nLastCx = 0;  
  6.     static int nLastCy = 0;  
  7.     int nWidth  = cx - nLastCx;  
  8.     int nHeight = cy - nLastCy;  
  9.     AdjustDlgItem(m_pFrameSplit, 0, 0, nWidth, nHeight);  
  10.     nLastCx = cx;  
  11.     nLastCy = cy;  
  12. }  
  13.   
  14. // 移动控件 以实现窗口自适应   
  15. void CMainWndDlg::AdjustDlgItem(UINT nId, int nLeft, int nTop,int nRight, int nBottom)  
  16. {  
  17.     AdjustDlgItem(GetDlgItem(nId), nLeft, nTop, nRight, nBottom);  
  18. }  
  19.   
  20. // 移动控件 以实现窗口自适应   
  21. void CMainWndDlg::AdjustDlgItem(CWnd* pItem, int nLeft, int nTop,int nRight, int nBottom)  
  22. {  
  23.     if(NULL == pItem)  
  24.         return;  
  25.     if(!IsWindow(pItem->GetSafeHwnd()))  
  26.         return;  
  27.   
  28.     // 取得控件区域   
  29.     CRect rcWnd;  
  30.     pItem->GetWindowRect(&rcWnd);  
  31.     ScreenToClient(&rcWnd);  
  32.   
  33.     // 重新计算区域   
  34.     rcWnd.top += nTop;  
  35.     rcWnd.bottom += nBottom;  
  36.     rcWnd.left += nLeft;  
  37.     rcWnd.right += nRight;  
  38.   
  39.     // 移动控件   
  40.     pItem->MoveWindow(rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height());  
  41. }  


为分割窗添加一些自己需要的功能

0 0
原创粉丝点击