MFC中标题栏上的“关闭"按钮 [英] Close Button on Title Bar in MFC

查看:843
本文介绍了MFC中标题栏上的“关闭"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于Vc ++ 6.0对话框的MFC应用程序中: 我不希望我的用户通过按窗口本身右上角的按钮[X]以及(Alt + F4)来关闭窗口.我想显示一个messageBox(您真的要关闭该应用程序")吗?如果用户单击确定"按钮,则该应用程序必须关闭;否则,如果用户单击取消"按钮,则该应用程序一定不能关闭.

In Vc++ 6.0 Dialog Based MFC application: I do not want my user close the window by pressing the button [X] in the top-right side of the window itself and also (Alt+F4). I want to display a messageBox ("Do you really want to close the application"); if the user clicks the OK button then the application has to close, else if user clicks the CANCEL button then the application must not be closed.

推荐答案

处理WM_SYSCOMMAND消息并在其中执行类似的操作.

Handle the WM_SYSCOMMAND message and do something like this in it.

void CMyApp::OnSysCommand(UINT nID, LPARAM lParam)
{
    if(nID == SC_CLOSE)
    {
        if(MessageBox(_T("Really"), _T("What"), MB_YESNO) == IDYES);
            //Do What you want here.
        else
            //Do something else
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

以下是将WM_SYSCOMMAND处理程序添加到您的代码中的方法:

转到ClassView.如果是基于对话框的应用程序,则右键单击对话框类;如果是SDI/MDI应用程序,则右键单击大型机类.点击属性.

Go to ClassView. Right click your dialog class if it is a dialog based application OR your mainframe class if it is a SDI/MDI Application. Click Properties.

在属性"窗口中,单击消息"按钮.向下滚动到WM_SYSCOMMAND,然后在下拉组合上双击以添加处理程序.

In Properties Window, click on the Messages button. Scroll down to WM_SYSCOMMAND and on drop-down combo double-click to add the handler.

您也可以通过在消息映射中添加一个条目来手动完成此操作.并分别在.h/.cpp中添加声明/定义.

You can do it manually as well by adding an entry in the message map. And adding declaration/definition in .h/.cpp respectively.

这篇关于MFC中标题栏上的“关闭"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆