• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            笑看風(fēng)云淡

            寵辱不驚,看庭前花開花落;去留無意,望天空云卷云舒
            posts - 96, comments - 48, trackbacks - 0, articles - 0
              C++博客 :: 首頁(yè) :: 新隨筆 ::  :: 聚合  :: 管理

            在文檔窗口創(chuàng)建的時(shí)候 ,它缺省總是會(huì)新建一個(gè)新文檔 , 那么怎么讓它不新建文檔呢?就這個(gè)問題 , 我對(duì)文檔視圖窗口應(yīng)用程序啟動(dòng)時(shí)的文檔創(chuàng)建機(jī)制 , 稍稍的淺淺挖了一下 , 做了一個(gè)詳細(xì)的分析 , 希望能夠?qū)Τ鯇W(xué)者有所幫助 .

            App文件的InitInstance()函數(shù)中, 有如下幾行代碼:
            CCommandLineInfo  cmdInfo;
            ParseCommandLine(cmdInfo);

                      if (!ProcessShellCommand(cmdInfo)) return FALSE;

            幾行代碼是程序啟動(dòng)時(shí)創(chuàng)建新文檔的關(guān)鍵代碼 .

             

            1: 我們首先來看看讓CCommandLineInfo類是個(gè)什么東西:( 部分源代碼 )

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

               . . .

             };

              這里要重點(diǎn)注意enum {FileNew, . . . , FileNothing = -1 }m_nShellCommand;

            這里聯(lián)合類型定義的m_nShellCommand 就是外殼程序執(zhí)行的命令類型 , 如果m_nShellCommand設(shè)置為FileNew ,那么程序就會(huì)創(chuàng)建新文檔 . 如果想在文檔開始時(shí)不創(chuàng)建新文檔 , 就必須將m_nShellCommand設(shè)置為FilleNothing .

            下面我們?cè)倏纯?span>CCommandLineInfo的構(gòu)造函數(shù) .

            //in appcore.cpp

             CCommandLineInfo::CCommandLineInfo()

             {

                    m_bShowSplash   = TRUE;

                    m_bRunEmbedded  = FALSE;

                    m_bRunAutomated = FALSE;

                    m_nShellCommand = FileNew;

             }

            這里很明白的看出 , 構(gòu)造函數(shù)中 , 缺省將 m_nShellCommand設(shè)置為 FileNew .

             

            2:再來看看ParseCommandLine(cmdInfo); 函數(shù) .

             

            void CWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo)

            {

                for (int i = 1; i < __argc; i++)

                {

                    LPCTSTR pszParam = __targv[i];

                    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主要是對(duì)輸入的命令行參數(shù)做一些分析 , 并調(diào)用ParseParam來進(jìn)行處理 .繼續(xù)分析 ParseParam函數(shù) , 查看如下源代碼:

            void CCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)

            {

                if (bFlag)

                {

                    USES_CONVERSION;

                    ParseParamFlag(T2CA(pszParam));

                }

                else

                    ParseParamNotFlag(pszParam);

             

                ParseLast(bLast);

            }

            其它的函數(shù)撇開不看 , 我們重點(diǎn)來分析一下ParseParamFlag()和ParseLast()函數(shù) .

            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判斷傳過來的字符串 ,判斷它的參數(shù)類型 , 并根據(jù)參數(shù)類型做不同的處理 .

            void CCommandLineInfo::ParseLast(BOOL bLast)

            {

                if (bLast)

                {

                    if (m_nShellCommand == FileNew && !m_strFileName.IsEmpty())

                        m_nShellCommand = FileOpen;

                    m_bShowSplash = !m_bRunEmbedded && !m_bRunAutomated;

                }

            }

            ParseLast會(huì)判斷是否是是FileNew打開新文檔 , 如果是打開新文檔 , 并且打開的文檔名不為空的話, 就假定用戶想打開這個(gè)文檔 , 把命令設(shè)置為FileOpen .


            最后 , 我們可以總結(jié)一下ParseCommandLine的作用 . ParseCommandLine的作用主要是
            分析命令行參數(shù),如果沒有命令行參數(shù) ParseCommandLine()就假定用戶想新建一個(gè)文檔,于是設(shè)置一個(gè)FileNew命令,如果命令行參數(shù)中有一個(gè)文件名,ParseCommandLine()就假定用戶想打開該文件,于是設(shè)置一個(gè)FileOpen命令。

             

            3: 最后 , 我們來重點(diǎn)看看外殼命令解析的主角 : 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 ,并根據(jù)m_nShellCommand不同的類型值進(jìn)行不同的處理 .

            再來分析下面兩行代碼:


                    
            CCommandLineInfo cmdInfo;

                     ParseCommandLine(cmdInfo);

                   if (!ProcessShellCommand(cmdInfo)) return FALSE;


              
            1: 當(dāng)CCommandLineInfo cmdInfo進(jìn)行定義時(shí) , 首先調(diào)用構(gòu)造函數(shù) , 構(gòu)造函數(shù)中m_nShellCommand被設(shè)置為FileNew
             
            2: 然后執(zhí)行ParseCommandLine(cmdInfo);對(duì)命令進(jìn)行分析 .

              3: 最后執(zhí)行ProcessShellCommand (cmdInfo) , ProcessShellCommand ()判斷m_nShellCommand為FileNew , 于是調(diào)用OnFileNew()創(chuàng)建了一個(gè)新的文檔 .

               這也就是創(chuàng)建新文檔的來龍去脈 .

             

            最后, 我們看怎么樣解決不想在應(yīng)用程序啟動(dòng)時(shí)的創(chuàng)建新文檔的問題:

            直接在InitInstance()函數(shù)中用如下代碼代替原來的幾行即可:

            CCommandLineInfo cmdInfo;
            cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
            ParseCommandLine(cmdInfo);

                   if (!ProcessShellCommand(cmdInfo)) return FALSE;

            Feedback

            # re: 如何讓DOC/VIEW框架不創(chuàng)建新文檔  回復(fù)  更多評(píng)論   

            2008-08-04 22:10 by 過客
            好,挺詳細(xì)的

            # re: 如何讓DOC/VIEW框架不創(chuàng)建新文檔  回復(fù)  更多評(píng)論   

            2008-08-29 10:41 by feht1@yahoo.com.cn
            這個(gè)方法對(duì)多文檔項(xiàng)目有效
            單文檔項(xiàng)目會(huì)掛掉, 還需要做其它處理(咋辦咧? 探索中....)

            # re: 如何讓DOC/VIEW框架不創(chuàng)建新文檔  回復(fù)  更多評(píng)論   

            2008-09-01 08:48 by 天之驕子
            單文檔也是可以的吧,我以前試過

            # re: 如何讓DOC/VIEW框架不創(chuàng)建新文檔  回復(fù)  更多評(píng)論   

            2008-10-22 19:23 by 無語(yǔ)
            單文檔也不可以!!!
            測(cè)試通不過!可能還有其他原因!

            # re: 如何讓DOC/VIEW框架不創(chuàng)建新文檔  回復(fù)  更多評(píng)論   

            2008-10-30 09:33 by 真理
            說的挺好,又學(xué)了一招。可以用它來創(chuàng)建快捷方式呢!

            # re: 如何讓DOC/VIEW框架不創(chuàng)建新文檔  回復(fù)  更多評(píng)論   

            2009-03-04 21:42 by 飄過
            呵呵 多謝了

            # re: 如何讓DOC/VIEW框架不創(chuàng)建新文檔  回復(fù)  更多評(píng)論   

            2009-04-17 16:46 by blueengine@csdn.net
            對(duì)于SDI程序,可以在ParseCommandLine()之后ProcessShellCommand()之前加入cmdInfo.m_nShellCommand = cmdInfo.FileNew;即可。代碼如下:


            // Parse command line for standard shell commands, DDE, file open
            CCommandLineInfo cmdInfo;
            ParseCommandLine(cmdInfo);
            cmdInfo.m_nShellCommand = cmdInfo.FileNew;

            // Dispatch commands specified on the command line
            m_nCmdShow = SW_HIDE; //enforce the main window hide
            if (!ProcessShellCommand(cmdInfo))
            return FALSE;

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国内精品久久久久久久久电影网| 久久久久国产亚洲AV麻豆| 色狠狠久久综合网| 色妞色综合久久夜夜| 亚洲国产精品久久久久久| 青青草国产精品久久| 亚洲欧美一级久久精品| 国内精品久久久久久99蜜桃| 久久精品视频91| 久久AV高潮AV无码AV| 久久久久久国产精品美女| 久久亚洲日韩看片无码| 国产精品久久波多野结衣| 亚洲国产另类久久久精品黑人| 国产精品免费久久久久影院| 性欧美丰满熟妇XXXX性久久久| 美女写真久久影院| 伊人久久大香线蕉av不卡| 99热都是精品久久久久久| 久久国产高潮流白浆免费观看| 久久久久久久91精品免费观看| av午夜福利一片免费看久久| 亚洲国产高清精品线久久| 99久久www免费人成精品| 日韩精品久久久久久免费| 亚洲欧美日韩中文久久| 欧美日韩中文字幕久久久不卡| 久久久精品久久久久特色影视| 99久久婷婷免费国产综合精品| 久久SE精品一区二区| 久久人人添人人爽添人人片牛牛| 久久无码人妻精品一区二区三区| 久久99精品久久久久久野外| 91麻精品国产91久久久久| 996久久国产精品线观看| 久久综合九色综合网站| 亚洲精品国精品久久99热一| 久久天天躁狠狠躁夜夜2020一 | 久久人人爽人人爽人人片AV麻豆| 久久香蕉综合色一综合色88| 久久国产精品一区二区|