OnOK()与OnCancel()

点击打开链接

首先OnOK是对ID_OK的响应, OnCancel是对IDCANCEL的响应. 前者对应键盘的Enter, 后者对应Esc.

MFC:

1、Called when the user clicks the OK button (the button with an ID ofIDOK).

2、The framework calls this member function when the user clicks the Cancel button or presses the ESC key in a modal or modeless dialog box.

 

两个函数都是CDialog类的virtual的成员函数, 也就是MFC是希望你去重载它们. 两个函数有一个共同点,就是都会调用CDialog::EndDialog. 这个CDialog::EndDialog函数是在CDialog::OnOK();中调用的。EndDialog的作用如下,摘自MSDN:


Call this method to destroy a modal dialog box

Do not call EndDialog to destroy a modeless dialog box. Call CWindow::DestroyWindow instead


模态对话框可以用EndDialog来销毁, 非模态对话框要用DestroyWindow来销毁. 以下摘自MSDN:


If you implement the OK button in a modeless dialog box, you must override the OnOK method and call DestroyWindow inside it. Do not call the base-class method, because it calls EndDialog which makes the dialog box invisible but does not destroy it.


为什么强调用谁来销毁,因为这牵涉到资源释放的问题:

//调用基类的OnOK()函数,执行基类中的EndDialog(IDOK)函数,作用是关闭对话框,并把IDOK作为对话框的返回值,返回给调用对话(DoModal)的地方。

1、对于模态对话框:

void CMyDlg::OnOK()
{
    CDialog::OnOK();
}

2、对于非模态对话框,你关闭对话框时,就不能只调用CDialog的OnOK, 还应该DestroyWindow,像下面这样:

void CMyDlg::OnOK()
{
 CDialog::OnOK();//关闭窗口,窗口不可见,但是并没有销毁
 DestroyWindow();//销毁窗口,释放窗口资源
}










评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小米的修行之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值