How to catch the event of the window close button(red X button on window right top corner) in wpf form?

前端 未结 6 1303
醉酒成梦
醉酒成梦 2021-01-03 18:22

How can I catch the event of the window close button(red X button on window right top corner) in a WPF form? We have got the closing event, window unloaded event also, but w

6条回答
  •  情话喂你
    2021-01-03 18:54

    In 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
    

    To disable the Form X button:

    '=====================================================
    ' 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
    

提交回复
热议问题