C++ 打开文档的测试

C++ 打开文档的测试,c++,mfc,C++,Mfc,我正在编写一个简单的绘画应用程序,并在状态栏中添加了坐标显示。我只想在有打开的文档时显示它。启动程序时,它会显示空闲消息Ready 我将使用什么函数来测试打开的文档 这是我的OnMouseMove()处理程序: void CMDIView::OnMouseMove(UINT nFlags, CPoint point) { // Define a Device Context object for the view CClientDC aDC(this);

我正在编写一个简单的绘画应用程序,并在状态栏中添加了坐标显示。我只想在有打开的文档时显示它。启动程序时,它会显示空闲消息
Ready

我将使用什么函数来测试打开的文档

这是我的
OnMouseMove()
处理程序:

void CMDIView::OnMouseMove(UINT nFlags, CPoint point)
{
  // Define a Device Context object for the view
  CClientDC aDC(this);                                                 // DC is for this view

  // Verify the left button is down and mouse messages captured
  if((nFlags & MK_LBUTTON) && (this == GetCapture()))                  
  {
    m_SecondPoint = point;                                             // Save the current cursor position
    if(m_pTempElement)
    {
      // An element was created previously
      if(ElementType::CURVE == GetDocument()->GetElementType())        // A curve?
      {  // We are drawing a curve so add a segment to the existing curve
         std::static_pointer_cast<CCurve>(m_pTempElement)->AddSegment(m_SecondPoint);
         m_pTempElement->Draw(&aDC);                                   // Now draw it
         return;                                                       // We are done
      }
      else
      {
        // If we get to here it's not a curve so
        // redraw the old element so it disappears from the view
        aDC.SetROP2(R2_NOTXORPEN);                                     // Set the drawing mode
        m_pTempElement->Draw(&aDC);                                    // Redraw the old element to erase it
      }
    }

    // Create a temporary element of the type and color that
    // is recorded in the document object, and draw it
    m_pTempElement.reset(CreateElement());                             // Create a new element
    m_pTempElement->Draw(&aDC);                                        // Draw the element
  }


  {       //Coordinates display
      CString s;
      s.Format(L"X=%d Y=%d", point.x, point.y);
      CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
      CStatusBar* pStatus = &pFrame->m_wndStatusBar;
      pStatus->SetPaneText(0, s);
  }

}

如果没有打开的文档,则调用应返回
NULL

但是,如果是这种情况,那么您也将没有视图,并且视图是您在问题中显示的
CMDIView

也许一种替代方法是从
CMainFrame
实例而不是从视图处理状态栏文本的显示

因此(在伪代码中)在
CMainFrame

if (MDIGetActive() == NULL)
    // display "Ready"
else
   // ask current view for the text

另一种选择可能是捕获
CDocument
的销毁,并将状态栏文本重置为“就绪”。正如@Edward所指出的,让主框架处理文本显示,并让它根据当前视图是否存在和/或是否希望提供文本来决定是否设置文本本身,这样会更安全,封装也更好。

您可以使用以下代码:

CMDIChildWnd* pActiveChild = pFrame->MDIGetActive();
if (pActiveChild && pActiveChild->GetActiveDocument())
{    // one or more documents open
}

这是“标准”MFC应用程序吗?SDI?MDI?这是一个MFC MDI应用程序。谢谢。我决定在销毁当前CD文档的同时更改状态<代码>CMDIDoc::~CMDIDoc(){CString Idle=LPCTSTR(AFX_IDS_IDLEMESSAGE);//Idle=LPCTSTR(L“lawlawl”);cminframe*pFrame=(cminframe*)AfxGetApp()->m_pMainWnd;CStatusBar*pStatus=&pFrame->m_wndStatusBar;pStatus->SetPaneText(0,Idle)}设置销毁
CDocument的状态栏有点风险,您可以有多个文档open@EdwardClements是的,我同意-可能最好在主框架类中控制状态栏文本,并从文档DTOR通知,然后让主框架决定要做什么,就像在我的第一个备选方案中一样。
CMDIChildWnd* pActiveChild = pFrame->MDIGetActive();
if (pActiveChild && pActiveChild->GetActiveDocument())
{    // one or more documents open
}