DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

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

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

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

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

.cpp 文件

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


 .h 头文件

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