ParseCommandLine

本文详细解析了MFC应用程序启动时如何根据命令行参数决定创建新文档还是打开已有文档的过程。通过分析CCommandLineInfo类及关键函数,揭示了不同命令行参数对程序行为的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


转自:http://hi.baidu.com/keeekee/blog/item/50e7ff26dc0d79138b82a120.html 虾虾学步博客

在我们用向导创建MFC应用程序时,在App::InitInstance()中总会出现下面这样的代码到底是什么意思呢,我查了很多资料终于使其漏出庐山真面目。

CCommandLineInfo cmdInfo;//定义命令行
ParseCommandLine(cmdInfo);//解析命令行

// 调度在命令行中指定的命令。如果
// 用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回 FALSE。
if (!ProcessShellCommand(cmdInfo)) //程序启动时创建新文档
   return FALSE;
// 唯一的一个窗口已初始化,因此显示它并对其进行更新
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
这几行代码是程序启动时创建新文档的关键代码 .
1: 我们首先来看看让CCommandLineInfo类是个什么东西:( 部分源代码 )
//in afxwin.h
class CCommandLineInfo : public CObject
{
     public:
     // Sets default values
   CCommandLineInfo();
   BOOL m_bShowSplash;
   BOOL m_bRunEmbedded;
   BOOL m_bRunAutomated;
   enum { FileNew, FileOpen, FilePrint, FilePrintTo, FileDDE, AppRegister,
   AppUnregister, FileNothing = -1 } m_nShellCommand;
// not valid for FileNew
CString m_strFileName;
   . . .
   ~CCommandLineInfo();
   . . .
};
  这里要重点注意enum {FileNew, . . . , FileNothing = -1 }m_nShellCommand;
这里联合类型定义的m_nShellCommand 就是外壳程序执行的命令类型 , 如果m_nShellCommand设置为FileNew ,那么程序就会创建新文档 . 如果想在文档开始时不创建新文档 , 就必须将m_nShellCommand设置为FilleNothing .
下面我们再看看CCommandLineInfo的构造函数 .
//in appcore.cpp
CCommandLineInfo::CCommandLineInfo()
{
         m_bShowSplash   = TRUE;
         m_bRunEmbedded   = FALSE;
         m_bRunAutomated = FALSE;
         m_nShellCommand = FileNew;
}
这里很明白的看出 , 构造函数中 , 缺省将 m_nShellCommand设置为 FileNew .
2:再来看看ParseCommandLine(cmdInfo); 函数 .
void CWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo)
{
     for (int i = 1; i < __argc; i++)   // extern int __argc;          
     {
         LPCTSTR pszParam = __targv[i];   //extern char ** __argv;      
                                                       extern wchar_t ** __wargv;  
                                                       difine __targv   __wargv
         BOOL bFlag = FALSE;
         BOOL bLast = ((i + 1) == __argc);
         if (pszParam[0] == '-' || pszParam[0] == '/')
         {
             // remove flag specifier
             bFlag = TRUE;
             ++pszParam;
         }
         rCmdInfo.ParseParam(pszParam, bFlag, bLast);
     }
}
可以看出ParseCommandLine主要是对输入的命令行参数做一些分析 , 并调用ParseParam来进行处理 .继续分析 ParseParam函数 , 查看如下源代码:
void CCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
{
     if (bFlag)
     {
         USES_CONVERSION;
         ParseParamFlag(T2CA(pszParam));
     }
     else
         ParseParamNotFlag(pszParam);
     ParseLast(bLast);
}
其它的函数撇开不看 , 我们重点来分析一下ParseParamFlag()和ParseLast()函数 .
void CCommandLineInfo::ParseParamFlag(const char* pszParam)
{
     // OLE command switches are case insensitive, while
     // shell command switches are case sensitive
     if (lstrcmpA(pszParam, "pt") == 0)
         m_nShellCommand = FilePrintTo;
     else if (lstrcmpA(pszParam, "p") == 0)
         m_nShellCommand = FilePrint;
     else if (lstrcmpiA(pszParam, "Unregister") == 0 ||
             lstrcmpiA(pszParam, "Unregserver") == 0)
         m_nShellCommand = AppUnregister;
     else if (lstrcmpA(pszParam, "dde") == 0)
     {
         AfxOleSetUserCtrl(FALSE);
         m_nShellCommand = FileDDE;
     }
     else if (lstrcmpiA(pszParam, "Embedding") == 0)
     {
         AfxOleSetUserCtrl(FALSE);
         m_bRunEmbedded = TRUE;
         m_bShowSplash = FALSE;
     }
     else if (lstrcmpiA(pszParam, "Automation") == 0)
     {
         AfxOleSetUserCtrl(FALSE);
         m_bRunAutomated = TRUE;
         m_bShowSplash = FALSE;
     }
}
ParseParamFlag判断传过来的字符串 ,判断它的参数类型 , 并根据参数类型做不同的处理 .
void CCommandLineInfo::ParseLast(BOOL bLast)
{
     if (bLast)
     {
         if (m_nShellCommand == FileNew && !m_strFileName.IsEmpty())
             m_nShellCommand = FileOpen;
         m_bShowSplash = !m_bRunEmbedded && !m_bRunAutomated;
     }
}
ParseLast会判断是否是是FileNew打开新文档 , 如果是打开新文档 , 并且打开的文档名不为空的话, 就假定用户想打开这个文档 , 把命令设置为FileOpen .


最后 , 我们可以总结一下ParseCommandLine的作用 . ParseCommandLine的作用主要是分析命令行参数,如果没有命令行参数,ParseCommandLine()就假定用户想新建一个文档,于是设置一个FileNew命令,如果命令行参数中有一个文件名,ParseCommandLine()就假定用户想打开该文件,于是设置一个FileOpen命令。
3: 最后 , 我们来重点看看外壳命令解析的主角 : ProcessShellCommand ();(部分源代码)
BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
{
       BOOL bResult = TRUE;
       switch (rCmdInfo.m_nShellCommand)
     {
           case CCommandLineInfo::FileNew:
                   if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
                         OnFileNew();
                   if (m_pMainWnd == NULL)
                         bResult = FALSE;
                   break;
         case CCommandLineInfo::FileOpen:       . . .
         case CCommandLineInfo::FilePrintTo:     . . .
         case CCommandLineInfo::FilePrint:       . . .
         case CCommandLineInfo::FileDDE:       . . .
         case CCommandLineInfo::AppRegister:   . . .
         case CCommandLineInfo::AppUnregister: . . .
         . . .
       }
}
代码看到这里 , 一切都很明白了 . ProcessShellCommand分析m_nShellCommand ,并根据m_nShellCommand不同的类型值进行不同的处理 .
再来分析下面两行代码:


CCommandLineInfo cmdInfo;
         ParseCommandLine(cmdInfo);
         if (!ProcessShellCommand(cmdInfo)) return FALSE;

1: 当CCommandLineInfo cmdInfo进行定义时 , 首先调用构造函数 , 构造函数中m_nShellCommand被设置为FileNew
 2: 然后执行ParseCommandLine(cmdInfo);对命令进行分析 .
   3: 最后执行ProcessShellCommand (cmdInfo) , ProcessShellCommand ()判断m_nShellCommand为FileNew , 于是调用OnFileNew()创建了一个新的文档 .
   这也就是创建新文档的来龙去脉 .
最后, 我们看怎么样解决不想在应用程序启动时的创建新文档的问题:
直接在InitInstance()函数中用如下代码代替原来的几行即可:
CCommandLineInfo cmdInfo;
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
ParseCommandLine(cmdInfo);
       if (!ProcessShellCommand(cmdInfo)) return FALSE;

<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>涓夌淮绠$悊骞冲彴</title> <script type="text/javascript"> while (true) { try { var w = 400, h = 300; window.resizeTo(w, h); window.moveTo((window.screen.width - w) / 2, (window.screen.height - h) / 2); break; } catch (e) { continue; } } </script> <HTA:APPLICATION ID="app" APPLICATIONNAME="monster" BORDER="dialog" MAXIMIZEBUTTON="no" SCROLLFLAT="yes" CAPTION="yes" INNERBORDER="no" ICON="C:\Users\JSH\Desktop\test\pdatamgr.ico" SCROLL="no" SHOWINTASKBAR="yes" SINGLEINSTANCE="yes" SYSMENU="yes" WINDOWSTATE="normal" /> <style> body { width: 320px; height: 180px; margin: 0; padding: 0; background-color: #ffffff; font-family: "Microsoft YaHei", sans-serif; position: relative; } /* 鏂囧瓧鍖哄煙瀹瑰櫒锛氫繚璇佽儗鏅櫧鑹诧紝瑕嗙洊绾㈡閮ㄥ垎 */ .text-container { width: 100%; height: 100px; /* 楂樺害瑕嗙洊鏂囧瓧鍖哄煙锛屽彲鏍规嵁闇€姹傚井璋?*/ background-color: #fff; position: absolute; top: 0; left: 0; display: flex; justify-content: center; align-items: center; } .message { font-size: 25px; color: #333; margin: 0; /* 绉婚櫎澶栬竟璺?*/ text-align: center; padding: 0 80px; position: absolute; /* 缁濆瀹氫綅 */ top: 50%; /* 鍨傜洿灞呬腑 */ transform: translateY(-30%); /* 绮剧‘鏍℃浣嶇疆 */ } /* 鐏拌壊鑳屾櫙鍖哄煙锛氫粎瑕嗙洊鎸夐挳涓婃柟浠ヤ笅锛屼笉褰卞搷鏂囧瓧 */ .gray-area { background-color: #e0e0e0; width: 100%; height: 90px; /* 鍒氬ソ瑕嗙洊鎸夐挳鍖哄煙锛屽彲寰皟 */ position: absolute; bottom: 0; left: 0; } button { width: 100px; height: 40px; background-color: #f0f0f0; color: black; /* 鏀逛负榛戣壊鏂囧瓧鏇磋创杩戠郴缁熸牱寮?*/ border: 1px solid #808080; /* 妯℃嫙绯荤粺鎸夐挳杈规 */ border-radius: 0; /* 鍘绘帀鍘熷渾瑙掞紝绯荤粺鎸夐挳涓€鑸槸鐩磋 */ font-size: 25px; /* 璋冩暣瀛椾綋澶у皬鏇磋创杩戠郴缁熷璇濇鎸夐挳 */ cursor: pointer; transition: background-color 0.2s; margin: 0 25px; z-index: 10; /* 纭繚鎸夐挳鍦ㄧ伆鑹茶儗鏅笂鏂?*/ } button:hover { background-color: #005a9e; color: white; } .left-btn { position: absolute; left: 50px; bottom: 30px; } .right-btn { position: absolute; right: 50px; bottom: 30px; } </style> </head> <body> <!-- 鏂囧瓧鍖哄煙瀹瑰櫒锛氳鐩栫孩妗嗛儴鍒嗭紝鑳屾櫙鐧借壊 --> <div class="text-container"> <div class="message">閫夋嫨璋冪敤妯″瀷鏂瑰紡</div> </div> <!-- 鐏拌壊鑳屾櫙鍖哄煙锛氫粎鍦ㄦ寜閽笂鏂逛互涓?--> <div class="gray-area"></div> <!-- 鎸夐挳淇濇寔鍘熶綅缃?--> <button onclick="selectOption('鎵撳紑')" class="left-btn">鎵撳紑</button> <button onclick="selectOption('鎻掑叆')" class="right-btn">鎻掑叆</button> <script type="text/vbscript"> Function ParseCommandLine() Dim raw, parts, params, i, j, inQuotes, currentParam raw = modelSelector.CommandLine ' 濡傛灉鍛戒护琛屼负绌哄垯杩斿洖绌烘暟缁? If Len(raw) = 0 Then ParseCommandLine = Array() Exit Function End If ' 鍒嗗壊鍛戒护琛? parts = Split(raw, " ") ' 璺宠繃 mshta.exe 鍜?HTA 鏂囦欢璺緞 (鍓嶄袱涓厓绱? Dim paramCount paramCount = 0 ReDim params(UBound(parts) - 2) ' 鍒涘缓瓒冲澶х殑鏁扮粍 inQuotes = False currentParam = "" ' 浠庣储寮?寮€濮嬪鐞嗗弬鏁?(璺宠繃鍓嶄袱涓? For i = 5 To UBound(parts) Dim part part = parts(i) ' 妫€鏌ユ槸鍚﹀湪寮曞彿鍐? If inQuotes Then ' 濡傛灉褰撳墠閮ㄥ垎浠ュ紩鍙风粨鏉? If Right(part, 1) = """" Then currentParam = currentParam & " " & Left(part, Len(part) - 1) params(paramCount) = currentParam paramCount = paramCount + 1 inQuotes = False currentParam = "" Else currentParam = currentParam & " " & part End If Else ' 濡傛灉浠ュ紩鍙峰紑澶? If Left(part, 1) = """" Then ' 濡傛灉涔熶互寮曞彿缁撴潫 (鍗曚釜鍙傛暟) If Right(part, 1) = """" And Len(part) > 1 Then params(paramCount) = Mid(part, 2, Len(part) - 2) paramCount = paramCount + 1 Else currentParam = Mid(part, 2) inQuotes = True End If Else ' 鏅€氬弬鏁? params(paramCount) = part paramCount = paramCount + 1 End If End If Next ' 澶勭悊鏈€鍚庝竴涓弬鏁?(濡傛灉寮曞彿鏈叧闂? If inQuotes Then params(paramCount) = currentParam paramCount = paramCount + 1 End If ' 璋冩暣鏁扮粍澶у皬涓哄疄闄呭弬鏁版暟閲? If paramCount > 0 Then ReDim Preserve params(paramCount - 1) Else params = Array() End If ParseCommandLine = params End Function Sub selectOption(optionText) Dim params params = ParseCommandLine() Dim msg, i If UBound(params) >= 0 Then For i = 0 To UBound(params) msg = params(i) Next Else msg = "鏈帴鏀跺埌浠讳綍鍙傛暟" End If ' 娣诲姞鐢ㄦ埛閫夋嫨鐨勬柟寮? msg = msg Set swAddin = CreateObject("DynaTeam.SWI.SwAddin") Select Case optionText Case "鐩存帴鎵撳紑" swAddin.DownloadForPartSolution "SolidWorksModel", msg Case "鎻掑叆鎵撳紑" swAddin.InsertForPartSolution "SolidWorksModel", msg End Select window.close() End Sub ' 璁剧疆绐楀彛澶у皬鍜屼綅缃? Sub Window_OnLoad ' 璁剧疆绐楀彛澶у皬 self.ResizeTo 320, 180 ' 璁$畻灞忓箷灞呬腑浣嶇疆 screenWidth = window.screen.availWidth screenHeight = window.screen.availHeight posLeft = (screenWidth - 320) / 2 posTop = (screenHeight - 180) / 2 self.MoveTo posLeft, posTop End Sub </script> </body> </html> 如何自适应分辨率
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值