• <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)云淡

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

            在文檔窗口創(chuàng)建的時(shí)候 ,它缺省總是會(huì)新建一個(gè)新文檔 , 那么怎么讓它不新建文檔呢?就這個(gè)問(wèn)題 , 我對(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: 我們首先來(lái)看看讓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)建新文檔 . 如果想在文檔開(kāi)始時(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:再來(lái)看看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來(lái)進(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ù)撇開(kāi)不看 , 我們重點(diǎn)來(lái)分析一下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判斷傳過(guò)來(lái)的字符串 ,判斷它的參數(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打開(kāi)新文檔 , 如果是打開(kāi)新文檔 , 并且打開(kāi)的文檔名不為空的話, 就假定用戶想打開(kāi)這個(gè)文檔 , 把命令設(shè)置為FileOpen .


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

             

            3: 最后 , 我們來(lái)重點(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)行不同的處理 .

            再來(lái)分析下面兩行代碼:


                    
            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)建新文檔的來(lái)龍去脈 .

             

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

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

            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 過(guò)客
            好,挺詳細(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 天之驕子
            單文檔也是可以的吧,我以前試過(guò)

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

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

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

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

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

            2009-03-04 21:42 by 飄過(guò)
            呵呵 多謝了

            # 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   博問(wèn)   Chat2DB   管理


            国产 亚洲 欧美 另类 久久| 97精品久久天干天天天按摩| 久久久国产精品福利免费| 久久综合给久久狠狠97色| 久久久久久久97| 久久精品中文字幕一区| 久久人人爽人人人人爽AV| 精品久久久久久无码中文字幕一区 | 国内精品伊人久久久久777| 久久精品无码专区免费东京热| 久久福利青草精品资源站| 色天使久久综合网天天| 久久人人爽爽爽人久久久| 久久免费香蕉视频| 精品国产乱码久久久久久郑州公司| 久久se精品一区二区影院 | 精品国产91久久久久久久a| 亚洲国产小视频精品久久久三级| 久久午夜伦鲁片免费无码| 武侠古典久久婷婷狼人伊人| 国产情侣久久久久aⅴ免费| 国产免费久久精品99re丫y| 久久综合九色综合久99| 久久精品亚洲一区二区三区浴池| 狠狠色丁香久久婷婷综合蜜芽五月| 久久综合久久综合久久综合| 久久综合狠狠综合久久| 国产色综合久久无码有码| 欧美亚洲日本久久精品| 国产成人精品久久亚洲高清不卡| 久久久无码人妻精品无码| 伊人久久精品无码av一区| 日产精品久久久久久久| 热久久国产欧美一区二区精品| 午夜不卡888久久| 7777久久亚洲中文字幕| 久久久久亚洲精品天堂| 国产午夜福利精品久久2021| 国产精品一区二区久久不卡| 久久精品天天中文字幕人妻| 91精品国产综合久久婷婷|