我需要帮助切换
CFormViews
在我的MFC SDI C++项目中。我已经挖掘了很长时间,不知道为什么我的代码不能工作。通过搜索互联网(包括这个网站),我找到了几个通过在MainFrm中添加两个函数来切换表单的教程。cpp(a
CMainFrame
继承自的对象
CFrameWnd
). 其中一个被传递了一个我想要切换到的表单id,然后获得一个指向活动视图的指针,并从那里运行一些其他代码。然而
GetActiveView()
始终返回
NULL
指针值。我知道有一个活动视图,因为我正在单击活动窗体中的按钮。我的代码如下。这正是我所指的函数。它位于MainFrm中。cpp(启动新MFC项目时创建的默认窗口文件)。
到目前为止,我已经尝试了来自Microsoft知识库的代码,其中讨论了如何获取最新信息
CDocument
或
CView
从任何地方,我都尝试先获取活动帧,然后调用
GetActiveView
从…起
主框架
,我尝试了下面的代码。都没有用。很明显,我对MFC的了解还不够,无法解决任何问题。如果您需要我提供更多信息,请咨询。我可能没有提及我应该拥有的一切。我选择为一个学校项目进行MFC,在我知道可以使用这些表单之前,我无法继续创建UML或编写任何其他代码。
void CMainFrame::SelectView(UINT ViewID)
{
// If the view the user selected is already displaying, do nothing
if (ViewID == m_CurrentView)
return;
// Get a pointer to the current view
CView* pCurrentView = GetActiveView();
// We are about to change the view, so we need a pointer to the runtime class
CRuntimeClass* pNewView = NULL; // Added = NULL because it wouldn't allow program to be run without initialization of pNewView
// We will process a form
// First, let's change the identifier of the current view to our integer
::SetWindowLong(pCurrentView->m_hWnd, GWL_ID, m_CurrentView);
// Now we will identify what form the user selected
switch (ViewID)
{
case IDD_CHOOSE_ITEM:
pNewView = RUNTIME_CLASS(CChooseItemView);
break;
case IDD_ITEM_INFORMATION:
pNewView = RUNTIME_CLASS(CItemInformationView);
break;
}
// We will deal with the frame
CCreateContext crtContext;
// We have a new view now. So we initialize the context
crtContext.m_pNewViewClass = pNewView;
// No need to change the document. We keep the current document
crtContext.m_pCurrentDoc = GetActiveDocument();
CView* pNewViewer = STATIC_DOWNCAST(CView, CreateView(&crtContext));
// Now we can create a new view and get rid of the previous one
if (pNewViewer != NULL)
{
pNewViewer->ShowWindow(SW_SHOW);
pNewViewer->OnInitialUpdate();
SetActiveView(pNewViewer);
RecalcLayout();
m_CurrentView = ViewID;
pCurrentView->DestroyWindow();
}
}