MFC 模态对话框dlg.DoModal()返回值
来源:互联网 发布:5.10沙恩霍斯特数据 编辑:程序博客网 时间:2024/05/16 07:18
查了很多资料,都说DoModal()返回值为OK或CANCEL键的ID
其实,这种说法是错误的
MSDN是这样说明其返回值的
If successful, the value of the nRetCode parameter specified in the call to EndDialog; otherwise, -1.
如果操作成功,其返回值为由EndDialog指定的nRetCode的值,而此参数nRetCode的含义为关闭对话框所采用的方式
也就是说,在关闭此模态对话框时,其返回值为关闭对话框时所采用的方式
因此它只在对话框关闭时才返回相关参数值
默认对话框关闭方式有2种:OnOK(); OnCancel()
当使用OnOK()函数关闭对话框时,返回值为IDOK
当使用OnCancel()函数关闭对话框时,返回值为IDCANCEL
返回值与ID无关
比如一个按钮的ID为IDC_BUTTON1
在此按钮的处理函数中添加关闭对话框方式: OnOk()
则 点击此按钮时,对话框返回值为IDOK
除此两种默认关闭方式外,还可用EndDialog(int nRetCode)设定自定义的关闭方式
如下例子:
- void EndDialog(
- int nResult
- );
- Parameters
- nResult
- Contains the value to be returned from the dialog box to the caller of DoModal.
- Remarks
- This member function returns nResult as the return value of DoModal. You must use the EndDialog function to complete processing whenever a modal dialog box is created.
- You can call EndDialog at any time, even in OnInitDialog, in which case you should close the dialog box before it is shown or before the input focus is set.
- EndDialog does not close the dialog box immediately. Instead, it sets a flag that directs the dialog box to close as soon as the current message handler returns.
- Example
- /* MyWnd.cpp */
- #include "MyDialog.h"
- void CMyWnd::ShowDialog()
- {
- CMyDialog myDlg;
- int nRet = myDlg.DoModal();
- if ( nRet == IDOK || nRet == 5 )
- AfxMessageBox("Dialog closed successfully");
- }
- /* MyDialog.cpp */
- void CMyDialog::OnSomeAction()
- {
- // Do something
- int nRet = 5; // Just any value would do!
- EndDialog(nRet); // This value is returned by DoModal!
- // Do something
- return; // Dialog closed and DoModal returns only here!
- }
在工程中遇到这样的问题
一个模态对话框 有三个按钮
分别是ONOK() ONCANCEL() EndDialog(0XFF)
但是发现 当按模态对话框右上角的叉号关闭对话框时
总是跳到ONCANCEL()关闭对话框时相同的处理方法
推断: 点击叉后 它默认也是ONCANCEL关闭窗口
解决方法: 因为叉响应WM_COLOSE消息 因此为它创建一个关闭方式
- void WeiKuangKe::OnClose()
- {
- // TODO: Add your message handler code here and/or call default
- int nRet=5;
- EndDialog(nRet);
- }
参考资料:
http://technet.microsoft.com/zh-cn/office/wddd3ztw%28VS.80%29.aspx
转自:http://blog.csdn.net/shuilan0066/article/details/5775383
- MFC 模态对话框dlg.DoModal()返回值
- MFC 模态对话框dlg.DoModal()返回值(转)
- MFC 模态对话框dlg.DoModal()返回值
- MFC 模态对话框dlg.DoModal()返回值
- MFC 模态对话框dlg.DoModal()返回值
- MFC 模态对话框dlg.DoModal()返回值
- MFC 模态对话框dlg.DoModal()返回值
- MFC创建模态对话框与非模态对话框 DoModal Invalidate
- MFC创建模态对话框与非模态对话框 DoModal Invalidate
- MFC创建模态对话框与非模态对话框 DoModal Invalidate
- MFC创建模态对话框与非模态对话框 DoModal Invalidate
- 控制台应用程序调用MFC DLL产生的对话框,解决对话框dlg.DoModal报错问题
- MFC 中dlg.DoModal()对话框移动到指定区域显示的一个解决办法
- 设置Dialog中Button的返回值------>Dlg.DoModal()的返回值
- Dlg之DoModal
- CDialog::DoModal返回值
- 隐藏MFC对话框DoModal出来的窗口
- MFC custom控件引发DoModal 返回-1
- 现场总线分类
- python 解析XML xml.dom
- 第4章、打包Android应用(从零开始学Android)
- Android开发免积分资料(教程+工具+源码)下载汇总
- WinCE键盘驱动
- MFC 模态对话框dlg.DoModal()返回值
- 10个助您提高网站访问速度的技巧
- Java CountDownLatch
- android listview addHeaderView和addFooterView的注意事项【转】
- Android源码50例汇总,分享给大家
- 装饰模式【Decorator Pattern】
- Redhat5-64bit yum源修改为CentOS的yum源问题
- oprofile 使用步骤 测试程序中各函数运行时间----步骤
- Codeforces #2B B. The least round way