How to prevent ALT + F4 WinForm, but allow all other forms of WinForm closure?

I searched around interwebs and various parts of this resource where this question was asked and noticed that I received the following bits of code:

Protected Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim cp As CreateParams = MyBase.CreateParams
        Const CS_NOCLOSE As Integer = &H200
        cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
        Return cp
    End Get
End Property

What works as intended, it disables the use of ALT + F4. However, as an unforeseen side effect of this code: closing the window using the control unit is disabled :

Closing

Is there a version of this code that disables ALT + F4 , but still allows you to close the window through its control unit or other user interface parameters (for example, the close button and the Close option in the menu.)

, - , e.CloseReason , UserClosing - , , , ... . , .

+4
4

, KeyDown .

:

Public NotInheritable Class MainInterface
    Private Sub New() 'No constructor.
    End Sub

    Public Shared Sub DisableAltF4(ByVal TargetForm As Form)
        TargetForm.KeyPreview = True
        AddHandler TargetForm.KeyDown, AddressOf Form_KeyDown
    End Sub

    Private Shared Sub Form_KeyDown(sender As Object, e As KeyEventArgs)
        e.Handled = (e.Alt AndAlso e.KeyCode = Keys.F4)
    End Sub
End Class

Load :

Private Sub yourForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MainInterface.DisableAltF4(Me)
End Sub

, . , yourForm.vb, yourForm.Designer.vb, .

Public Class BaseForm
    Inherits Form

    Protected Overrides Sub OnLoad(e As System.EventArgs)
        MyBase.OnLoad(e)
        Me.KeyPreview = True
    End Sub

    Protected Overrides Sub OnKeyDown(e As System.Windows.Forms.KeyEventArgs)
        MyBase.OnKeyDown(e)
        e.Handled = e.Handled OrElse (e.Alt AndAlso e.KeyCode = Keys.F4)
    End Sub
End Class

yourForm.vb:

Public Class yourForm
    Inherits BaseForm

    ...code...
End Class

yourForm.Designer.vb:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class yourForm
    Inherits yourNamespace.BaseForm

    ...code...
End Class
+1

KeyPreview = True KeyDown:

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.Alt AndAlso e.KeyCode = Keys.F4 Then
        e.Handled = True
    End If
End Sub
+2

, RemoveMenu() interop. .

Of course, you can call Form.Close()in your code to close the form. This may be caused by the event handler of the Clickuser button, menu item, etc. In addition, you can implement System.Windows.Forms.IMessageFilterto process a custom key sequence (instead of ALT + F4) to close the form, for example. C + L + O + S + E.

0
source

Easy:

In c #

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Alt | Keys.F4))
    {
        return true;  // The key is manually processed
    }
    else
        return base.ProcessCmdKey(ref msg, keyData);
}

In VB.Net

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
    If keyData = (Keys.Alt Or Keys.F4) Then
        Return True
    Else
        Return MyBase.ProcessCmdKey(msg, keyData)
    End If
End Function
0
source

Source: https://habr.com/ru/post/1682988/


All Articles