学习MFC这么久,直到前几天才弄明白了一个长久困扰我的问题.并且是一个很重要的问题,是关于MFC的机制.下面我拿出来和大家一起共享一下,请多指教. 大家都知道,对于一个SDI,不管是新建还是打开一份文档,都会引发一系列的操作. 而我们的SDI开始运行时,都会自动先新建一份文档.确实它跟点击”新建”菜单项的操作是相同的.那么到底是在哪里自动调用这个菜单项的操作的呢? 到处都没找到.后来无意之间,在InitInstance中看到一个奇怪的家伙: BOOL CManageApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CManageDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CManageView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } 这个函数到底是干吗的?带着这种疑问,我去安装文件中找到: BOOL CWinApp :: ProcessShellCommand(CCommandLineInfo &info) { ……. Switch (info .m_nshellCommand) { Case CComandLineInfo::FileNew: If(!AfxGetApp( ) - >OnCmdMsg( ID_FILE_NEW, 0 ,NULL , NULL)) OnFileNew( ); …….. } 原来如此!

评论