.NET 如何禁用<Alt + F4>及窗口<关闭>按钮
Posted by Admin L in .NET Programming on 20-06-2011. Tags: .NET 编程经验
作者:牧山道人
原文地址:https://www.seeksunslowly.com/disable-alt-f4-close-sc
转载请注明出处,谢谢。
_____________________________________
有时,我们需要禁用<Alt + F4>及窗口右上角的<关闭>按钮以防止某窗口被异常关闭(比如,备份数据的进度窗口)。
通过试验与总结,下面介绍一种简单有效的方法(VB 2008),并且没有动用 API,以避免不稳定因素。
1、新建 Windows Forms Application。
2、在 Form1 上放置一个 Button(Text = “Close Me”)。
3、在窗体设计器中双击 Form1 任意位置,将所有代码替换为以下代码:
[cc lang=”vbnet”]
Public Class Form1
‘ Can close form or not.
Private canClose As Boolean = False
Private Sub Me_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
If Not canClose Then e.Cancel = True
End Sub
Private Sub Me_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) _
Handles Me.FormClosed
Me.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
canClose = True
Me.Close()
End Sub
End Class
[/cc]
4、运行程序,可以发现<Alt + F4>及窗口右上角<关闭>按钮已失效,点<Close Me>才能关闭该窗口。
源码下载:
https://www.SeekSunSlowly.com/downloadable-source-codes/disable-alt-f4-close.zip