CDockablePane类的使用.doc
-CDockablePane类的使用目录CDockablePane与对话框类联合使用方法2CDockablePane与CTreeCtrl类联合使用的方法2CDockablePane同CFormView类的联合使用方法2两个带图标的CDockablePane派生类与CFormView类的联合使用方法2CDockablePane类的布局设计2CDockablePane窗口风格设置2去除VS2021风格的框架菜单2在View视图类中使用CSplitterWnd来拆分视图窗口不在CMainFrame类的虚函数OnCreateClient中拆分视图框架窗口2制止dockablepane停靠浮动拖动2VS2021菜单修改不了问题2VS2021 DockablePane 每次翻开都自动隐藏2EnableDocking函数解释2CDockablePane与对话框类联合使用方法1、新建对话框资源:新建一个对话框资源IDD_DIALOG_DOCKDLG,Style设为Child,Border设为None;把默认的OK和Cancel按键去掉,因为一般情况下点击这两个按钮后,对话框会销毁,而这里是不需要销毁的如果没去掉点击了,悬浮框中的对话框容就不能用了;如下添加一个按钮IDC_BUTTON_TEST,后为对话框添加类CTestDlg;为按钮添加响应void CTestDlg:OnBnClickedButtonTest() / TODO: Add your control notification handler code here MessageBo*(_T("Hello World!");2、派生CDockablePane类添加继承自CDockablePane的类CMyPane添加此类的WM_CREATE和WM_SIZE消息响应,并添加上面对话框的成员变量CTestDlg m_TestDlg;在CMyPane:OnCreate和CMyPane:OnSize函数中添加代码int CMyPane:OnCreate(LPCREATESTRUCT lpCreateStruct) if (CDockablePane:OnCreate(lpCreateStruct) = -1) return -1; / TODO: Add your specialized creation code here CRect rectDummy; rectDummy.SetRectEmpty(); / 创立选项卡窗口: if (!m_TestDlg.Create(IDD_DIALOG_DOCKDLG,this) TRACE0("未能创立输出选项卡窗口/n"); return -1; / 未能创立 m_TestDlg.ShowWindow(SW_SHOW); return 0;void CMyPane:OnSize(UINT nType, int c*, int cy) CDockablePane:OnSize(nType, c*, cy); / TODO: Add your message handler code here / 选项卡控件应覆盖整个工作区m_TestDlg.SetWindowPos (this, -1, -1, c*, cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER); m_TestDlg.ShowWindow(SW_SHOW);3、修改MainFrame类然后在MainFrame.h代码中添加成员变量CMyPane m_MyPane;在CMainFrame:OnCreate函数中添加代码if (!m_wndStatusBar.Create(this) TRACE0("Failed to create status barn"); return -1; / fail to createm_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT);if (!m_MyPane.Create(_T("MyPane"), this, CRect(0, 0, 100, 100), TRUE, IDD_DIALOG_DOCKDLG, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI) TRACE0("未能创立输出窗口/n"); return FALSE;m_MyPane.EnableDocking(CBRS_ALIGN_ANY);/ TODO: Delete these five lines if you don't want the toolbar and menubar to be dockablem_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY);m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockPane(&m_wndMenuBar);DockPane(&m_wndToolBar);DockPane(&m_MyPane); / 调整m_MyPane的大小使之适合父窗口4、运行结果5、补充 CDockablePane派生类销毁时,对话框也销毁掉没有试验void ControlPanel:OnDestroy() CDockablePane:OnDestroy(); / TODO: Add your message handler code herem_dlg.DestroyWindow(); CDockablePane派生类对象的Create函数里面的666是这个停靠栏的ID,这里是随便指定的一个数值,只要不和其他已用资源重复即可,真正应用的时候,以在字符串表中添加一个ID,前面使用的是对话框的IDD m_Panel.Create(_T("tset"),this,CRect(0,0,300,300),TRUE,666,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI);CDockablePane与CTreeCtrl类联合使用的方法1、从CDockablePane继承,创立一个自定义类avView avView.h的代码*pragma once/ avViewclass avView : public CDockablePane DECLARE_DYNAMIC(avView)public: avView(); virtual avView();protected: af*_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); af*_msg void OnSize(UINT nType, int c*, int cy);public:CTreeCtrl m_wndClassView;void AdjustLayout();protected: DECLARE_MESSAGE_MAP(); avView.cpp的代码/ NavView.cpp : implementation file/*include "stdaf*.h"*include "MDISample.h"*include "NavView.h"/ avViewIMPLEMENT_DYNAMIC(avView, CDockablePane)avView:avView()avView:avView()BEGIN_MESSAGE_MAP(avView, CDockablePane) ON_WM_CREATE() ON_WM_SIZE()END_MESSAGE_MAP()void avView:OnSize(UINT nType, int c*, int cy) CDockablePane:OnSize(nType, c*, cy);AdjustLayout();void avView:AdjustLayout() if (GetSafeHwnd() = NULL) return; CRect rectClient; GetClientRect(rectClient); int cyTlb = 3;m_wndClassView.SetWindowPos(NULL, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER);/ avView message handlersint avView:OnCreate(LPCREATESTRUCT lpCreateStruct) CRect rectDummy; rectDummy.SetRectEmpty(); / Create views: const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; if (!m_wndClassView.Create(dwViewStyle, rectDummy, this, 2) TRACE0("Failed to create Class View/n"); return -1; / fail to create HTREEITEM hRoot = m_wndClassView.InsertItem(_T("FakeApp classes"), 0, 0); m_wndClassView.SetItemState(hRoot, TVIS_BOLD, TVIS_BOLD); HTREEITEM hClass = m_wndClassView.InsertItem(_T("CFakeAboutDlg"), 1, 1, hRoot); m_wndClassView.InsertItem(_T("CFakeAboutDlg()"), 3, 3, hClass); m_wndClassView.E*pand(hRoot, TVE_E*PAND); hClass = m_wndClassView.InsertItem(_T("CFakeApp"), 1, 1, hRoot); m_wndClassView.InsertItem(_T("CFakeApp()"), 3, 3, hClass); m_wndClassView.InsertItem(_T("InitInstance()"), 3, 3, hClass); m_wndClassView.InsertItem(_T("OnAppAbout()"), 3, 3, hClass); hClass = m_wndClassView.InsertItem(_T("CFakeAppDoc"), 1, 1, hRoot); m_wndClassView.InsertItem(_T("CFakeAppDoc()"), 4, 4, hClass); m_wndClassView.InsertItem(_T("CFakeAppDoc()"), 3, 3, hClass); m_wndClassView.InsertItem(_T("OnNewDocument()"), 3, 3, hClass); hClass = m_wndClassView.InsertItem(_T("CFakeAppView"), 1, 1, hRoot); m_wndClassView.InsertItem(_T("CFakeAppView()"), 4, 4, hClass); m_wndClassView.InsertItem(_T("CFakeAppView()"), 3, 3, hClass); m_wndClassView.InsertItem(_T("GetDocument()"), 3, 3, hClass); m_wndClassView.E*pand(hClass, TVE_E*PAND); hClass = m_wndClassView.InsertItem(_T("CFakeAppFrame"), 1, 1, hRoot); m_wndClassView.InsertItem(_T("CFakeAppFrame()"), 3, 3, hClass); m_wndClassView.InsertItem(_T("CFakeAppFrame()"), 3, 3, hClass); m_wndClassView.InsertItem(_T("m_wndMenuBar"), 6, 6, hClass); m_wndClassView.InsertItem(_T("m_wndToolBar"), 6, 6, hClass); m_wndClassView.InsertItem(_T("m_wndStatusBar"), 6, 6, hClass); hClass = m_wndClassView.InsertItem(_T("Globals"), 2, 2, hRoot); m_wndClassView.InsertItem(_T("theFakeApp"), 5, 5, hClass); m_wndClassView.E*pand(hClass, TVE_E*PAND); return 0;2、修改MainFrm.cpp OnCreate方法最后参加如下代码CString str;str.LoadString(ID_VIEW_NAV);if (!m_Nav.Create(str, this, CRect(0, 0, 200, 200), TRUE, ID_VIEW_NAV, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI) TRACE0("Failed to create navigate window/n"); return FALSE; / failed to createm_Nav.EnableDocking(CBRS_ALIGN_ANY);DockPane(&m_Nav);/ ID_VIEW_NAV是自定义的字符串资源CDockablePane同CFormView类的联合使用方法1、CFormView简介 MFC提供了一个名为CFormView的特殊视图类,我们称其为表单视图表单视图是指用控件来输入和输出数据的视图,用户可以方便地在表单视图中使用控件表单视图具有对话框和滚动视图的特性,它使程序看起来象是一个具有滚动条的对话框在有些情况下,用表单视图比用普通视图更符合用户的需要,例如,在向数据库输入数据时,显然用表单的形式可以更习惯些。用AppWizard可以方便地创立基于表单视图的应用程序,只要在MFC AppWizard对话框的第六步先选择CView,然后在Base class栏中选择CFormView,AppWizard就会创立一个基于CFormView的应用程序读者可以按上述方法建立一个名为Test的应用程序在Test工程的资源中,读者会发现一个ID为IDD_TEST_FORM的对话框模板,该对话框模板可供用户放置和安排控件在程序运行时,框架根据该对话框模板创立CFormView对象,并根据模板的信息在表单视图中自动创立控件与设计对话框类相类似,用户可以用ClassWizard为表单视图类参加与控件对应的成员变量,可以调用UpdateData在控件和成员变量之间交换数据,但对控件的初始化工作是在OnInitialUpdate函数而不是在OnInitDialog函数中进展的。基于表单视图的应用程序与基于对话框的应用程序都是在应用程序中直接使用控件,但二者有很多不同之处基于对话框的应用程序是用一个对话框来作为程序的主窗口的,因而程序的主窗口的特性与对话框类似,如窗口的大小不能改变,程序没有菜单条、工具条和状态栏等。基于表单视图的应用程序仍然是基于Doc/View框架构造的,只是视图被换成了表单视图,也就是说,应用程序的窗口可以改变大小,程序有菜单条、工具条和状态栏,且程序仍然可以Dov/View运行机制来处理文档。2、添加CDockablePane的派生类CDock,形式同前述。3、添加CFormView的派生类。利用类向导,添加一个基类为CFormView的类CFrmView。为CFrmView分别重载Create函数和OnMouseActivate函数,前者是为了把Create函数重载为public,一行代码都不用写,后者是为了防止CDockablePane处于悬浮状态时程序崩溃不重载必然崩溃!int CFrmView:OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)/ TODO: 在此添加消息处理程序代码和/或调用默认值int nResult = 0;CFrameWnd* pParentFrame = GetParentFrame();if( pParentFrame = pDesktopWnd )/ When this is dockednResult= CFormView:OnMouseActivate(pDesktopWnd, nHitTest, message);else/ When this is not dockedBOOL isMiniFrameWnd =pDesktopWnd->IsKindOf(RUNTIME_CLASS(CMiniFrameWnd);BOOL isPaneFrameWnd = pDesktopWnd->IsKindOf(RUNTIME_CLASS(CPaneFrameWnd);BOOL isMultiPaneFrameWnd =pDesktopWnd->IsKindOf(RUNTIME_CLASS(CMultiPaneFrameWnd);/ pDesktopWnd is the frame window for CDockablePanenResult = CWnd:OnMouseActivate( pDesktopWnd, nHitTest, message );return nResult;4、向CDockablePane装东西在Dock.h添加protected类型的指针:CFrmView *m_frmview在构造函数中添加m_frmview = (CFrmView*)(RUNTIME_CLASS(CFrmView)->CreateObject();重载OnCreate函数和OnSize函数,前者是为初始化,后者是为设置frmview在dock中的显示大小,一般填充满。int CDock:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CDockablePane:OnCreate(lpCreateStruct) = -1)return -1;/ TODO: 在此添加您专用的创立代码RECT rect;GetClientRect(&rect);m_frmview->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, rect, this, 0, NULL);return 0;void CDock:OnSize(UINT nType, int c*, int cy)CDockablePane:OnSize(nType, c*, cy);/ TODO: 在此处添加消息处理程序代码if (GetSafeHwnd() = NULL)return;if(m_frmview->GetSafeHwnd()!=NULL)CRect rect;GetClientRect(rect);m_frmview->SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(),SWP_NOACTIVATE | SWP_NOZORDER);两个带图标的CDockablePane派生类与CFormView类的联合使用方法1、利用类向导,添加两个基类为CDockablePane的类CDock1,CDock2。2、在MainFrm.h中添加Dock1.h和Dock2.h的头文件;并派生两个子类protected:CDock1 m_dock1;CDock2 m_dock2;CDockablePane* m_pTabbedBar;3、在MainFrm的OnCreate()里面添加:最好接着EnableAutoHidePanes(CBRS_ALIGN_ANY);这句后面添加if(!m_dock1.Create("Dock1",this,CRect(0,0,300,300),true,IDD_FRMVIEW,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI)TRACE0("Failed to create dock1 windown");return FALSE;if(!m_dock2.Create("Dock2",this,CRect(0,0,300,300),true,IDD_FRMVIEW1,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI)TRACE0("Failed to create dock2 windown");return FALSE;BOOL bHiColorIcons=TRUE; /下面这几句用于添加标签的图标,IDR_SHJKTYPE 为图标IDHICON hdock1=(HICON) :LoadImage(:Af*GetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons " IDR_SHJKTYPE : IDR_SHJKTYPE), IMAGE_ICON, :GetSystemMetrics(SM_C*SMICON), :GetSystemMetrics(SM_CYSMICON), 0);m_dock1.SetIcon(hdock1, FALSE);HICON hdock2=(HICON) :LoadImage(:Af*GetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons " IDR_SHJKTYPE : IDR_SHJKTYPE), IMAGE_ICON, :GetSystemMetrics(SM_C*SMICON), :GetSystemMetrics(SM_CYSMICON), 0);m_dock2.SetIcon(hdock2, FALSE);m_dock1.EnableDocking(CBRS_ALIGN_ANY); /设置可停靠,ANY还可替换成NOALIGN,LEFT,RIGHT,TOP,BOTTOM等m_dock2.EnableDocking(CBRS_ALIGN_ANY);DockPane(&m_dock1);DockPane(&m_dock2);m_pTabbedBar = NULL;/将两个dock合成一个窗口,二者通过标签切换m_dock1.AttachToTabWnd(&m_dock2, DM_SHOW, FALSE, &m_pTabbedBar);2、写CFormView类利用类向导,添加两个基类为CFormView的类CFrmView,CFrmView1;为CFrmView和CFrmView1分别重载Create函数和OnMouseActivate函数,前者是为了把Create函数重载为public,一行代码都不用写,后者是为了防止CDockablePane处于悬浮状态时程序崩溃不重载必然崩溃!int CFrmView:OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)/ TODO: 在此添加消息处理程序代码和/或调用默认值int nResult = 0;CFrameWnd* pParentFrame = GetParentFrame();if( pParentFrame = pDesktopWnd )/ When this is dockednResult = CFormView:OnMouseActivate(pDesktopWnd, nHitTest, message);else/ When this is not dockedBOOL isMiniFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CMiniFrameWnd ) );BOOL isPaneFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CPaneFrameWnd ) );BOOL isMultiPaneFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CMultiPaneFrameWnd ) );/ pDesktopWnd is the frame window for CDockablePanenResult = CWnd:OnMouseActivate( pDesktopWnd, nHitTest, message );/return CFormView:OnMouseActivate(pDesktopWnd, nHitTest, message);return nResult;3、向CDockablePane装东西在Dock1.h和Dock2.h中添加protected类型的指针:CFrmView *m_frmview;Dock1.hCFrmView1 *m_frmview;Dock2.h接下来以Dock1.cpp为例说,Dock2.cpp一样,但是注意指针别指重了,在构造函数中添加m_frmview = (CFrmView*)(RUNTIME_CLASS(CFrmView)->CreateObject();重载OnCreate函数和OnSize函数,前者是为初始化,后者是为设置frmview在dock中的显示大小,一般填充满int CDock1:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CDockablePane:OnCreate(lpCreateStruct) = -1)return -1;/ TODO: 在此添加您专用的创立代码RECT rect;GetClientRect(&rect);m_frmview->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, rect, this, 0, NULL);return 0;void CDock1:OnSize(UINT nType, int c*, int cy)CDockablePane:OnSize(nType, c*, cy);/ TODO: 在此处添加消息处理程序代码if (GetSafeHwnd() = NULL)return;if(m_frmview->GetSafeHwnd()!=NULL)CRect rect;GetClientRect(rect);m_frmview->SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOACTIVATE | SWP_NOZORDER);CDockablePane类的布局设计1. CFrameWndE* 在框架类的头文件中定义一个CDockablePane的数组CDockablePane m_Panes5;/ 一个CDockablePane的数组2. CFrameWndE*: OnCreate() 在Create函数中自动生成了以下代码,对MFC比拟熟悉的这里就不讲了CMFCPopupMenu:SetForceMenuFocus(FALSE);InitUserToolbars(NULL, uiFirstUserToolBarId, uiLastUserToolBarId);EnablePaneMenu(TRUE, ID_VIEW_CUSTOMIZE, 0, ID_VIEW_TOOLBAR);CDockingManager:SetDockingMode(DT_SMART);EnableAutoHidePanes(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);第一种情况if (!m_Panes0.Create(_T("Pane 0"), this, CRect(0, 0, 200, 100), TRUE, 1000,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI)return FALSE;m_Panes0.EnableDocking(CBRS_ALIGN_ANY);DockPane(&m_Panes0);/ LEFT第二种情况if (!m_Panes0.Create(_T("Pane 0"), this, CRect(0, 0, 200, 100), TRUE, 1000,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI)return FALSE;if (!m_Panes1.Create(_T("Pane 1"), this, CRect(0, 0, 200, 100), TRUE, 1001,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |WS_CLIPCHILDREN | CBRS_BOTTOM | CBRS_FLOAT_MULTI)return FALSE;m_Panes0.EnableDocking(CBRS_ALIGN_ANY);m_Panes1.EnableDocking(CBRS_ALIGN_ANY);DockPane(&m_Panes1);/ BOTTOM先后顺序决定谁占主导DockPane(&m_Panes0);/ LEFT第三种情况if (!m_Panes0.Create(_T("Pane 0"), this, CRect(0, 0, 200, 100), TRUE, 1000,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI)return FALSE;if (!m_Panes1.Create(_T("Pane 1"), this, CRect(0, 0, 200, 100), TRUE, 1001,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |WS_CLIPCHILDREN | CBRS_BOTTOM | CBRS_FLOAT_MULTI)return FALSE;m_Panes0.EnableDocking(CBRS_ALIGN_ANY);m_Panes1.EnableDocking(CBRS_ALIGN_ANY);DockPane(&m_Panes0);/ LEFT先后顺序决定谁占主导DockPane(&m_Panes1);/ BOTTOM第四种情况if (!m_Panes0.Cr