【问题标题】:MFC SDI GetActiveView() always returns NULLMFC SDI GetActiveView() 总是返回 NULL
【发布时间】:2017-11-03 18:02:03
【问题描述】:

我需要在我的 MFC SDI C++ 项目中切换 CFormViews 的帮助。我已经挖掘了很长时间,无法弄清楚为什么我的代码不起作用。通过搜索互联网(包括这个网站),我发现了几个通过向 MainFrm.cpp(继承自 CFrameWndCMainFrame 对象)添加两个函数来切换表单的教程。其中一个传递了我要切换到的表单的 id,然后获取指向活动视图的指针,并从那里运行一些其他代码。但是,GetActiveView() 始终返回 NULL 指针值。我知道有一个活动视图,因为我正在单击活动表单中的按钮。我的代码如下。这只是我所指的功能。它位于 MainFrm.cpp(启动新 MFC 项目时创建的默认窗口文件)中。

到目前为止,我已经尝试了 Microsoft 知识库中有关如何从 Anywhere 获取当前 CDocumentCView 的代码,我尝试先获取活动框架,然后从 @987654330 调用 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();
    }
}

【问题讨论】:

  • MFC Doc/View:如何获取指向各种对象的指针?link.一个很好的概述,这张表中的一个应该会有所帮助..!
  • 如果返回 NULL,则其他窗格、拆分器等可能处于活动状态。但是您仍然可以从现有的 CDocument 中获取关联的 CView。
  • 谢谢您,但我已经尝试过您提供的链接。我试过了,从 CMainFrame 获取 CView (这是上面代码的一部分)。其他窗格和拆分器将如何处于活动状态?我正在使用仅打开一个窗口和一个表单的默认 SDI 代码(无论向导设置的默认表单是什么)。如果我需要从 MainFrame 中访问 CView,我将如何从 CDocument 文件中获取视图?我会创建一个 CDocument 对象,然后从其中调用一个函数吗?感谢您的帮助!
  • 没有活动视图,但您确定有工作视图?首先尝试从您的工作按钮表单中设置 SetActiveView(..myView..)。

标签: c++ mfc sdi


【解决方案1】:

要从 CDocument 获取非活动视图但关联的 CView,您可以在 Doc 中实现此架构

// ----- 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)
  :   
}

【讨论】:

  • 您说要在我的 SelectView 代码中添加上述代码。上面的代码看起来像一个新函数(SwitchToView)。我很困惑。我应该在 MainFrm.cpp 中保留我的原始 SelectView 代码并在我的文档文件中添加一个 SwitchToView 函数吗?
  • 我会稍微解释一下我的设置。我的项目名称是 Project2,所以当我最初设置我的 mfc sdi 程序时,它使用 Project2.h/cpp、Project2Doc.h/cpp 和 Project2View.h/cpp 设置它。还有 MainFrm.h/cpp。在附加到 Project2View.h/cpp 的默认对话框(它源自 CFormView)上,我添加了一些按钮。我添加了更多从 CRecordView 派生的对话框。它们每个都有一个与之关联的 .h/cpp 文件。当我单击默认对话框(在应用程序运行时加载的对话框)上的按钮时,我的代码会创建一个新的 CMainFrame...
  • ...object 并将我想要更改的视图的 id 传递给它。我将要更改为的视图的 id 传递给 MainFrm 构造函数,该构造函数将 id 传递给 SelectView 函数。也许这是我做错的部分。当我按下默认对话框视图 (Project2View.cpp) 上的按钮时,我应该有什么代码?我是一个非常聪明的女人,但这让我很难过。我显然错过了一些东西。
  • 为什么要添加一个框架?一个应用程序通常只有一个 CMainframe、一个文档和 1..n 个视图。
  • 无需新建CMainframe。只需在您的 CView Button 处理程序中调用 SelectView() 函数: (CMainframe*)AfxGetMainWnd()->SelectView(..) 您的代码就可以工作了。
【解决方案2】:

以下代码对我有用:

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;
}

希望对你有帮助。

【讨论】:

  • 这是 MDI 接口的代码。 OP 在 SDI 应用程序中存在问题。
猜你喜欢
  • 2014-03-04
  • 2016-11-02
  • 2014-05-06
  • 2012-06-05
  • 2014-05-30
  • 2014-02-23
  • 2017-10-12
  • 2013-08-16
  • 2016-03-28
相关资源
最近更新 更多