visual-studio MFC中标题栏上的关闭按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1163484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:47:58  来源:igfitidea点击:

Close Button on Title Bar in MFC

visual-studiovisual-c++mfc

提问by Aamir

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.

在 Vc++ 6.0 基于对话框的 MFC 应用程序中:我不希望我的用户通过按下窗口本身右上角的按钮 [X] 以及 (Alt+F4) 来关闭窗口。我想显示一个消息框(“你真的要关闭应用程序吗”);如果用户单击 OK 按钮,则应用程序必须关闭,否则,如果用户单击 CANCEL 按钮,则应用程序不得关闭。

回答by RichieHindle

You need to implement OnClose()and only call the base class's OnClose()if you want to quit.

如果您想退出,您需要实现OnClose()并且仅调用基类OnClose()

回答by Aamir

Handle the WM_SYSCOMMANDmessage and do something like this in it.

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

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);
    }
}

Here is how to add WM_SYSCOMMAND Handler to your Code:

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

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.

转到类视图。如果您的对话框类是基于对话框的应用程序,请右键单击您的对话框类,如果它是 SDI/MDI 应用程序,则右键单击您的大型机类。单击属性。

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

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

OR

或者

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

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