How to bypass the GUI in an MFC application if command line options exist?

I have an existing MFC application that the user indicates the input file, the output file, and then the Process button. I would just like to add the ability for input / output files to be command line parameters. But, if they exist, I do not want the GUI to be displayed. I just want the Process to run. I see where I can get the command line options (m_lpCmdLine), but how can I get around the GUI display? If I enter the application, it goes directly to winmain.cpp and displays a graphical interface without entering any of my code.

+5
source share
1 answer

MFC sets the class, which will be called the C [your application name] App (for example, CExampleApp) to [your application name] .h / .cpp (for example, Example.h / .cpp). Here you will have a function called "InitInstance" (MFC is automatically generated again). If you created a dialogue-based application, you will have some code that looks like this:

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
}

In particular, calling "dlg.DoModal ()" will call up your dialog box. If you avoid this, the GUI will never start.

If you use the MDI application, you will have the following code:

// 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();

This creates and displays your main window. Avoid this and the window will not be created. However, you SHOULD return FALSE from the InitInstance function, although either it will enter the application message pump.

+3
source

All Articles