• <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   管理


            亚洲色婷婷综合久久| 18禁黄久久久AAA片| 色天使久久综合网天天| 久久99精品久久久久久9蜜桃| 国产精品国色综合久久| 国内精品久久久久影院一蜜桃| 亚洲乱码中文字幕久久孕妇黑人| 欧美伊人久久大香线蕉综合| 亚洲七七久久精品中文国产| 亚洲精品无码久久久| 久久久久久久久久久精品尤物| 亚洲日本va午夜中文字幕久久| 一本一道久久a久久精品综合| 久久精品国产精品亜洲毛片| 久久99精品久久久久久噜噜| 久久综合伊人77777麻豆| 久久久国产打桩机| 国产精品一久久香蕉国产线看观看| 人妻精品久久久久中文字幕69| 久久亚洲AV成人无码国产 | 久久97久久97精品免视看| 久久99精品久久久久久野外| 欧美一区二区久久精品| 亚洲精品无码久久千人斩| 免费国产99久久久香蕉| 色综合久久中文字幕综合网| 少妇高潮惨叫久久久久久| 国产精品一久久香蕉国产线看| 久久AⅤ人妻少妇嫩草影院| 久久久久se色偷偷亚洲精品av | 久久久无码精品亚洲日韩软件| 国产精品亚洲综合久久| 99久久国产热无码精品免费| 久久福利片| 久久国产乱子伦免费精品| 欧美激情精品久久久久久久| 久久精品人成免费| 国产99久久久国产精品小说| 91性高湖久久久久| 久久夜色精品国产噜噜亚洲AV| 久久AAAA片一区二区|