代码之家  ›  专栏  ›  技术社区  ›  stanigator

MFC:禁用SDI应用程序中的新文件和保存文件功能

  •  2
  • stanigator  · 技术社区  · 15 年前

    在代码中使用命令以编程方式禁用应用程序中这两个功能的最简单方法是什么?事先谢谢。

    3 回复  |  直到 13 年前
        1
  •  4
  •   Alan    15 年前

    您可以处理更新UI消息:

    ON_UPDATE_COMMAND_UI(ID_FILE_NEW, OnUpdateFileNew)
    ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
    
    ...
    
    void CMainFrame::OnUpdateFileNew(CCmdUI *pCmdUI)
    {
        pCmdUI->Enable( FALSE );
    }
    
    void CMainFrame::OnUpdateFileSave(CCmdUI *pCmdUI)
    {
        pCmdUI->Enable( FALSE );
    }
    
        2
  •  1
  •   Jason Plank Maksim Kondratyuk    13 年前

    重写 CWinApp::OnFileNew 有自己的功能,如下所示。

    BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
        ON_COMMAND(ID_APP_ABOUT, &CMyApp::OnAppAbout)
        // Standard file based document commands
        **//ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)**
        ON_COMMAND(ID_FILE_NEW, &CMyApp::OnFileNew)
        ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
        // Standard print setup command
        ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinApp::OnFilePrintSetup)
    END_MESSAGE_MAP()
    
    
    void CMyApp::OnFileNew()
    {
             //Create a static member variable to hold the state. For the first time create a docment. From next time avoid calling CWinApp::OnFileNew();
        if( m_bDocCreated == FALSE )
        {
            CString strMsg;
            strMsg.Format( L"Create New DOC" );
            AfxMessageBox( strMsg );
    
            CWinApp::OnFileNew();
            m_bDocCreated = TRUE;
        }
        else
        {
            CFrameWnd* pFrame = (CFrameWnd*)AfxGetMainWnd();
            CMyDoc* pDoc = (CMyDoc*)pFrame->GetActiveDocument();
            CString strMsg;
            strMsg.Format( L"Doc ID = %ld",pDoc->m_lIndex );
            AfxMessageBox( strMsg );
    
        }
    
    
    }
    
        3
  •  -1
  •   Traveling Tech Guy    15 年前

    呼叫 CMenu::EnableMenuItem 带有适当的菜单项和 MF_DISABLED 作为第二个参数。这里是 documentation .