在mfc单文档项目里,程序启动后会调用一次CDocument::OnNewDocument()函数,我怎么判断他是启动时触发的还是用户自己通过单击新建触发的?
启动时的那次调用能屏蔽么?

解决方案 »

  1.   

    App类的InitInstance函数中调用了ProcessShellCommand(cmdInfo);
    看第一个case语句就明白了BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
    {
    BOOL bResult = TRUE;
    switch (rCmdInfo.m_nShellCommand)
    {
    case CCommandLineInfo::FileNew:
    if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
    OnFileNew();
    if (m_pMainWnd == NULL)
    bResult = FALSE;
    break; // If we've been asked to open a file, call OpenDocumentFile() case CCommandLineInfo::FileOpen:
    if (!OpenDocumentFile(rCmdInfo.m_strFileName))
    bResult = FALSE;
    break; // If the user wanted to print, hide our main window and
    // fire a message to ourselves to start the printing case CCommandLineInfo::FilePrintTo:
    case CCommandLineInfo::FilePrint:
    m_nCmdShow = SW_HIDE;
    ASSERT(m_pCmdInfo == NULL);
    if(OpenDocumentFile(rCmdInfo.m_strFileName))
    {
    m_pCmdInfo = &rCmdInfo;
    ENSURE_VALID(m_pMainWnd);
    m_pMainWnd->SendMessage(WM_COMMAND, ID_FILE_PRINT_DIRECT);
    m_pCmdInfo = NULL;
    }
    bResult = FALSE;
    break; // If we're doing DDE, hide ourselves case CCommandLineInfo::FileDDE:
    m_pCmdInfo = (CCommandLineInfo*)(UINT_PTR)m_nCmdShow;
    m_nCmdShow = SW_HIDE;
    break; // If we've been asked to register, exit without showing UI.
    // Registration was already done in InitInstance().
    case CCommandLineInfo::AppRegister:
    {
    Register();
    bResult = FALSE;    // that's all we do // If nobody is using it already, we can use it.
    // We'll flag that we're unregistering and not save our state
    // on the way out. This new object gets deleted by the
    // app object destructor. if (m_pCmdInfo == NULL)
    {
    m_pCmdInfo = new CCommandLineInfo;
    m_pCmdInfo->m_nShellCommand = CCommandLineInfo::AppUnregister;
    }
    break;
    } // If we've been asked to unregister, unregister and then terminate
    case CCommandLineInfo::AppUnregister:
    {
    BOOL bUnregistered = Unregister(); // if you specify /EMBEDDED, we won't make an success/failure box
    // this use of /EMBEDDED is not related to OLE if (!rCmdInfo.m_bRunEmbedded)
    {
    if (bUnregistered)
    AfxMessageBox(AFX_IDP_UNREG_DONE);
    else
    AfxMessageBox(AFX_IDP_UNREG_FAILURE);
    }
    bResult = FALSE;    // that's all we do // If nobody is using it already, we can use it.
    // We'll flag that we're unregistering and not save our state
    // on the way out. This new object gets deleted by the
    // app object destructor. if (m_pCmdInfo == NULL)
    {
    m_pCmdInfo = new CCommandLineInfo;
    m_pCmdInfo->m_nShellCommand = CCommandLineInfo::AppUnregister;
    }
    }
    break;
    }
    return bResult;
    }SDI好像是不可以屏蔽的,MDI可以
      

  2.   

    MFC的SDI框架就是这样的,没办法,除非你用MDI