本文介绍了为什么自动调用CWinAppEx :: OnFileNew?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用Visual Studio 2015来开发 单个文档MFC程序。 我只是覆盖默认的  CWinAppEx :: OnFileNew方法 void CmyappApp :: OnFileNew() {    CWinAppEx :: OnFileNew();    MessageBox(NULL,_T(" hello.\ n)),_ T(" Error"),MB_OKCANCEL); } 然后我发现在执行程序时会自动调用上面的方法,因为我看到了Message程序窗口出现后立即显示框。    我跟踪了堆栈,发现onFileNew方法在CmyappApp :: InitInstance中被2个函数调用:     1)ProcessShellCommand(cmdInfo),默认情况下cmdInfo.m_nShellCommand =  CCommandLineInfo :: FileNew。     2)  m_pMainWnd-> UpdateWindow();    我不知道UpdateWindow()实际上是什么,但是如果跳过了 ProcessShellCommand,例如,设置  cmdInfo.m_nShellCommand =  CCommandLineInfo: : FileNothing,以下异常将由  UpdateWindow()抛出: mrteditor.exe中0x101CCC0E(mfc140ud.dll)抛出异常:0xC0000005:访问冲突读取位置0x00000020。    最后,我必须采用以下解决方法:    首先,声明一个标志initFlag,它在CmyappApp的构造函数中初始化为true。    第二,在 CmyappApp :: InitInstance()结束时设置initFlag = false。    第三,根据initFlag的值将例程踢进不同的分支: void CmyappApp :: OnFileNew () {   if(initFlag){    CWinAppEx :: OnFileNew();    return;  } //我的代码去这里 } 但这种方式看起来很脏:-(有没有更优雅的方法来解决这个问题? 谢谢! 解决方案 你想通过改变CWinAppEx :: OnFileNew()的默认MFC处理来实现什么? Hi, I am using Visual Studio 2015 to develop asingle document MFC program.And I just override the default CWinAppEx::OnFileNew method byvoid CmyappApp::OnFileNew(){   CWinAppEx::OnFileNew();   MessageBox(NULL, _T("hello.\n"), _T("Error"), MB_OKCANCEL);}Then I find that the method above is called automatically upon executing the program, because I see the Message box immediately after the program window shows up.    I traced the stack and found that the onFileNew method gets called in CmyappApp::InitInstance by 2 functions:    1)ProcessShellCommand(cmdInfo), in which cmdInfo.m_nShellCommand = CCommandLineInfo::FileNew by default.    2) m_pMainWnd->UpdateWindow();    I don't what UpdateWindow() actually does, however if ProcessShellCommand is skipped by, for example, setting cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing, the following exception will be thrown by UpdateWindow():Exception thrown at 0x101CCC0E (mfc140ud.dll) in mrteditor.exe: 0xC0000005: Access violation reading location 0x00000020.    Finally I have to adopt the following workaround:    first, declare a flag initFlag which is initialized to true in the constructor of CmyappApp.    second, set initFlag=false at the end of CmyappApp::InitInstance().    Third, kick the routine into different branches according to the value of initFlag:void CmyappApp::OnFileNew(){ if (initFlag) {  CWinAppEx::OnFileNew();  return; }// my codes go here}But this way looks dirty:-( Is there any more elegant way to resolve this?Thank you! 解决方案 What do you want to accomplish by altering the default MFC processing for CWinAppEx::OnFileNew()? 这篇关于为什么自动调用CWinAppEx :: OnFileNew?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-26 23:46