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怎么显示 不出来
...全文
360 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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

16,548

社区成员

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

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

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