不用splitter控件 简单实现对mfc对话框的分割的方法

时间:2023-03-09 14:55:05
不用splitter控件 简单实现对mfc对话框的分割的方法

不用splitter控件  简单实现对mfc对话框的分割的方法

直接贴上源代码主要部分吧

这个是基于对话框的工程 进行对话框的分割实现

只是相应了三个消息函数,看一下就会明白的

我空间资源里边有现成的工程代码可以下载运行

.cpp 文件

  1. // spliteDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "splite.h"
  5. #include "spliteDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CSpliteDlg dialog
  13. CSpliteDlg::CSpliteDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CSpliteDlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CSpliteDlg)
  17. //}}AFX_DATA_INIT
  18. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  19. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  20. m_SplitCursor = LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CURSOR_SPLIT));
  21. }
  22. void CSpliteDlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(CSpliteDlg)
  26. DDX_Control(pDX, IDC_LIST, m_edit);
  27. DDX_Control(pDX, IDC_TREE, m_tree);
  28. //}}AFX_DATA_MAP
  29. }
  30. BEGIN_MESSAGE_MAP(CSpliteDlg, CDialog)
  31. //{{AFX_MSG_MAP(CSpliteDlg)
  32. ON_WM_MOUSEMOVE()
  33. ON_WM_LBUTTONDOWN()
  34. ON_WM_LBUTTONUP()
  35. ON_WM_SIZE()
  36. //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CSpliteDlg message handlers
  40. #define SIZEBAR          2// Space BTW Tree and Edit
  41. BOOL CSpliteDlg::OnInitDialog()
  42. {
  43. CDialog::OnInitDialog();
  44. // Set the icon for this dialog.  The framework does this automatically
  45. //  when the application's main window is not a dialog
  46. SetIcon(m_hIcon, TRUE);         // Set big icon
  47. SetIcon(m_hIcon, FALSE);        // Set small icon
  48. CRect rect ;
  49. GetClientRect( &rect );
  50. // TODO: Add extra initialization here
  51. CRect treepos ;
  52. m_tree.GetClientRect( treepos );
  53. m_tree.MoveWindow(0, 0, treepos.Width(), rect.Height() );
  54. m_edit.MoveWindow(treepos.Width() +SIZEBAR , 0, rect.Width()-(treepos.Width() +SIZEBAR), rect.Height() );
  55. return TRUE;  // return TRUE  unless you set the focus to a control
  56. }
  57. void CSpliteDlg::OnMouseMove(UINT nFlags, CPoint point)
  58. {
  59. // TODO: Add your message handler code here and/or call default
  60. CRect treepos ;
  61. m_tree.GetWindowRect( &treepos );
  62. ScreenToClient( &treepos );
  63. CRect editpos ;
  64. m_edit.GetWindowRect( &editpos );
  65. ScreenToClient( &editpos );
  66. CRect rc;
  67. rc.left = treepos.right;
  68. rc.right = editpos.left;
  69. rc.top = treepos.top;
  70. rc.bottom = treepos.bottom;
  71. CRect rect;
  72. GetClientRect( &rect );
  73. if ( rect.PtInRect(point) )
  74. {
  75. SetCursor(m_SplitCursor);  //设置鼠标光标形状
  76. if ( nFlags & MK_LBUTTON )
  77. {
  78. ResizeWindows(point.x, rect.right-rect.left);
  79. }
  80. }
  81. else
  82. {
  83. if(m_bButtonDown)
  84. {
  85. m_bButtonDown = FALSE;
  86. ReleaseCapture();
  87. }
  88. }
  89. CDialog::OnMouseMove(nFlags, point);
  90. }
  91. void CSpliteDlg::ResizeWindows( int CxBarAt, int len )
  92. {
  93. CRect treepos ;
  94. m_tree.GetClientRect( &treepos );
  95. CRect rect ;
  96. GetClientRect( &rect );
  97. if(CxBarAt <= 1)
  98. CxBarAt = 1;
  99. if(CxBarAt >= len - 1)
  100. CxBarAt = len - 1;
  101. // 移动tree窗口
  102. m_tree.MoveWindow( 0,0, CxBarAt-SIZEBAR, rect.Height() );
  103. // 移动edit窗口
  104. m_edit.MoveWindow(CxBarAt, 0, rect.right-CxBarAt, rect.Height());
  105. }
  106. void CSpliteDlg::OnLButtonDown(UINT nFlags, CPoint point)
  107. {
  108. // TODO: Add your message handler code here and/or call default
  109. m_bButtonDown = TRUE;
  110. CRect treepos ;
  111. m_tree.GetWindowRect( &treepos );
  112. ScreenToClient( &treepos );
  113. CRect editpos ;
  114. m_edit.GetWindowRect( &editpos );
  115. ScreenToClient( &editpos );
  116. CRect rect;
  117. rect.left = treepos.right;
  118. rect.right = editpos.left;
  119. rect.top = treepos.top;
  120. rect.bottom = treepos.bottom;
  121. //  CRect rect;
  122. //  GetClientRect( &rect );
  123. if ( rect.PtInRect(point) )
  124. {
  125. SetCursor(m_SplitCursor);
  126. }
  127. SetCapture();
  128. CDialog::OnLButtonDown(nFlags, point);
  129. }
  130. void CSpliteDlg::OnLButtonUp(UINT nFlags, CPoint point)
  131. {
  132. // TODO: Add your message handler code here and/or call default
  133. m_bButtonDown=FALSE;
  134. ReleaseCapture();
  135. CDialog::OnLButtonUp(nFlags, point);
  136. }
  137. void CSpliteDlg::OnSize(UINT nType, int cx, int cy)
  138. {
  139. CDialog::OnSize(nType, cx, cy);
  140. // TODO: Add your message handler code here
  141. if((IsWindow(m_tree.m_hWnd)) && (IsWindow(m_edit.m_hWnd)))
  142. {// get sizes; width of tree never changed
  143. CRect rcTree;
  144. m_tree.GetClientRect(&rcTree);
  145. CRect rcEdit;
  146. m_edit.GetClientRect(&rcEdit);
  147. // 移动tree控件
  148. rcTree.bottom=cy;
  149. m_tree.MoveWindow(&rcTree,TRUE);
  150. // 移动edit控件
  151. rcEdit.left=rcTree.right+SIZEBAR;
  152. rcEdit.right=cx;
  153. rcEdit.top=0;
  154. rcEdit.bottom=rcTree.bottom;
  155. m_edit.MoveWindow(&rcEdit,TRUE);
  156. }
  157. }

.h 头文件

    1. // spliteDlg.h : header file
    2. //
    3. #if !defined(AFX_SPLITEDLG_H__5C7DA2C0_E37E_426A_A7D3_E1DC7DFD2766__INCLUDED_)
    4. #define AFX_SPLITEDLG_H__5C7DA2C0_E37E_426A_A7D3_E1DC7DFD2766__INCLUDED_
    5. #if _MSC_VER > 1000
    6. #pragma once
    7. #endif // _MSC_VER > 1000
    8. /////////////////////////////////////////////////////////////////////////////
    9. // CSpliteDlg dialog
    10. class CSpliteDlg : public CDialog
    11. {
    12. // Construction
    13. public:
    14. CSpliteDlg(CWnd* pParent = NULL);   // standard constructor
    15. // Dialog Data
    16. //{{AFX_DATA(CSpliteDlg)
    17. enum { IDD = IDD_SPLITE_DIALOG };
    18. CListCtrl   m_edit;
    19. CTreeCtrl   m_tree;
    20. //}}AFX_DATA
    21. // ClassWizard generated virtual function overrides
    22. //{{AFX_VIRTUAL(CSpliteDlg)
    23. protected:
    24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    25. //}}AFX_VIRTUAL
    26. // Implementation
    27. protected:
    28. HICON m_hIcon;
    29. // Generated message map functions
    30. //{{AFX_MSG(CSpliteDlg)
    31. virtual BOOL OnInitDialog();
    32. afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    33. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    34. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    35. afx_msg void OnSize(UINT nType, int cx, int cy);
    36. //}}AFX_MSG
    37. DECLARE_MESSAGE_MAP()
    38. private:
    39. void ResizeWindows(int CxBarAt, int len );
    40. private:
    41. HCURSOR     m_SplitCursor;
    42. BOOL        m_bButtonDown;
    43. };
    44. //{{AFX_INSERT_LOCATION}}
    45. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    46. #endif // !defined(AFX_SPLITEDLG_H__5C7DA2C0_E37E_426A_A7D3_E1DC7DFD2766__INCLUDED_)