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

            笑看風云淡

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

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

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

                      if (!ProcessShellCommand(cmdInfo)) return FALSE;

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

             

            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;

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

            下面我們再看看CCommandLineInfo的構造函數(shù) .

            //in appcore.cpp

             CCommandLineInfo::CCommandLineInfo()

             {

                    m_bShowSplash   = TRUE;

                    m_bRunEmbedded  = FALSE;

                    m_bRunAutomated = FALSE;

                    m_nShellCommand = FileNew;

             }

            這里很明白的看出 , 構造函數(shù)中 , 缺省將 m_nShellCommand設置為 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ù)做一些分析 , 并調用ParseParam來進行處理 .繼續(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ù)撇開不看 , 我們重點來分析一下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ù)類型 , 并根據參數(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打開新文檔 , 如果是打開新文檔 , 并且打開的文檔名不為空的話, 就假定用戶想打開這個文檔 , 把命令設置為FileOpen .


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

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

               這也就是創(chuà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)建新文檔  回復  更多評論   

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

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

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

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

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

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

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

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

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

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

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

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

            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;
            亚洲欧洲久久av| 26uuu久久五月天| 久久精品久久久久观看99水蜜桃| 免费久久人人爽人人爽av| 久久久无码精品亚洲日韩京东传媒| 香蕉久久夜色精品升级完成| 久久久久久久综合日本亚洲| 久久影视国产亚洲| 久久亚洲国产成人精品性色| 国产精品成人99久久久久| 久久久久亚洲av综合波多野结衣| 97久久精品无码一区二区天美| 亚洲国产成人久久一区WWW| 77777亚洲午夜久久多喷| 久久久久国色AV免费看图片| 97久久超碰成人精品网站| 日韩欧美亚洲综合久久影院Ds| 99久久国产热无码精品免费| 欧美国产成人久久精品| 久久久久无码精品国产| 久久精品卫校国产小美女| 久久这里只有精品久久| 久久香蕉国产线看观看精品yw| 国产日韩欧美久久| 久久精品国产免费一区| 久久精品麻豆日日躁夜夜躁| 77777亚洲午夜久久多人| 久久国产热这里只有精品| 青青草原综合久久大伊人精品| 国内精品综合久久久40p| 久久综合偷偷噜噜噜色| 久久综合久久综合亚洲| 中文字幕精品无码久久久久久3D日动漫 | 99精品久久精品一区二区| 色天使久久综合网天天| 久久久久婷婷| 一本久道久久综合狠狠躁AV| 香蕉久久久久久狠狠色| 国产精品亚洲综合久久| 亚洲精品无码久久久影院相关影片| 久久无码AV一区二区三区|