MFC SDI GetActiveView() 总是 returns NULL
MFC SDI GetActiveView() always returns NULL
我需要帮助在我的 MFC SDI C++ 项目中切换 CFormViews
。我已经挖掘了很长时间,无法弄清楚为什么我的代码不起作用。通过搜索互联网(包括本网站),我发现了几个通过向 MainFrm.cpp(继承自 CFrameWnd
的 CMainFrame
对象)添加两个函数来切换表单的教程。其中之一传递了我要切换到的表单的 ID,然后获取指向活动视图的指针,并从那里运行一些其他代码。然而,GetActiveView()
总是returns一个NULL
指针值。我知道有一个活动视图,因为我正在单击活动表单中的按钮。我的代码如下。这只是我所指的功能。它位于 MainFrm.cpp(启动新 MFC 项目时创建的默认 window 文件)。
到目前为止,我已经尝试了 Microsoft 知识库中讨论如何从任何地方获取当前 CDocument
或 CView
的代码,我尝试先获取活动帧,然后调用GetActiveView
来自 CFrameWnd
,我尝试了下面的代码。都无济于事。我显然对 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();
}
}
以下代码对我有用:
virtual CView* SwitchToView(CView* pNewView);
在 cpp 中:
CView* CMyDoc::SwitchToView(CView* pNewView)
{
CMDIFrameWndEx* pMainWnd = (CMDIFrameWndEx*)AfxGetMainWnd();
// Get the active MDI child window
CMDIChildWndEx* pChild = (CMDIChildWndEx*)pMainWnd->MDIGetActive();
// Get the active view attached to the active MDI child window.
CView* pOldActiveView = pChild->GetActiveView();
// Exchange control ID of old view
// note: if you have more than two view you have to remember which view you switched to
// so you can set it's old control ID correctly
if(pNewView == m_pMyView)
pOldActiveView->SetDlgCtrlID(CTRLID_MYVIEW2);
if(pNewView == m_pMyView2)
pOldActiveView->SetDlgCtrlID(CTRLID_MYVIEW);
// Exchange control ID of new new
// note: the control ID of the active view must always be AFX_IDW_PANE_FIRST
pNewView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
// Set flag so that document will not be deleted when view is dettached.
BOOL bAutoDelete = m_bAutoDelete;
m_bAutoDelete = FALSE;
// Dettach existing view
RemoveView(pOldActiveView);
// restore flag
m_bAutoDelete = bAutoDelete;
// Show the newly active view and hide the inactive view.
pNewView->ShowWindow(SW_SHOW);
pOldActiveView->ShowWindow(SW_HIDE);
// Attach new view
AddView(pNewView);
pChild->RecalcLayout();
pNewView->UpdateWindow();
pChild->SetActiveView(pNewView);
return pOldActiveView;
}
希望对你有所帮助
要从 CDocument 中获取非活动视图但关联的 CView,您可以在文档中实现此模式
// ----- GetCChooseItemView() -- -Search the first associated CView in INACTIVE Views too ! ------
CView* CMyDoc::GetCChooseItemView(void)
{
CRuntimeClass* prt = RUNTIME_CLASS(CChooseItemView);
CView* pView = NULL;
// Continue search in inactive View by T(o)m
POSITION pos = GetFirstViewPosition();
while (pos != NULL)
{
pView = GetNextView(pos);
if (pView->GetRuntimeClass() == prt)
{
if (pView->IsKindOf(RUNTIME_CLASS(CChooseItemView)))
break;
}
pView = NULL; // not valid vie
}
return static_cast<CChooseItemView*>(pView);
}
然后添加您的 SelectView 代码
void CMainFrame::SelectView(UINT ViewID)
{
: (code as before)
:
// Get a pointer to the current view
CView* pCurrentView = GetActiveView();
// Get a pointer to the current view
CView* pCurrentView = GetActiveView();
if (pCurrentView == NULL
{
CMyDoc* pDoc = static_cast<CMyDoc*>(GetActiveDocument());
if (pDoc)
{
pCurrentView = pDoc->GetChhoseItemView();
if (pCurrentView == NULL)
mpCurrentView = pDoc->GetCItemInformationView() // let as exercise for the OP
if (pCurrentView == NULL
{
DebugBreak(); // Errror No View found..
}
}
: (code as befeore)
:
}
我需要帮助在我的 MFC SDI C++ 项目中切换 CFormViews
。我已经挖掘了很长时间,无法弄清楚为什么我的代码不起作用。通过搜索互联网(包括本网站),我发现了几个通过向 MainFrm.cpp(继承自 CFrameWnd
的 CMainFrame
对象)添加两个函数来切换表单的教程。其中之一传递了我要切换到的表单的 ID,然后获取指向活动视图的指针,并从那里运行一些其他代码。然而,GetActiveView()
总是returns一个NULL
指针值。我知道有一个活动视图,因为我正在单击活动表单中的按钮。我的代码如下。这只是我所指的功能。它位于 MainFrm.cpp(启动新 MFC 项目时创建的默认 window 文件)。
到目前为止,我已经尝试了 Microsoft 知识库中讨论如何从任何地方获取当前 CDocument
或 CView
的代码,我尝试先获取活动帧,然后调用GetActiveView
来自 CFrameWnd
,我尝试了下面的代码。都无济于事。我显然对 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();
}
}
以下代码对我有用:
virtual CView* SwitchToView(CView* pNewView);
在 cpp 中:
CView* CMyDoc::SwitchToView(CView* pNewView)
{
CMDIFrameWndEx* pMainWnd = (CMDIFrameWndEx*)AfxGetMainWnd();
// Get the active MDI child window
CMDIChildWndEx* pChild = (CMDIChildWndEx*)pMainWnd->MDIGetActive();
// Get the active view attached to the active MDI child window.
CView* pOldActiveView = pChild->GetActiveView();
// Exchange control ID of old view
// note: if you have more than two view you have to remember which view you switched to
// so you can set it's old control ID correctly
if(pNewView == m_pMyView)
pOldActiveView->SetDlgCtrlID(CTRLID_MYVIEW2);
if(pNewView == m_pMyView2)
pOldActiveView->SetDlgCtrlID(CTRLID_MYVIEW);
// Exchange control ID of new new
// note: the control ID of the active view must always be AFX_IDW_PANE_FIRST
pNewView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
// Set flag so that document will not be deleted when view is dettached.
BOOL bAutoDelete = m_bAutoDelete;
m_bAutoDelete = FALSE;
// Dettach existing view
RemoveView(pOldActiveView);
// restore flag
m_bAutoDelete = bAutoDelete;
// Show the newly active view and hide the inactive view.
pNewView->ShowWindow(SW_SHOW);
pOldActiveView->ShowWindow(SW_HIDE);
// Attach new view
AddView(pNewView);
pChild->RecalcLayout();
pNewView->UpdateWindow();
pChild->SetActiveView(pNewView);
return pOldActiveView;
}
希望对你有所帮助
要从 CDocument 中获取非活动视图但关联的 CView,您可以在文档中实现此模式
// ----- GetCChooseItemView() -- -Search the first associated CView in INACTIVE Views too ! ------
CView* CMyDoc::GetCChooseItemView(void)
{
CRuntimeClass* prt = RUNTIME_CLASS(CChooseItemView);
CView* pView = NULL;
// Continue search in inactive View by T(o)m
POSITION pos = GetFirstViewPosition();
while (pos != NULL)
{
pView = GetNextView(pos);
if (pView->GetRuntimeClass() == prt)
{
if (pView->IsKindOf(RUNTIME_CLASS(CChooseItemView)))
break;
}
pView = NULL; // not valid vie
}
return static_cast<CChooseItemView*>(pView);
}
然后添加您的 SelectView 代码
void CMainFrame::SelectView(UINT ViewID)
{
: (code as before)
:
// Get a pointer to the current view
CView* pCurrentView = GetActiveView();
// Get a pointer to the current view
CView* pCurrentView = GetActiveView();
if (pCurrentView == NULL
{
CMyDoc* pDoc = static_cast<CMyDoc*>(GetActiveDocument());
if (pDoc)
{
pCurrentView = pDoc->GetChhoseItemView();
if (pCurrentView == NULL)
mpCurrentView = pDoc->GetCItemInformationView() // let as exercise for the OP
if (pCurrentView == NULL
{
DebugBreak(); // Errror No View found..
}
}
: (code as befeore)
:
}