C# 如何以wpf形式捕获窗口关闭按钮(窗口右上角的红色X按钮)的事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8969846/
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
How to catch the event of the window close button(red X button on window right top corner) in wpf form?
提问by HotTester
How can I catch the event of the window close button(red X button on window right top corner) in a WPF form? We have got the closing event, window unloaded event also, but we want to show a pop up if he clicks the close button of the WPF form.
如何在 WPF 表单中捕获窗口关闭按钮(窗口右上角的红色 X 按钮)的事件?我们有关闭事件,也有窗口卸载事件,但是如果他单击 WPF 表单的关闭按钮,我们想显示一个弹出窗口。
采纳答案by Natxo
Use the Closingevent in the Window, you can handle it like this to prevent it from closing:
使用ClosingWindow中的事件,你可以像这样处理它以防止它关闭:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
回答by Vojtana
if it is pressed confirm button in form2 do action, if it is pressed X-button do nothing:
如果按下 form2 中的确认按钮执行操作,如果按下 X 按钮不执行任何操作:
public class Form2
{
public bool confirm { get; set; }
public Form2()
{
confirm = false;
InitializeComponent();
}
private void Confirm_Button_Click(object sender, RoutedEventArgs e)
{
//your code
confirm = true;
this.Close();
}
}
first form:
第一种形式:
public void Form2_Closing(object sender, CancelEventArgs e)
{
if(Form2.confirm == false) return;
//your code
}
回答by JA12
In VB.NET:
在 VB.NET 中:
Private Sub frmMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
' finalize the class
End Sub
To disable the Form X button:
要禁用 Form X 按钮:
'=====================================================
' Disable the X button on the control bar
'=====================================================
Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim myCp As CreateParams = MyBase.CreateParams
myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
Return myCp
End Get
End Property
回答by Jemt tinhwa
at form1.Designer.csput below code to assign the event
在form1.Designer.cs放置下面的代码来分配事件
this.Closing += Window_Closing;
at form1.csput the closing function
在form1.cs放置关闭功能
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//change the event to avoid close form
e.Cancel = true;
}
回答by Naveen Kumar V
SOLUTION:
解决方案:
Have a flagto identify if Close() method is called from other than X icon button. (eg: IsNonCloseButtonClicked;)
有一个标志来标识是否从 X 图标按钮之外调用 Close() 方法。(例如:IsNonCloseButtonClicked;)
Have a conditional statement inside Closing ()event method which checks if the IsNonCloseButtonClicked is false.
在 Closing ()事件方法中有一个条件语句,用于检查 IsNonCloseButtonClicked 是否为假。
If false, the app is trying to close itself through other than X icon button. If true, it means X icon button is clicked for closing this app.
如果为 false,则应用程序正在尝试通过 X 图标按钮以外的按钮关闭自身。如果为 true,则表示单击 X 图标按钮以关闭此应用程序。
[Sample Code]
[示例代码]
private void buttonCloseTheApp_Click (object sender, RoutedEventArgs e) {
IsNonCloseButtonClicked = true;
this.Close (); // this will trigger the Closing () event method
}
private void MainWindow_Closing (object sender, System.ComponentModel.CancelEventArgs e) {
if (IsNonCloseButtonClicked) {
e.Cancel = !IsValidated ();
// Non X button clicked - statements
if (e.Cancel) {
IsNonCloseButtonClicked = false; // reset the flag
return;
}
} else {
// X button clicked - statements
}
}
回答by yingshao xo
Try this:
尝试这个:
protected override void OnClosing(CancelEventArgs e)
{
this.Visibility = Visibility.Hidden;
string msg = "Close or not?";
MessageBoxResult result =
MessageBox.Show(
msg,
"Warning",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result == MessageBoxResult.No)
{
// If user doesn't want to close, cancel closure
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}

