您的位置:首页 > 其它

MFC程序带参数运行

2009-05-02 23:28 344 查看
下面的例子可以用来判断程序运行的时候是否使用了-c,-s或者-p选项,具体程序中大家可以按照例子做简单改动即可。
第一步:从CCommandLineInfo重载一个类CWzdCommandLineInfo,实现方式如下
#if !defined WZDCOMMANDLINEINFO_H
#define WZDCOMMANDLINEINFO_H
// WzdCommandLineInfo.h : header file
//////////////////////////////////////////////////////////////////////////
// CWzdCommandLineInfo window
class CWzdCommandLineInfo : public CCommandLineInfo
{
// Construction
public :
CWzdCommandLineInfo( ) ;
// Attributes
public:
BOOL m_bCFlag;
BOOL m_bSFlag;
BOOL m_bPFlag;
CString m_sArg;
// Operations
public:
void ParseParam(const TCHAR* pszParam,BOOL bFlag, BOOL bLast);
// Overrides
// Implementation
public:
virtual ~CWzdCommandLineInfo();
} ;
//////////////////////////////////////////////////////////////////////////
#endif


头文件结束,下面是CPP文件

// WzdCommandLineInfo.cpp : implementation file
//
#include "stdafx.h"
#include "WzdCommandLineInfo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////////
// CWzdCommandLineInfo
CWzdCommandLineInfo::CWzdCommandLineInfo( )
{
m_bCFlag = FALSE ;
m_bSFlag = FALSE ;
m_bPFlag = FALSE;
m_sArg = _T(" ") ;
}
CWzdCommandLineInfo::~CWzdCommandLineInfo( )
{
}
//////////////////////////////////////////////////////////////////////////
void CWzdCommandLineInfo::ParseParam(const TCHAR* pszParam, BOOL bFlag,
BOOL bLast)
{
CString sArg(pszParam);
if (bFlag)
{
m_bCFlag = !sArg.CompareNoCase("c");
m_bSFlag = !sArg.CompareNoCase("s");
m_bPFlag = !sArg.CompareNoCase("p");
}
// m_strFileName gets the first nonflag name
else if (m_strFileName.IsEmpty())
{
m_sArg = sArg ;
}
CCommandLineInfo::ParseParam(pszParam, bFlag, bLast ) ;
}

第二步:在APP类中添加成员变量:
public:
CWzdCommandLineInfo m_cmdInfo;
第三步:在InitInstance()函数中修改如下代码
// Parse command line for standard shell commands, DDE, file open
ParseCommandLine(m_cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(m_cmdInfo))
return FALSE;

如此即可以在程序的其它地方通过AfxGetApp()->m_cmdInfo取得CWzdCommandLineInfo对象,然后通过判断起成员变量m_bCFlag等等就能知道是用哪个选项运行的了。

转载自:http://www.host01.com/article/software/VisualC/20060917184425572.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: