15,980
社区成员




/*
参考:
https://wenku.baidu.com/view/1109160002020740be1e9b29.html
http://blog.csdn.net/fengyanhui/article/details/5993104
*/
void CMainFrame::CreateMoreViews()
{
m_pViewList[0] = (CMyReportView*)GetActiveView();
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CSRView);
context.m_pCurrentDoc = m_pViewList[0]->GetDocument();
context.m_pNewDocTemplate = NULL;
context.m_pLastView = NULL;
context.m_pCurrentFrame = this;
m_pViewList[1] = (CMyReportView*)CreateView(&context);
m_pViewList[1]->OnInitialUpdate();
m_pViewList[1]->ShowWindow(SW_HIDE);
}
void CMainFrame::OnShowAview()
{
m_pViewList[0]->ShowWindow(SW_SHOW);
m_pViewList[1]->ShowWindow(SW_HIDE);
SetActiveView(m_pViewList[0]);
RecalcLayout();
}
void CMainFrame::OnShowBview()
{
m_pViewList[0]->ShowWindow(SW_HIDE);
m_pViewList[1]->ShowWindow(SW_SHOW);
SetActiveView(m_pViewList[1]);
RecalcLayout();
}
void CMainFrame::ShowBetterView(const int viewIndex)
{
if (m_curViewIndex == viewIndex)
return;
m_pViewList[m_curViewIndex]->ShowWindow(SW_HIDE);
m_curViewIndex = viewIndex;
CMyReportView *pView = m_pViewList[m_curViewIndex]; // 两个view都是从我从CListView派生出来的CMyReportView派生出来的
pView->ShowWindow(SW_SHOW);
SetActiveView(pView);
SetWindowLong(pView->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST);
RecalcLayout();
}
void CMainFrame::OnShowAview()
{
ShowBetterView(0);
}
void CMainFrame::OnShowBview()
{
ShowBetterView(1);
}
以上代码只不过是把两个函数中的写到了一个,逻辑完全没有变化。
检查了几遍,也看不出问题。
由于后面加了一个SetWindowLong(),所以看起来是第一个虽然隐藏,但是没有把它的GWL_ID设置成其它的,所以可能会有影响,于是在CMyReportView的OnCreate()获了GWL_ID,结果两个view的值都是 AFX_IDW_PANE_FIRST!
然后对ShowBetterView()添加一行代码:
...
m_pViewList[m_curViewIndex]->ShowWindow(SW_HIDE);
SetWindowLong(m_pViewList[m_curViewIndex]->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST + m_curViewIndex + 1); // 这一行新添加的
m_curViewIndex = viewIndex;
...
添加之后,再测试,又恢复正常了。估计这次是真把问题解决了,如果以后没有再回贴,那应该就是没有再出现过这个情况了void CMainFrame::OnShowBview()
{
m_pViewList[0]->ShowWindow(SW_HIDE);
m_pViewList[1]->ShowWindow(SW_SHOW);
SetActiveView(m_pViewList[1]);
SetWindowLong(m_pViewList[1]->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST); // 这一行是参考新盒子添加的
RecalcLayout();
}
// CMainFrame message handlers
void CMainFrame::SwitchViews(int ID)
{
CView* pOldActiveView = GetActiveView();
CView* pNewActiveView;
CCreateContext cc;
cc.m_pCurrentDoc=pOldActiveView->GetDocument();
CString title=cc.m_pCurrentDoc->GetPathName();;
// which one
switch (ID)
{
case 1://
pNewActiveView=(CView*) new CWinOh51View;
title += " Binary View";
break;
case 2://
pNewActiveView=(CView*) new CFormatView;
title += " Formated View";
break;
case 3://
pNewActiveView=(CView*) new CFlashView;
title += " Flash View";
break;
case 4://
pNewActiveView=(CView*) new CHexView;
title += " Hex View";
break;
}
// Create new
pNewActiveView->Create(0,0,0,CFrameWnd::rectDefault,this,ID,&cc);
pNewActiveView->OnInitialUpdate();
SetActiveView(pNewActiveView);
pNewActiveView->ShowWindow(SW_SHOW);
// Exchange view's window ID so RecalcLayout() works.
SetWindowLong(pNewActiveView->m_hWnd, GWL_ID,AFX_IDW_PANE_FIRST);
RecalcLayout();
//If we use: delete pOldActiveView,
//we got : Warning: calling DestroyWindow in CWnd::~CWnd,
//OnDestroy or PostNcDestroy in derived class will not be called.
pOldActiveView->DestroyWindow();
// new title
SetWindowText(title);
}
//
void CMainFrame::OnSwitchViews()
{
// TODO: Add your command handler code here
CView *pOldView=(CView *)GetActiveView();
//CHexView
if(pOldView->IsKindOf(RUNTIME_CLASS(CWinOh51View)))
{
SwitchViews(2);//CFormatView
}
else if (pOldView->IsKindOf(RUNTIME_CLASS(CFormatView)))
{
SwitchViews(3);//CFlashView
}
else if (pOldView->IsKindOf(RUNTIME_CLASS(CFlashView)))
{
SwitchViews(4);//CHexView
}
else //(pOldView->IsKindOf(RUNTIME_CLASS(CHexView)))
{
SwitchViews(1);//CWinOh51View
}
}