测试打开的文档

时间:2014-01-02 06:09:55

标签: c++ mfc

我正在编写一个简单的绘图应用程序,并在状态栏中添加了坐标显示。我只希望它显示何时有打开的文档。当我启动程序时,它会显示空闲消息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);
  }

}

Coordinates of Mouse

固定

CMDIDoc::~CMDIDoc()
{
    CString Idle = LPCTSTR(AFX_IDS_IDLEMESSAGE);
    //Idle = LPCTSTR(L"lawlawlwawl");
    CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
    CStatusBar* pStatus = &pFrame->m_wndStatusBar;
    pStatus->SetPaneText(0, Idle );
}

2 个答案:

答案 0 :(得分:1)

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

但是,如果是这种情况,那么你也没有视图,视图是 - 我认为 - 你在问题中显示的CMDIView类。

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

CMainFrame中的(以伪代码形式),

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

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

答案 1 :(得分:0)

您可以使用以下代码:

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