• <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++博客 :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

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

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

                      if (!ProcessShellCommand(cmdInfo)) return FALSE;

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

             

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

               . . .

             };

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

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

            下面我們再看看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主要是對輸入的命令行參數(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會判斷是否是是FileNew打開新文檔 , 如果是打開新文檔 , 并且打開的文檔名不為空的話, 就假定用戶想打開這個文檔 , 把命令設(shè)置為FileOpen .


            最后 , 我們可以總結(jié)一下ParseCommandLine的作用 . ParseCommandLine的作用主要是
            分析命令行參數(shù),如果沒有命令行參數(shù) ParseCommandLine()就假定用戶想新建一個文檔,于是設(shè)置一個FileNew命令,如果命令行參數(shù)中有一個文件名,ParseCommandLine()就假定用戶想打開該文件,于是設(shè)置一個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)行定義時 , 首先調(diào)用構(gòu)造函數(shù) , 構(gòu)造函數(shù)中m_nShellCommand被設(shè)置為FileNew
             
            2: 然后執(zhí)行ParseCommandLine(cmdInfo);對命令進(jìn)行分析 .

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

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

             

            最后, 我們看怎么樣解決不想在應(yīng)用程序啟動時的創(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ù)  更多評論   

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

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

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

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

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

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

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

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

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

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

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

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

            2009-04-17 16:46 by blueengine@csdn.net
            對于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;

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


            国产成年无码久久久久毛片| 蜜臀av性久久久久蜜臀aⅴ麻豆| 国产精品久久久久久一区二区三区 | 久久精品国产91久久综合麻豆自制| 久久精品国产福利国产秒| 国产精品gz久久久| 久久久无码精品亚洲日韩蜜臀浪潮| 国内精品伊人久久久久av一坑| 久久精品国产一区二区| 精品国际久久久久999波多野| 久久人人爽人人澡人人高潮AV| 久久99国内精品自在现线| 久久精品国产黑森林| 国产成人精品免费久久久久| 人妻无码精品久久亚瑟影视| 青青青伊人色综合久久| 久久亚洲精品成人av无码网站| 性欧美大战久久久久久久| 亚洲国产精品久久66| 久久狠狠色狠狠色综合| 久久国产精品99国产精| 久久精品国产久精国产果冻传媒 | 久久九九有精品国产23百花影院| 亚洲精品午夜国产va久久| 久久露脸国产精品| 久久久久综合国产欧美一区二区| 91精品国产综合久久香蕉| 国产成人精品久久一区二区三区 | 日日狠狠久久偷偷色综合免费 | 久久99国产乱子伦精品免费| 午夜精品久久影院蜜桃| 亚洲午夜精品久久久久久浪潮| 久久久WWW免费人成精品| 久久97久久97精品免视看秋霞| 品成人欧美大片久久国产欧美| 国产精品热久久毛片| 久久精品国产一区二区三区不卡 | 久久国产色AV免费观看| 久久99亚洲网美利坚合众国| 久久精品蜜芽亚洲国产AV| 国产精品久久久久无码av|