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

}




固定:

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

最佳答案

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

但是,如果是这种情况,那么您也将没有任何视图,并且该视图是-我假设-您在问题中显示的CMDIView类。

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

因此,在您的CMainFrame中(以伪代码),

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


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

关于c++ - 测试打开的文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20877317/

10-10 13:20