MFC 中标题栏上的关闭按钮

2022-01-12 00:00:00 visual-studio visual-c++ mfc

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

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 处理程序添加到您的代码:

转到类视图.如果它是基于对话框的应用程序,请右键单击您的对话框类;如果它是 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.

相关文章