MFC program running with Parameters

Source: Internet
Author: User

 

The following example can be used to determine whether the-c,-s, or-p option is used when the program is running. You can simply change the option in the specific program according to the example.

Step 1: Reload a class CWzdCommandLineInfo from CCommandLineInfo. The implementation method is as follows:

# If! Defined wzdcommandlineinfo_h <br/> # define wzdcommandlineinfo_h <br/> // wzdcommandlineinfo. h: header file <br/> ////////////////////////////////// //////////////////////////////////////// <br/> // cwzdcommandlineinfo window <br/> class cwzdcommandlineinfo: public ccommandlineinfo <br/>{< br/> // construction <br/> Public: <br/> cwzdcommandlineinfo (); <br/> // attributes <br/> Public: <br/> bool m_bcf Lag; <br/> bool m_bsflag; <br/> bool m_bpflag; <br/> cstring m_sarg; <br/> // Operations <br/> public: <br/> void parseparam (const tchar * pszparam, bool bflag, bool blast); <br/> // overrides <br/> // implementation <br/> public: <br/> virtual ~ Cwzdcommandlineinfo (); <br/> }; <br/> //////////////////////////////////// /// // <br /> # endif 

 

 

The header file ends. The following is the CPP file.

 

 

// WzdCommandLineInfo. cpp: implementation file <br/> // <br/> # include "stdafx. h "<br/> # include" WzdCommandLineInfo. h "</p> <p> # ifdef _ DEBUG <br/> # define new DEBUG_NEW <br/> # undef THIS_FILE <br/> static char THIS_FILE [] = __ FILE __; <br/> # endif <br/> ////////////////////////////// //////////////////////////////////////// //// <br/> // CWzdCommandLineInfo <br/> CWzdCommandLineInfo:: CWzdCommandLineInfo () <br/ >{< Br/> m_bCFlag = FALSE; <br/> m_bSFlag = FALSE; <br/> m_bPFlag = FALSE; <br/> m_sArg = _ T (""); <br/>}< br/> CWzdCommandLineInfo ::~ CWzdCommandLineInfo () <br/>{< br/>}< br/> ////////////////////////// //////////////////////////////////////// /////// <br/> void CWzdCommandLineInfo:: ParseParam (const TCHAR * pszParam, BOOL bFlag, <br/> BOOL bLast) <br/>{< br/> CString sArg (pszParam ); <br/> if (bFlag) <br/>{< br/> m_bCFlag =! SArg. CompareNoCase ("c"); <br/> m_bSFlag =! SArg. CompareNoCase ("s"); <br/> m_bPFlag =! SArg. compareNoCase ("p"); <br/>}< br/> // m_strFileName gets the first nonflag name <br/> else if (m_strFileName.IsEmpty ()) <br/>{< br/> m_sArg = sArg; <br/>}< br/> CCommandLineInfo: ParseParam (pszParam, bFlag, bLast); <br/>} 

Step 2: Add member variables to the APP class:

Public:

CWzdCommandLineInfo m_cmdInfo;

Step 3: Modify the following code in the InitInstance () function:

// Parse command line for standard shell commands, DDE, file open <br/> parsecommandline (m_cmdinfo ); <br/> // dispatch commands specified on the command line <br/> If (! Processshellcommand (m_cmdinfo) <br/> return false; 

In this way, the CWzdCommandLineInfo object can be obtained through AfxGetApp ()-> m_commaninfo elsewhere in the program, and then the option to run can be known by judging the member variable m_bCFlag.

 

 

Reprinted from: http://www.host01.com/article/software/VisualC/20060917184425572.htm

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.