【问题标题】:How do I bypass GUI in MFC app if command line options exist?如果存在命令行选项,如何绕过 MFC 应用程序中的 GUI?
【发布时间】:2011-03-05 00:38:14
【问题描述】:

我有一个现有的简单 MFC 应用程序,用户指定输入文件、输出文件,然后是“处理”按钮。我只想添加功能,以便输入/输出文件是命令行参数。但是,如果它们存在,我不希望 GUI 出现。我只想执行“流程”。我知道在哪里可以获得命令行参数 (m_lpCmdLine),但是如何绕过 GUI 的显示?如果我进入应用程序,它会直接进入 winmain.cpp 并显示 GUI,而无需进入我的任何代码。

【问题讨论】:

    标签: command-line mfc arguments


    【解决方案1】:

    MFC 在 [Your App Name].h/.cpp (eg Example.h/.cpp) 中设置一个名为 C[Your App Name]App (eg CExampleApp) 的类名为“InitInstance”的函数(同样由 MFC 自动生成)。如果您创建了一个基于对话框的应用程序,那么您将在函数中有一些如下所示的代码:

    CExampleDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with Cancel
    }
    

    特别是“dlg.DoModal()”调用将调用您的对话窗口。如果你避免这种情况,那么 GUI 将永远不会启动。

    如果您使用的是 MDI 应用程序,那么您将拥有如下代码:

    // create main MDI Frame window
    CMainFrame* pMainFrame = new CMainFrame;
    if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
        return FALSE;
    m_pMainWnd = pMainFrame;
    
    pMainFrame->ShowWindow(m_nCmdShow);
    pMainFrame->UpdateWindow();
    

    这将创建并显示您的主窗口。避免这种情况,不会创建任何窗口。但是,您必须从 InitInstance 函数返回 FALSE,否则它将进入应用程序消息泵。

    【讨论】:

    • 就我而言,我需要LoadFrame(),但即使我没有调用ShowWindow(),框架也会闪烁。我发现通过在LoadFrame() 之前调用CWinAppEx::m_bLoadWindowPlacement = false;,您不会得到任何窗口和闪烁。
    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 2022-09-23
    • 2011-07-22
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    相关资源
    最近更新 更多