Detecting mouse button down & up events on the system menu of form.

Visual Studio , .Net , WinForms , CSharp , Visual Basic India
  • 7 years ago

    How do I detect left mouse button down and up events on the 'Close' option of the system menu that appears when the mouse is clicked on the top left corner of a form?

  • 6 years ago

    Here’s what I found. Though it’s not exactly what I originally posted for but at least it narrows down to my requirement.

    Instead of identifying separately if the user clicked the Close button at the top right corner of the title bar of the form or the ‘Close’ option in the system menu, or he/she pressed the Alt+F4 keys, we can combine these factors together to check if the user is consuming the system command (i.e. any of the above three techniques) to close the form or is doing so programmatically. If the system command is used, action is taken accordingly; else we ignore.

    A button is used to close down the form programmatically without any interruption.

    Public Class Form2
        Private Const WM_SYSCOMMAND As Integer = &H112
        Private Const SC_CLOSE As Integer = &HF060
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            ' Check if the user is consuming the system command to close the form.
            If (m.Msg = WM_SYSCOMMAND) AndAlso (CInt(m.WParam) = SC_CLOSE) Then
                MsgBox("Form closing by system command. Take necessary actions here...")
            End If
    
            MyBase.WndProc(m)
        End Sub
    
        Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
        End Sub
    End Class
    

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow