InitInstance函数_第1页
InitInstance函数_第2页
InitInstance函数_第3页
InitInstance函数_第4页
InitInstance函数_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、首先看看InitInstance()函数: BOOL CSomeApp:InitInstance().Enable3dControls();LoadStdProfileSettings();AddDocTemplate(.) . ShowWindow(.);m_pMainWnd->DragAcceptFiles();EnableShellOpen();RegisterShellFileTypes(TRUE);CCommandLineInfo cmdInfo;ParseCommandLine(cmdInfo);if (!ProcessShellCommand(cmdInfo)return

2、FALSE;return TRUE; 下面对InitInstance中的一些操作及其流程进行分析1.常规设置    如:SetDialogBkColor()Enable3dControls().(如果设置了后者,则前者就不必要了)SetRegistryKey(指定注册表键,替代INI文件)2.LoadStdProfileSettings()LoadStdProfileSettings 完成最近文件列表功能,在菜单中添加最近的文件作为菜单项过程: 建立一个CRecentFileList从注册表或INI文件中读入最近文件列表; 当菜单建立时,文件列表将添加到菜单中ID

3、_FILE_MRU_FILE*位置; 3.m_pMainWnd->DragAcceptFiles()接收文件拖入 使主窗口能响应文件拖入消息WM_DROPFILES; 当有文件拖入时, 框架窗口的OnDropFiles将处理,以打开这些文件。 void CFrameWnd:OnDropFiles(HDROP hDropInfo).SetActiveWindow(); / activate us first !UINT nFiles = :DragQueryFile(hDropInfo, (UINT)-1, NULL, 0);CWinApp* pApp = AfxGetApp();ASSE

4、RT(pApp != NULL);for (UINT iFile = 0; iFile < nFiles; iFile+).TCHAR szFileName_MAX_PATH;:DragQueryFile(hDropInfo, iFile, szFileName, _MAX_PATH);/应用程序打开拖入文档pApp->OpenDocumentFile(szFileName);:DragFinish(hDropInfo); 4.EnableShellOpen();为在Windows中使用外壳操作打开文件作准备 void CWinApp:EnableShellOpen().

5、ASSERT(m_atomApp = NULL && m_atomSystemTopic = NULL); / do oncem_atomApp = :GlobalAddAtom(m_pszExeName);m_atomSystemTopic = :GlobalAddAtom(_T("system"); 5.RegisterShellFileTypes 向系统注册文件类型,以使用外壳操作。 将调用m_pDocManager->RegisterShellFileTypes() (CDocManager:RegisterShellFileType

6、s()源码附后) 要点:将所有文档模板的类型,外壳命令等写入注册表 包括type ID、shell/open/ddeexec = open("%1")、shell/print/ddeexec = print("%1")、shell/printto/ddeexec = printto("%1","%2","%3","%4")等等。 6.ProcessShellCommand 处理命令行、外壳命令等 CCommandLineInfo cmdInfo;ParseCommandLin

7、e(cmdInfo);if (!ProcessShellCommand(cmdInfo)return FALSE; 先建立CCommandLineInfo对象 再将命令行参数等分解到cmdInfovoid CWinApp:ParseCommandLine(CCommandLineInfo& rCmdInfo).for (int i = 1; i < _argc; i+).LPCTSTR pszParam = _targvi;BOOL bFlag = FALSE;BOOL bLast = (i + 1) = _argc);if (pszParam0 = '-' |

8、pszParam0 = '/')./ remove flag specifierbFlag = TRUE;+pszParam;rCmdInfo.ParseParam(pszParam, bFlag, bLast); 通过该操作,命令行被转化为cmdInfo; 命令行的意义app (新建文件)app filename(打开文件) app /p filename(打印文件)app /pt filename printer driver port (用指定的打印机打印)app /dde (运行并接收DDE命令)app /Automation (启动为自动化服务器)app /Embed

9、ding (内嵌式运行)ParseCommandLine后,操作类型(打开、新建、打印.)存放在m_nShellCommand; 文件名存放在m_strFileName. 处理命令 主要操作: switch (rCmdInfo.m_nShellCommand).case CCommandLineInfo:FileNew:OnFileNew().break;case CCommandLineInfo:FileOpen:OpenDocumentFile(rCmdInfo.m_strFileName).break;case CCommandLineInfo:FilePrint:case CComma

10、ndLineInfo:FilePrintTo:打开文件,发送ID_FILE_PRINT_DIRECT,返回FALSE值(导致立即程序退出)case CCommandLineInfo:FileDDE: m_nCmdShow = SW_HIDE;(程序被运行,但被隐藏,m_nCmdShow作为ShowWindow的参数)等等操作 附一:CDocManager:RegisterShellFileTypes void CDocManager:RegisterShellFileTypes(BOOL bCompat).ASSERT(!m_templateList.IsEmpty(); / must hav

11、e some doc templatesCString strPathName, strTemp;AfxGetModuleShortFileName(AfxGetInstanceHandle(), strPathName);POSITION pos = m_templateList.GetHeadPosition();/针对每种文档模板进行注册for (int nTemplateIndex = 1; pos != NULL; nTemplateIndex+).CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos)

12、;CString strOpenCommandLine = strPathName;CString strPrintCommandLine = strPathName;CString strPrintToCommandLine = strPathName;CString strDefaultIconCommandLine = strPathName;if (bCompat).CString strIconIndex;HICON hIcon = :ExtractIcon(AfxGetInstanceHandle(), strPathName, nTemplateIndex);if (hIcon

13、!= NULL).strIconIndex.Format(_afxIconIndexFmt, nTemplateIndex);DestroyIcon(hIcon);else.strIconIndex.Format(_afxIconIndexFmt, DEFAULT_ICON_INDEX);strDefaultIconCommandLine += strIconIndex;CString strFilterExt, strFileTypeId, strFileTypeName;if (pTemplate->GetDocString(strFileTypeId,CDocTemplate:re

14、gFileTypeId) && !strFileTypeId.IsEmpty()./ enough info to register itif (!pTemplate->GetDocString(strFileTypeName,CDocTemplate:regFileTypeName)strFileTypeName = strFileTypeId; / use id nameASSERT(strFileTypeId.Find(' ') = -1); / no spaces allowed/ first register the type ID of our

15、 serverif (!_AfxSetRegKey(strFileTypeId, strFileTypeName)continue; / just skip itif (bCompat)./ pathDefaultIcon = path,1strTemp.Format(_afxDefaultIconFmt, (LPCTSTR)strFileTypeId);if (!_AfxSetRegKey(strTemp, strDefaultIconCommandLine)continue; / just skip it/ If MDI Applicationif (!pTemplate->GetD

16、ocString(strTemp, CDocTemplate:windowTitle) |strTemp.IsEmpty()./ pathshellopenddeexec = open("%1")strTemp.Format(_afxShellOpenFmt, (LPCTSTR)strFileTypeId,(LPCTSTR)_afxDDEExec);if (!_AfxSetRegKey(strTemp, _afxDDEOpen)continue; / just skip itif (bCompat)./ pathshellprintddeexec = print("

17、;%1")strTemp.Format(_afxShellPrintFmt, (LPCTSTR)strFileTypeId,(LPCTSTR)_afxDDEExec);if (!_AfxSetRegKey(strTemp, _afxDDEPrint)continue; / just skip it/ pathshellprinttoddeexec = printto("%1","%2","%3","%4")strTemp.Format(_afxShellPrintToFmt, (LPCTSTR)strFi

18、leTypeId,(LPCTSTR)_afxDDEExec);if (!_AfxSetRegKey(strTemp, _afxDDEPrintTo)continue; / just skip it/ pathshellopencommand = path /dde/ pathshellprintcommand = path /dde/ pathshellprinttocommand = path /ddestrOpenCommandLine += _afxDDEArg;strPrintCommandLine += _afxDDEArg;strPrintToCommandLine += _afx

19、DDEArg;else.strOpenCommandLine += _afxOpenArg;else./ pathshellopencommand = path filename/ pathshellprintcommand = path /p filename/ pathshellprinttocommand = path /pt filename printer driver portstrOpenCommandLine += _afxOpenArg;if (bCompat).strPrintCommandLine += _afxPrintArg;strPrintToCommandLine

20、 += _afxPrintToArg;/ pathshellopencommand = path filenamestrTemp.Format(_afxShellOpenFmt, (LPCTSTR)strFileTypeId,(LPCTSTR)_afxCommand);if (!_AfxSetRegKey(strTemp, strOpenCommandLine)continue; / just skip itif (bCompat)./ pathshellprintcommand = path /p filenamestrTemp.Format(_afxShellPrintFmt, (LPCT

21、STR)strFileTypeId,(LPCTSTR)_afxCommand);if (!_AfxSetRegKey(strTemp, strPrintCommandLine)continue; / just skip it/ pathshellprinttocommand = path /pt filename printer driver portstrTemp.Format(_afxShellPrintToFmt, (LPCTSTR)strFileTypeId,(LPCTSTR)_afxCommand);if (!_AfxSetRegKey(strTemp, strPrintToComm

22、andLine)continue; / just skip itpTemplate->GetDocString(strFilterExt, CDocTemplate:filterExt);if (!strFilterExt.IsEmpty().ASSERT(strFilterExt0 = '.');LONG lSize = _MAX_PATH * 2;LONG lResult = :RegQueryValue(HKEY_CLASSES_ROOT, strFilterExt,strTemp.GetBuffer(lSize), &lSize);strTemp.Rele

23、aseBuffer();if (lResult != ERROR_SUCCESS | strTemp.IsEmpty() |strTemp = strFileTypeId)./ no association for that suffixif (!_AfxSetRegKey(strFilterExt, strFileTypeId)continue;if (bCompat).strTemp.Format(_afxShellNewFmt, (LPCTSTR)strFilterExt);(void)_AfxSetRegKey(strTemp, _afxShellNewValue, _afxShell

24、NewValueName); 附二:CWinApp: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 =

25、 FALSE;break;/ If we've been asked to open a file, call OpenDocumentFile()case CCommandLineInfo:FileOpen:if (!OpenDocumentFile(rCmdInfo.m_strFileName)bResult = FALSE;break;/ If the user wanted to print, hide our main window and/ fire a message to ourselves to start the printingcase CCommandLineI

26、nfo:FilePrintTo:case CCommandLineInfo:FilePrint:m_nCmdShow = SW_HIDE;ASSERT(m_pCmdInfo = NULL);OpenDocumentFile(rCmdInfo.m_strFileName);m_pCmdInfo = &rCmdInfo;m_pMainWnd->SendMessage(WM_COMMAND, ID_FILE_PRINT_DIRECT);m_pCmdInfo = NULL;bResult = FALSE;break;/ If we're doing DDE, hide ourselvescase CCom

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论