MFC中如何将 CFormView放置到一个CDockablePane中
最近再搭建一个平台,设计到界面设计,然后就遇到题目中所述的问题,简单的讲就是要把CFormView放置到CDockablePane中,利用CDockablePane做成那种浮动的效果,郁闷的发现网络上貌似关于这点的信息较少,有的也是说的含糊不清。没办法只能自己研究了 ~ 好吧,其他就都不说了,直接上代码:
CMainFrame类
1 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
2 {
3 if(CMDIFrameWndEx::OnCreate(lpCreateStruct) == -1)
4 return -1;
5 // enable Visual Studio 2008 style docking window behavior
6 CDockingManager::SetDockingMode(DT_SMART);
7 // enable Visual Studio 2008 style docking window auto-hide behavior
8 EnableAutoHidePanes(CBRS_ALIGN_ANY);
9 // create docking windows
10 if(!CreateDockingWindows())
11 {
12 TRACE0("Failed to create docking windows\\n");
13 return -1;
14 }
15
16 m_wndManageDock.EnableDocking(CBRS_ALIGN_ANY);
17 DockPane(&m_wndManageDock);
18 return 0;
19
2 {
3 if(CMDIFrameWndEx::OnCreate(lpCreateStruct) == -1)
4 return -1;
5 // enable Visual Studio 2008 style docking window behavior
6 CDockingManager::SetDockingMode(DT_SMART);
7 // enable Visual Studio 2008 style docking window auto-hide behavior
8 EnableAutoHidePanes(CBRS_ALIGN_ANY);
9 // create docking windows
10 if(!CreateDockingWindows())
11 {
12 TRACE0("Failed to create docking windows\\n");
13 return -1;
14 }
15
16 m_wndManageDock.EnableDocking(CBRS_ALIGN_ANY);
17 DockPane(&m_wndManageDock);
18 return 0;
19
1 BOOL CMainFrame::CreateDockingWindows()
2 {
3
4 ////
此处省略VS自动生成的一些代码
5
6 if (!m_wndManageDock.Create(_T("Manage Panel"), this, CRect(0, 0, 200, 200), TRUE, 222,
7 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT| CBRS_FLOAT_MULTI))
8 {
9 TRACE0("未能创建“属性”窗口\n");
10 return FALSE; // 未能创建
11 }
12
13 SetDockingWindowIcons(theApp.m_bHiColorIcons);
14 return TRUE;
15
16 }
2 {
3
4 ////


5
6 if (!m_wndManageDock.Create(_T("Manage Panel"), this, CRect(0, 0, 200, 200), TRUE, 222,
7 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT| CBRS_FLOAT_MULTI))
8 {
9 TRACE0("未能创建“属性”窗口\n");
10 return FALSE; // 未能创建
11 }
12
13 SetDockingWindowIcons(theApp.m_bHiColorIcons);
14 return TRUE;
15
16 }
ManageDock.h
1
#pragma once
2 #include " ManagePanel.h "
3
4 // CManageDock
5 class CManageDock : public CDockablePane
6 {
7 DECLARE_DYNAMIC(CManageDock)
8
9 public :
10 CManageDock();
11 virtual ~ CManageDock();
12
13 public :
14 CManagePanel * m_wndManagePanel;
15
16 protected :
17 DECLARE_MESSAGE_MAP()
18 public :
19 afx_msg void OnPaint();
20 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
21 };
2 #include " ManagePanel.h "
3
4 // CManageDock
5 class CManageDock : public CDockablePane
6 {
7 DECLARE_DYNAMIC(CManageDock)
8
9 public :
10 CManageDock();
11 virtual ~ CManageDock();
12
13 public :
14 CManagePanel * m_wndManagePanel;
15
16 protected :
17 DECLARE_MESSAGE_MAP()
18 public :
19 afx_msg void OnPaint();
20 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
21 };
ManageDock.cpp
1
#include
"
stdafx.h
"
2 #include " EB Studio.h "
3 #include " ManageDock.h "
4 // CManageDock
5
6 IMPLEMENT_DYNAMIC(CManageDock, CDockablePane)
7
8 CManageDock::CManageDock()
9 {
10
11 }
12
13 CManageDock:: ~ CManageDock()
14 {
15 }
16
17
18 BEGIN_MESSAGE_MAP(CManageDock, CDockablePane)
19 ON_WM_PAINT()
20 ON_WM_CREATE()
21 END_MESSAGE_MAP()
22
23
24 // CManageDock 消息处理程序
25
26 void CManageDock::OnPaint()
27 {
28 CPaintDC dc( this ); // device context for painting
29 // TODO: 在此处添加消息处理程序代码
30 // 不为绘图消息调用 CDockablePane::OnPaint()
31 CRect rc;
32 GetClientRect(rc);
33 CBrush brush;
34 brush.CreateSolidBrush(RGB( 255 , 255 , 255 )); // 背景设置为白色
35
36 dc.FillRect( & rc, & brush);
37 }
38
39 int CManageDock::OnCreate(LPCREATESTRUCT lpCreateStruct)
40 {
41 if (CDockablePane::OnCreate(lpCreateStruct) == - 1 )
42 return - 1 ;
4
44 // TODO: 在此添加您专用的创建代码
45
46 CRect rectDummy;
47 rectDummy.SetRectEmpty();
48
49 // if(!m_wndFormView->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, rectDummy, this, 0, NULL)); // 重要的地方
50 // {
51 // TRACE0("未能创建文件视图\n");
52 // return -1; // 未能创建
53 // }
54 m_wndManagePanel = CManagePanel::CreateOne( this );
55
56 return 0 ;
57 }
2 #include " EB Studio.h "
3 #include " ManageDock.h "
4 // CManageDock
5
6 IMPLEMENT_DYNAMIC(CManageDock, CDockablePane)
7
8 CManageDock::CManageDock()
9 {
10
11 }
12
13 CManageDock:: ~ CManageDock()
14 {
15 }
16
17
18 BEGIN_MESSAGE_MAP(CManageDock, CDockablePane)
19 ON_WM_PAINT()
20 ON_WM_CREATE()
21 END_MESSAGE_MAP()
22
23
24 // CManageDock 消息处理程序
25
26 void CManageDock::OnPaint()
27 {
28 CPaintDC dc( this ); // device context for painting
29 // TODO: 在此处添加消息处理程序代码
30 // 不为绘图消息调用 CDockablePane::OnPaint()
31 CRect rc;
32 GetClientRect(rc);
33 CBrush brush;
34 brush.CreateSolidBrush(RGB( 255 , 255 , 255 )); // 背景设置为白色
35
36 dc.FillRect( & rc, & brush);
37 }
38
39 int CManageDock::OnCreate(LPCREATESTRUCT lpCreateStruct)
40 {
41 if (CDockablePane::OnCreate(lpCreateStruct) == - 1 )
42 return - 1 ;
4
44 // TODO: 在此添加您专用的创建代码
45
46 CRect rectDummy;
47 rectDummy.SetRectEmpty();
48
49 // if(!m_wndFormView->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, rectDummy, this, 0, NULL)); // 重要的地方
50 // {
51 // TRACE0("未能创建文件视图\n");
52 // return -1; // 未能创建
53 // }
54 m_wndManagePanel = CManagePanel::CreateOne( this );
55
56 return 0 ;
57 }
ManagePanel.h
1 #pragma once 2 #include "Resource.h" 3 4 // CManagePanel 窗体视图 5 class CManagePanel : public CFormView 6 { 7 DECLARE_DYNCREATE(CManagePanel) 8 9 //protected:原来 10 public: 11 CManagePanel(); // 动态创建所使用的受保护的构造函数 12 virtual ~CManagePanel(); 13 14 static CManagePanel *CreateOne( CWnd *pParent );//自己加 15 16 17 public: 18 enum { IDD = IDD_MANAGEPANEL }; 19 #ifdef _DEBUG 20 virtual void AssertValid() const; 21 #ifndef _WIN32_WCE 22 virtual void Dump(CDumpContext& dc) const; 23 #endif 24 #endif 25 26 protected: 27 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 28 29 DECLARE_MESSAGE_MAP() 30 public: 31 virtual void OnInitialUpdate(); 32 afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message); 33 };
ManagePanel.cpp
1 #include "stdafx.h" 2 #include "EB Studio.h" 3 #include "ManagePanel.h" 4 5 // CManagePanel 6 7 IMPLEMENT_DYNCREATE(CManagePanel, CFormView) 8 9 CManagePanel::CManagePanel() 10 : CFormView(CManagePanel::IDD) 11 { 12 13 } 14 15 CManagePanel::~CManagePanel() 16 { 17 } 18 19 void CManagePanel::DoDataExchange(CDataExchange* pDX) 20 { 21 CFormView::DoDataExchange(pDX); 22 } 23 24 BEGIN_MESSAGE_MAP(CManagePanel, CFormView) 25 ON_WM_MOUSEACTIVATE() 26 END_MESSAGE_MAP() 27 28 29 // CManagePanel 诊断 30 31 #ifdef _DEBUG 32 void CManagePanel::AssertValid() const 33 { 34 CFormView::AssertValid(); 35 } 36 37 #ifndef _WIN32_WCE 38 void CManagePanel::Dump(CDumpContext& dc) const 39 { 40 CFormView::Dump(dc); 41 } 42 #endif 43 #endif //_DEBUG 44 45 46 // CManagePanel 消息处理程序 47 48 void CManagePanel::OnInitialUpdate() 49 { 50 CFormView::OnInitialUpdate(); 51 52 GetParentFrame()->RecalcLayout(); 53 SetScrollSizes( MM_TEXT, CSize( 300, 300 ) ); 54 ResizeParentToFit(FALSE); 55 56 // TODO: 在此添加专用代码和/或调用基类 57 } 58 59 CManagePanel *CManagePanel::CreateOne( CWnd *pParent ) 60 { 61 CManagePanel *p_ManagePanel = new CManagePanel; 62 63 //CMyFormView *pFormView = NULL; 64 //CRuntimeClass *pRuntimeClass = RUNTIME_CLASS(CMyFormView); 65 //pFormView = (CMyFormView *)pRuntimeClass->CreateObject(); 66 67 //CDockableFormViewAppDoc *pDoc = CDockableFormViewAppDoc::CreateOne();///////////////////////////////////////// 68 //pFormView->m_pDocument = pDoc;//////////////////////////////////////////////////////////////////////////////// 69 CCreateContext *pContext = NULL; 70 #if 0 71 if( !p_ManagePanel->CreateEx(0, NULL, NULL, WS_CHILD | WS_VISIBLE, CRect(0,0,205,157), 72 pParent, -1, pContext ) ) 73 #else 74 if (!p_ManagePanel->Create(NULL, NULL, WS_CHILD | WS_VISIBLE, CRect(0, 0, 300, 300), pParent, 0, pContext)) 75 #endif 76 //if( !pFormView->CreateEx( 0, AfxRegisterWndClass(0, 0, 0, 0), NULL, 77 // WS_CHILD | WS_VISIBLE, CRect( 0, 0, 205, 157), pParent, -1, pContext) ) 78 { 79 AfxMessageBox( _T("Failed in creating CMyFormView") ); 80 81 } 82 p_ManagePanel->OnInitialUpdate(); 83 return p_ManagePanel; 84 85 } 86 87 int CManagePanel::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message) 88 { 89 // TODO: 在此添加消息处理程序代码和/或调用默认值 90 91 int nResult = 0; 92 93 CFrameWnd* pParentFrame = GetParentFrame(); 94 if(pParentFrame == pDesktopWnd) 95 { 96 // When this is docked 97 nResult = CFormView::OnMouseActivate(pDesktopWnd, nHitTest, message); 98 } 99 else 100 { 101 // When this is not docked 102 BOOL isMiniFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CMiniFrameWnd ) ); 103 BOOL isPaneFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CPaneFrameWnd ) ); 104 BOOL isMultiPaneFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CMultiPaneFrameWnd ) ); 105 // pDesktopWnd is the frame window for CDockablePane 106 nResult = CWnd::OnMouseActivate( pDesktopWnd, nHitTest, message ); 107 108 //nResult = CWnd::OnMouseActivate( pDesktopWnd, nHitTest, message ); 109 //if( nResult == MA_NOACTIVATE || nResult == MA_NOACTIVATEANDEAT ) 110 // return nResult; 111 //if (pDesktopWnd != NULL) 112 //{ 113 // // either re-activate the current view, or set this view to be active 114 // //CView* pView = pDesktopWnd->GetActiveView(); 115 // //HWND hWndFocus = ::GetFocus(); 116 // //if (pView == this && 117 // // m_hWnd != hWndFocus && !::IsChild(m_hWnd, hWndFocus)) 118 // //{ 119 // // // re-activate this view 120 // // OnActivateView(TRUE, this, this); 121 // //} 122 // //else 123 // //{ 124 // // // activate this view 125 // // pDesktopWnd->SetActiveView(this); 126 // //} 127 //} 128 } 129 return nResult;