如何以wpf格式捕捉窗口关闭按钮(窗口右上角的红色X按钮)的事件?
如何以wpf格式捕捉窗口关闭按钮(窗口右上角的红色X按钮)的事件? 我们还有关闭事件,窗口卸载事件,但是如果他点击wpf表单的关闭按钮,我们想要显示一个弹出窗口。
请帮助。
使用窗口中的Closing
事件,您可以像这样处理它以防止它关闭:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; }
如果按下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(); } }
第一种forms:
public void Form2_Closing(object sender, CancelEventArgs e) { if(Form2.confirm == false) return; //your code }
在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
要禁用表单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