MFC中的InitInstance(),DoModal()函数返回IDOK或者IDCANCEL,为什么里面的代码不执行呢?

ljwsunyard1 2013-04-22 01:18:54
BOOL CIDOKApp::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
CIDOKDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
AfxMessageBox("hello");
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
AfxMessageBox("hello");
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}

//
//
//hello怎么显示 不出来
...全文
331 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
理查德大叔 2013-04-22
  • 打赏
  • 举报
回复
AfxMessageBox("hello");的弹出是依赖于它所在的上级对话框的,如果你的程序是基于对话框的应用程序,那么 if (nResponse == IDOK) {}返回时,你程序中唯一的对话框已经消亡了,所以AfxMessageBox("hello")是不会弹出来的。 我把MFC中的代码挖给你看:AfxMessageBox("hello"); 实际上是执行 int CWinApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt) { // disable windows for modal dialog EnableModeless(FALSE); HWND hWndTop; HWND hWnd = CWnd::GetSafeOwner_(NULL, &hWndTop); // WINBUG: re-enable the parent window, so that focus is restored // correctly when the dialog is dismissed. if (hWnd != hWndTop) EnableWindow(hWnd, TRUE); ……………………………… } 当你这么做时,hWnd和hWndTop返回的值都是0,所以EnableWindow(hWnd, TRUE);不执行,那么对话框自然不会弹出。 我说下正确的做法:你双击OK按钮,程序会自动创建 void CXXXXXXX::OnBnClickedOk() { // TODO : ここにコントロール通知ハンドラ コードを追加します。 AfxMessageBox("hello"); // 放在这里既可,正确弹出 OnOK(); }
ljwsunyard1 2013-04-22
  • 打赏
  • 举报
回复
void CIDOKDlg::OnOK() 
{
	// TODO: Add extra validation here
//	AfxMessageBox("hello");
CDialog::OnOK();
		//AfxMessageBox("hello");
}

void CIDOKDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
//	AfxMessageBox("hello");
	CDialog::OnCancel();
}
//试过了,没用, //m_pMainWnd = &dlg; // 注释掉这句 //这样就行了. //不明白为什么啊?
skynfly 2013-04-22
  • 打赏
  • 举报
回复
引用 5 楼 ljwsunyard1 的回复:
每次新建MFC应用程序工程,都会有那个判断啊,这是MFC框架自动生成的代码。只有那个 AfxMessageBox("hello"); 是我加上去的。if和else中我都加了AfxMessageBox("hello");就是打印不出来啊,怎么样让DoModal返回IDOK或者IDCANCEL啊。
让他返回IDOK或者IDCANCEL,应该在你退出的地方加上 CDialog::OnOK(); 或者CDialog::OnCancel();就行
ljwsunyard1 2013-04-22
  • 打赏
  • 举报
回复
引用 4 楼 VisualEleven 的回复:
m_pMainWnd = &dlg; // 注释掉这句
谢谢,为什么是这样啊?
shen_wei 2013-04-22
  • 打赏
  • 举报
回复
 INT_PTR nRet = -1;
   nRet = aboutDlg.DoModal();

   // Handle the return value from DoModal
   switch (nRet)
   {
      case -1: 
         AfxMessageBox(_T("Dialog box could not be created!"));
         break;
      case IDABORT:
         // Do something
         break;
      case IDOK:
         // Do something
         break;
      case IDCANCEL:
         // Do something
         break;
      default:
         // Do something
         break;
   };
ljwsunyard1 2013-04-22
  • 打赏
  • 举报
回复
每次新建MFC应用程序工程,都会有那个判断啊,这是MFC框架自动生成的代码。只有那个 AfxMessageBox("hello"); 是我加上去的。if和else中我都加了AfxMessageBox("hello");就是打印不出来啊,怎么样让DoModal返回IDOK或者IDCANCEL啊。
Eleven 2013-04-22
  • 打赏
  • 举报
回复
m_pMainWnd = &dlg; // 注释掉这句
sumos 2013-04-22
  • 打赏
  • 举报
回复
m_pMainWnd = &dlg; 我知道肯定是这句话的问题,但是我可能无法解释很完美。 当m_pMainWnd指向的窗口销毁时,代表着应用程序消息循环的终结。
oyljerry 2013-04-22
  • 打赏
  • 举报
回复
你的DoModal()是否返回了对应的值
skynfly 2013-04-22
  • 打赏
  • 举报
回复
DoModal()的返回值不只有IDOK和IDCANCEL
MFC串口通信程序代码,含界面 // 串口通信.cpp : 定义应用程序的类行为。 // #include "stdafx.h" #include "串口通信.h" #include "串口通信Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // C串口通信App BEGIN_MESSAGE_MAP(C串口通信App, CWinApp) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) END_MESSAGE_MAP() // C串口通信App 构造 C串口通信App::C串口通信App() { // 支持重新启动管理器 m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART; // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance } // 唯一的一个 C串口通信App 对象 C串口通信App theApp; // C串口通信App 初始化 BOOL C串口通信App::InitInstance() { // 如果一个运行在 Windows XP 上的应用程序清单指定要 // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式, //则需要 InitCommonControlsEx()。 否则,将无法创建窗口。 INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); // 将它设置为包括所有要在应用程序使用的 // 公共控件类。 InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance(); AfxEnableControlContainer(); // 创建 shell 管理器,以防对话框包含 // 任何 shell 树视图控件或 shell 列表视图控件。 CShellManager *pShellManager = new CShellManager; // 激活“Windows Native”视觉管理器,以便在 MFC 控件启用主题 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows)); // 标准初始化 // 如果未使用这些功能并希望减小 // 最终可执行文件的大小,则应移除下列 // 不需要的特定初始化例程 // 更改用于存储设置的注册表项 // TODO: 应适当修改该字符串, // 例如修改为公司或组织名 SetRegistryKey(_T("应用程序向导生成的本地应用程序")); C串口通信Dlg dlg; m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: 在此放置处理何时用 // “确定”来关闭对话框的代码 } else if (nResponse == IDCANCEL) { // TODO: 在此放置处理何时用 // “取消”来关闭对话框的代码 } else if (nResponse == -1) { TRACE(traceAppMsg, 0, "警告: 对话框创建失败,应用程序将意外终止。\n"); TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件,则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n"); } // 删除上面创建的 shell 管理器。 if (pShellManager != NULL) { delete pShellManager; } // 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序, // 而不是启动应用程序的消息泵。 return FALSE; }
小型计算器的源程序,基于MFC. // MyCalrr.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "MyCalrr.h" #include "MyCalrrDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMyCalrrApp BEGIN_MESSAGE_MAP(CMyCalrrApp, CWinApp) //{{AFX_MSG_MAP(CMyCalrrApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyCalrrApp construction CMyCalrrApp::CMyCalrrApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CMyCalrrApp object CMyCalrrApp theApp; ///////////////////////////////////////////////////////////////////////////// // CMyCalrrApp initialization BOOL CMyCalrrApp::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 CMyCalrrDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
#include "stdafx.h" #include "caculate.h" #include "caculateDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CCaculateApp BEGIN_MESSAGE_MAP(CCaculateApp, CWinApp) //{{AFX_MSG_MAP(CCaculateApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCaculateApp construction CCaculateApp::CCaculateApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CCaculateApp object CCaculateApp theApp; ///////////////////////////////////////////////////////////////////////////// // CCaculateApp initialization BOOL CCaculateApp::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 CCaculateDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
很全面的通讯录。各种项目还可自行修改。// Lab_link.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "Lab_link.h" #include "Lab_linkDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CLab_linkApp BEGIN_MESSAGE_MAP(CLab_linkApp, CWinApp) //{{AFX_MSG_MAP(CLab_linkApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CLab_linkApp construction CLab_linkApp::CLab_linkApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CLab_linkApp object CLab_linkApp theApp; ///////////////////////////////////////////////////////////////////////////// // CLab_linkApp initialization BOOL CLab_linkApp::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 CLab_linkDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the applicati
动态加载GIF动画 VC 实现 include "stdafx.h" #include "TransparentGif.h" #include "TransparentGifDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CTransparentGifApp BEGIN_MESSAGE_MAP(CTransparentGifApp, CWinAppEx) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) END_MESSAGE_MAP() // CTransparentGifApp 构造 CTransparentGifApp::CTransparentGifApp() { // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance } // 唯一的一个 CTransparentGifApp 对象 CTransparentGifApp theApp; // CTransparentGifApp 初始化 BOOL CTransparentGifApp::InitInstance() { // 如果一个运行在 Windows XP 上的应用程序清单指定要 // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式, //则需要 InitCommonControlsEx()。否则,将无法创建窗口。 INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); // 将它设置为包括所有要在应用程序使用的 // 公共控件类。 InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinAppEx::InitInstance(); AfxEnableControlContainer(); // 标准初始化 // 如果未使用这些功能并希望减小 // 最终可执行文件的大小,则应移除下列 // 不需要的特定初始化例程 // 更改用于存储设置的注册表项 // TODO: 应适当修改该字符串, // 例如修改为公司或组织名 SetRegistryKey(_T("应用程序向导生成的本地应用程序")); CTransparentGifDlg dlg; m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: 在此放置处理何时用 // “确定”来关闭对话框的代码 } else if (nResponse == IDCANCEL) { // TODO: 在此放置处理何时用 // “取消”来关闭对话框的代码 } // 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序, // 而不是启动应用程序的消息泵。 return FALSE; }

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧