辅助在应用程序启动时分析命令行。
语法
class CCommandLineInfo : public CObject
成员
公共构造函数
| 名称 | 描述 |
|---|---|
CCommandLineInfo::CCommandLineInfo |
构造默认的 CCommandLineInfo 对象。 |
公共方法
| 名称 | 描述 |
|---|---|
CCommandLineInfo::ParseParam |
重写此回调以分析单个参数。 |
公共数据成员
| 名称 | 描述 |
|---|---|
CCommandLineInfo::m_bRunAutomated |
指示已找到命令行 /Automation 选项。 |
CCommandLineInfo::m_bRunEmbedded |
指示已找到命令行 /Embedding 选项。 |
CCommandLineInfo::m_bShowSplash |
指示是否应显示初始屏幕。 |
CCommandLineInfo::m_nShellCommand |
指示要处理的 shell 命令。 |
CCommandLineInfo::m_strDriverName |
如果 shell 命令为“打印到”,则指示驱动程序名称;否则为空。 |
CCommandLineInfo::m_strFileName |
指示要打开或打印的文件名;如果 shell 命令为“新建”或“DDE”,则为空。 |
CCommandLineInfo::m_strPortName |
如果 shell 命令为“打印到”,则指示端口名称;否则为空。 |
CCommandLineInfo::m_strPrinterName |
如果 shell 命令为“打印到”,则指示打印机名称;否则为空。 |
CCommandLineInfo::m_strRestartIdentifier |
如果重启管理器重启应用程序,则指示重启管理器的唯一重启标识符。 |
备注
MFC 应用程序通常会在其应用程序对象的 InitInstance 函数中创建此类的本地实例。 然后,该对象将传递给 CWinApp::ParseCommandLine,后者重复调用 ParseParam 以填充 CCommandLineInfo 对象。 然后,CCommandLineInfo 对象将传递给 CWinApp::ProcessShellCommand,以处理命令行参数和标志。
可以使用此对象封装以下命令行选项和参数:
| 命令行参数 | 执行的命令 |
|---|---|
| 应用程序 | 新建文件。 |
| app filename | 打开文件。 |
应用/p文件名 |
将文件打印到默认打印机。 |
应用/pt文件名打印机驱动程序端口 |
将文件打印到指定打印机。 |
应用程序/dde |
启动并等待 DDE 命令。 |
应用程序/Automation |
作为 OLE 自动化服务器启动。 |
应用程序/Embedding |
启动以编辑嵌入的 OLE 项。 |
应用程序/Register应用程序 /Regserver |
通知应用程序执行任何注册任务。 |
应用程序/Unregister应用程序 /Unregserver |
通知应用程序执行任何取消注册任务。 |
从 CCommandLineInfo 派生一个新类以处理其他标志和参数值。 重写 ParseParam 以处理新标志。
继承层次结构
CCommandLineInfo
要求
标头:afxwin.h
CCommandLineInfo::CCommandLineInfo
此构造函数创建具有默认值的 CCommandLineInfo 对象。
CCommandLineInfo();
备注
默认值为显示初始屏幕(m_bShowSplash=TRUE)并在“文件”菜单上执行“新建”命令。m_nShellCommand=NewFile
应用程序框架调用 ParseParam 以填充此对象的数据成员。
示例
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
CCommandLineInfo::m_bRunAutomated
指示已在命令行上找到 /Automation 标志。
BOOL m_bRunAutomated;
备注
如果为 TRUE,这意味着作为 OLE 自动化服务器启动。
CCommandLineInfo::m_bRunEmbedded
指示已在命令行上找到 /Embedding 标志。
BOOL m_bRunEmbedded;
备注
如果为 TRUE,这意味着启动以编辑嵌入的 OLE 项。
CCommandLineInfo::m_bShowSplash
指示应显示初始屏幕。
BOOL m_bShowSplash;
备注
如果为 TRUE,这意味着应在启动期间显示此应用程序的初始屏幕。 如果 ParseParam 等于 TRUE,则 m_nShellCommand 的默认实现将此数据成员设置为 CCommandLineInfo::FileNew。
CCommandLineInfo::m_nShellCommand
指示此应用程序实例的 shell 命令。
m_nShellCommand;
备注
此数据成员的类型为以下枚举类型,该类型在 CCommandLineInfo 类中定义。
enum {
FileNew,
FileOpen,
FilePrint,
FilePrintTo,
FileDDE,
AppRegister,
AppUnregister,
RestartByRestartManager,
FileNothing = -1
};
有关这些值的简要说明,请参阅以下列表。
CCommandLineInfo::FileNew指示未在命令行上找到任何文件名。CCommandLineInfo::FileOpen指示在命令行上找到了文件名,并且未在命令行上找到以下任何标志:/p、/pt、/dde。CCommandLineInfo::FilePrint指示已在命令行上找到/p标志。CCommandLineInfo::FilePrintTo指示已在命令行上找到/pt标志。CCommandLineInfo::FileDDE指示已在命令行上找到/dde标志。CCommandLineInfo::AppRegister指示在命令行上找到了/Register或/Regserver标志,并要求应用程序注册。CCommandLineInfo::AppUnregister指示要求/Unregister或/Unregserver应用程序取消注册。CCommandLineInfo::RestartByRestartManager指示应用程序已由重启管理器重启。CCommandLineInfo::FileNothing在启动时关闭新 MDI 子窗口的显示。 根据设计,应用程序向导生成的 MDI 应用程序在启动时会显示新的子窗口。 若要关闭此功能,应用程序可以在调用CCommandLineInfo::FileNothing时使用ProcessShellCommand作为 shell 命令。ProcessShellCommand由所有InitInstance( )派生类的CWinApp调用。
示例
// From CMyWinApp::InitInstance
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// DON'T display a new MDI child window during startup!!!
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
{
return FALSE;
}
CCommandLineInfo::m_strDriverName
在命令行上存储第三个非标志参数的值。
CString m_strDriverName;
备注
此参数通常是“打印到”shell 命令的打印机驱动程序的名称。 仅当在命令行上找到 ParseParam 标志时,/pt 的默认实现才会设置此数据成员。
CCommandLineInfo::m_strFileName
在命令行上存储第一个非标志参数的值。
CString m_strFileName;
备注
此参数通常是要打开的文件的名称。
CCommandLineInfo::m_strPortName
在命令行上存储第四个非标志参数的值。
CString m_strPortName;
备注
此参数通常是“打印到”shell 命令的打印机端口的名称。 仅当在命令行上找到 ParseParam 标志时,/pt 的默认实现才会设置此数据成员。
CCommandLineInfo::m_strPrinterName
在命令行上存储第二个非标志参数的值。
CString m_strPrinterName;
备注
此参数通常是“打印到”shell 命令的打印机的名称。 仅当在命令行上找到 ParseParam 标志时,/pt 的默认实现才会设置此数据成员。
CCommandLineInfo::m_strRestartIdentifier
命令行上的唯一重启标识符。
CString m_strRestartIdentifier;
备注
重启标识符对于应用程序的每个实例都是唯一的。
如果重启管理器退出应用程序并配置为重启应用程序,则重启管理器将使用重启标识符作为可选参数从命令行执行应用程序。 当重启管理器使用重启标识符时,应用程序可以重新打开之前打开的文档并恢复自动保存的文件。
CCommandLineInfo::ParseParam
框架调用此函数来分析/解释命令行中的单个参数。 第二个版本与第一个版本仅在 Unicode 项目方面有所不同。
virtual void ParseParam(
const char* pszParam,
BOOL bFlag,
BOOL bLast);
virtual void ParseParam(
const TCHAR* pszParam,
BOOL bFlag,
BOOL bLast);
参数
pszParam
参数或标志。
bFlag
指示 pszParam 是参数还是标志。
bLast
指示这是否是命令行上的最后一个参数或标志。
备注
CWinApp::ParseCommandLine 为命令行上的每个参数或标志调用一次 ParseParam,并将参数传递给 pszParam。 如果参数的第一个字符是 - 或 /,该参数将被删除,并且 bFlag 将设置为 TRUE。 分析最终参数时,bLast 设置为 TRUE。
此函数的默认实现识别以下标志:/p、/pt、/dde、/Automation 和 /Embedding,如下表所示:
| 命令行参数 | 执行的命令 |
|---|---|
| 应用程序 | 新建文件。 |
| app filename | 打开文件。 |
应用/p文件名 |
将文件打印到默认打印机。 |
应用/pt文件名打印机驱动程序端口 |
将文件打印到指定打印机。 |
应用程序/dde |
启动并等待 DDE 命令。 |
应用程序/Automation |
作为 OLE 自动化服务器启动。 |
应用程序/Embedding |
启动以编辑嵌入的 OLE 项。 |
应用程序/Register应用程序 /Regserver |
通知应用程序执行任何注册任务。 |
应用程序/Unregister应用程序 /Unregserver |
通知应用程序执行任何取消注册任务。 |
此信息存储在 m_bRunAutomated、m_bRunEmbedded 和 m_nShellCommand 中。 标志由正斜杠 / 或连字符 - 标记。
默认实现将第一个非标志参数放入 m_strFileName。 对于 /pt 标志,默认实现将第二、第三和第四个非标志参数分别放入 m_strPrinterName、m_strDriverName 和 m_strPortName。
默认实现还仅在存在新文件的情况下将 m_bShowSplash 设置为 TRUE。 对于新文件,用户已采取涉及应用程序本身的操作。 在任何其他情况下,包括使用 shell 打开现有文件的情况,用户操作直接涉及文件。 从以文档为中心的角度看,初始屏幕无需宣布应用程序启动。
在派生类中重写此函数以处理其他标志和参数值。
另请参阅
CObject 类
层次结构图
CWinApp::ParseCommandLine
CWinApp::ProcessShellCommand