|
Public Class Form1
Private Sub ShowCloseButtonState()
If CloseButton.Enabled(Me) Then
Me.Text = "XButton is enabled"
Else
Me.Text = "XButton is disabled"
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim f2 As New Form2
f2.Show()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckBox1.Checked = True
Me.ShowCloseButtonState()
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
CloseButton.Enabled(Me) = Me.CheckBox1.Checked
Me.ShowCloseButtonState()
End Sub
End Class
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim Param As CreateParams = MyBase.CreateParams
Const CS_NOCLOSE As Int32 = &H200
Param.ClassStyle = Param.ClassStyle Or CS_NOCLOSE
Return Param
End Get
End Property
End Class
Public NotInheritable Class CloseButton
Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hWnd As IntPtr, ByVal bRevert As Integer) As IntPtr
Private Declare Function GetMenuState Lib "user32" _
(ByVal hMenu As IntPtr, ByVal uId As Integer, _
ByVal uFlags As Integer) As Integer
Private Declare Function EnableMenuItem Lib "user32" _
(ByVal hMenu As IntPtr, ByVal wIDEnableItem As Integer, _
ByVal wEnable As Integer) As Integer
Private Const MF_ENABLED As Integer = &H0
Private Const MF_DISABLED As Integer = &H2
Private Const SC_CLOSE As Integer = &HF060
Public Shared Property Enabled(ByVal form As Form) As Boolean
Get
Return CloseButton.IsEnabled(form)
End Get
Set(ByVal value As Boolean)
CloseButton.ChangeState(form, value)
End Set
End Property
Private Shared Function IsEnabled(ByVal form As Form) As Boolean
Dim systemMenu As IntPtr = GetSystemMenu(form.Handle, 0)
Dim state As Integer = GetMenuState(systemMenu, SC_CLOSE, 0)
If (state And MF_DISABLED) = MF_DISABLED Then
Return False
Else
Return True
End If
End Function
Private Shared Sub ChangeState(ByVal form As Form, ByVal state As Boolean)
Dim systemMenu As IntPtr = GetSystemMenu(form.Handle, 0)
Dim flag As Integer
If state Then
flag = MF_ENABLED
Else
flag = MF_DISABLED
End If
EnableMenuItem(systemMenu, SC_CLOSE, flag)
End Sub
End Class |