声明:这些程序都是鄙人亲自调试通过,转载敬标注来源于酒天聊博文!
1、CDockablePane的关闭见解:
MFC提供的Pane关闭可以再事件里面找到:
BOOL CMainFrame::OnCloseDockingPane(CDockablePane* pWnd) ;
注释:关闭停靠窗口事件,事实上是隐藏窗体,还是占用资源的
BOOL CMainFrame::OnCloseMiniFrame(CPaneFrameWnd* pWnd)
注释:关闭悬浮窗口事件,事实上液是隐藏窗体,还是占用资源的
因此,如果仅仅是重写这两个消息,没做什么处理,那么内存还是消不下去,经过调试CDockablePane源码,获知如下方式可以真正销毁关闭该Dock窗体:
//停靠窗体关闭
BOOL CMainFrame::OnCloseDockingPane(CDockablePane* pWnd)
{
//处理关闭事件
if(pWnd->IsKindOf(RUNTIME_CLASS(CTabbedPane))){ //多个窗口
CTabbedPane* tabpane = (CTabbedPane *)pWnd;
CMFCBaseTabCtrl* pTabWnd = tabpane->GetUnderlyingWindow();
int num = pTabWnd->GetActiveTab();
if(num >= 0){ //这里仅仅关闭活动的面板
CDockablePane* pBar = DYNAMIC_DOWNCAST(CDockablePane, pTabWnd->GetTabWnd(num));
if (pBar != NULL)
{
ASSERT_VALID(pBar);
//将关闭消息加入该DockPane队列,等待完成本函数后关闭
::PostMessageA(pBar->m_hWnd ,WM_CLOSE, 0 , 0);
}
}
}
else{ //单个窗口
CDockablePane* pane = (CDockablePane*)pWnd;
if(pane->IsKindOf(RUNTIME_CLASS(CDockablePane)) || pane->IsKindOf(RUNTIME_CLASS(CPane)) && !pane->IsKindOf(RUNTIME_CLASS(CMFCToolBar))){
//将关闭消息加入该DockPane队列,等待完成本函数后关闭
::PostMessageA(pane->m_hWnd ,WM_CLOSE, 0 , 0);
}
}
return TRUE;
}
//关闭悬浮窗体
BOOL CMainFrame::OnCloseMiniFrame(CPaneFrameWnd* pWnd)
{
//处理关闭事件
CWnd* pwnd = pWnd->GetPane();
if(pwnd->IsKindOf(RUNTIME_CLASS(CTabbedPane))){ //多个窗口
CTabbedPane* tabpane = (CTabbedPane *)pwnd;
CMFCBaseTabCtrl* pTabWnd = tabpane->GetUnderlyingWindow();
//关闭所有的面板
for(int i=0;i<pTabWnd->GetTabsNum();i++)
{
CDockablePane* pBar = DYNAMIC_DOWNCAST(CDockablePane, pTabWnd->GetTabWnd(i));
if (pBar != NULL)
{
ASSERT_VALID(pBar);
//将关闭消息加入该DockPane队列,等待完成本函数后关闭
::PostMessageA(pBar->m_hWnd ,WM_CLOSE, 0 , 0);
}
}
}
else{ //单个窗口
CDockablePane* pane = (CDockablePane *)pwnd;
if(pane->IsKindOf(RUNTIME_CLASS(CDockablePane)) || pane->IsKindOf(RUNTIME_CLASS(CPane)) && pane->IsKindOf(RUNTIME_CLASS(CPaneFrameWnd))){
//将关闭消息加入该DockPane队列,等待完成本函数后关闭
::PostMessageA(pWnd->GetPane()->m_hWnd ,WM_CLOSE, 0 , 0);
}
}
return TRUE;
}
关闭后,如视图需要重新打开,用这句:
if(m_wndClassView.GetSafeHwnd())
{
m_wndClassView.ShowPane(TRUE,FALSE,TRUE);
return ;
}
else{
UINT Dockstyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT | CBRS_FLOAT_MULTI;
BOOL bNameValid;
CString strClassView;
bNameValid = strClassView.LoadString(IDS_CLASS_VIEW);
ASSERT(bNameValid);
if (!m_wndClassView.Create(strClassView, this, CRect(0, 0, 200, 200), TRUE, ID_VIEW_ONLINE_CLIENT_TERR_PANE, Dockstyle ,AFX_CBRS_OUTLOOK_TABS))
{
TRACE("未能创建%s窗口\n" ,strClassView);
return ;
}
m_wndClassView.EnableDocking(CBRS_ALIGN_ANY);
DockPane(&m_wndClassView);
}
问题解决了,偷着乐吧!!
2、MFC取消自动记忆界面布局的解决,重载该方法,详细的可以单步调试,看CDockPane.cpp源码:
//加载界面状态值
void CNSBClientProjectTestApp ::LoadCustomState()
{
//设置为TRUE,则自动加载界面记忆
this->m_bSaveState = FALSE;
}
//保存界面状态值
void CNSBClientProjectTestApp ::SaveCustomState()
{
//设置为TRUE,则自动保存界面记忆
this->m_bSaveState = FALSE;
}
3、取消自动记忆界面保存后,自己手动保存与加载:
//整个主窗口关闭事件(系统消息事件)
void CMainFrame::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//保存界面状态
CString strSectionPath = ::AFXGetRegPath(_T("WorkSpace"));
if(!m_dockManager.SaveState(_T(strSectionPath) ,IDR_MAINFRAME)){
MessageBox(_T("保存界面布局出错!") , "提示" , MB_OK | MB_ICONWARNING);
}
CMDIFrameWndEx::OnClose();
}
//加载保存的界面布局状态(自己加的函数),在App那边调用
BOOL CMainFrame::LoadMainFrmState()
{
CString strSectionPath = ::AFXGetRegPath(_T("WorkSpace"));
return m_dockManager.LoadState(_T(strSectionPath) ,IDR_MAINFRAME);
}