首页 > 代码库 > vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方案

vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方案



vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方案


#Region "WPF 当浏览器窗口关闭时触发 Quit事件 "

#If OnSourceInitialized Then
    Protected Overrides Sub OnSourceInitialized(e As EventArgs) 'onload 等句柄创建后引用WSInitialized(Me, e)
        WSInitialized(Me, e)
        MyBase.OnSourceInitialized(e)
    End Sub
#Else

    Private Sub LyWebTabItem_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        WSInitialized(sender, e)
    End Sub
#End If

    Sub WSInitialized(sender As Object, e As EventArgs)
        Dim hs As HwndSource = PresentationSource.FromVisual(Me) '=HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);  
        hs.AddHook(New HwndSourceHook(AddressOf WndProc))
    End Sub

    ''' <summary>  
    ''' Overridden  
    ''' </summary>  
    ''' <param name="m">The <see cref="Message"/> send to this procedure</param>  
    <PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust")> _
    Protected Overloads Function WndProc(hwnd As IntPtr, msg As Int32, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr  '(ByRef m As Forms.Message)
        'Protected Overloads sub WndProc((ByRef m As Forms.Message)) 'winform方式
        'If m.Msg = DirectCast(WindowsMessages.WM_PARENTNOTIFY, Integer) Then
        If msg = DirectCast(WindowsMessages.WM_PARENTNOTIFY, Integer) Then
            'int lp = m.LParam.ToInt32();  
            'Dim wp As Integer = m.WParam.ToInt32()
            Dim wp As Integer = wParam.ToInt32()

            Dim X As Integer = wp And &HFFFF
            'int Y = (wp >> 16) & 0xFFFF;  
            If X = DirectCast(WindowsMessages.WM_DESTROY, Integer) Then
                Me.OnQuit()
            End If
        End If
        Return IntPtr.Zero
        'MyBase.WndProc(m)
    End Function
    ''' <summary>  
    ''' A list of all the available window messages  
    ''' </summary>  
    Enum WindowsMessages
        WM_ACTIVATE = &H6
        WM_ACTIVATEAPP = &H1C
        WM_AFXFIRST = &H360
        WM_AFXLAST = &H37F
        WM_APP = &H8000
        WM_ASKCBFORMATNAME = &H30C
        WM_CANCELJOURNAL = &H4B
        WM_CANCELMODE = &H1F
        WM_CAPTURECHANGED = &H215
        WM_CHANGECBCHAIN = &H30D
        WM_CHAR = &H102
        WM_CHARTOITEM = &H2F
        WM_CHILDACTIVATE = &H22
        WM_CLEAR = &H303
        WM_CLOSE = &H10
        WM_COMMAND = &H111
        WM_COMPACTING = &H41
        WM_COMPAREITEM = &H39
        WM_CONTEXTMENU = &H7B
        WM_COPY = &H301
        WM_COPYDATA = http://www.mamicode.com/&H4A>

原内容是C#代码WINFORM 搜索下就有,暂略