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 = &H4A
        WM_CREATE = &H1
        WM_CTLCOLORBTN = &H135
        WM_CTLCOLORDLG = &H136
        WM_CTLCOLOREDIT = &H133
        WM_CTLCOLORLISTBOX = &H134
        WM_CTLCOLORMSGBOX = &H132
        WM_CTLCOLORSCROLLBAR = &H137
        WM_CTLCOLORSTATIC = &H138
        WM_CUT = &H300
        WM_DEADCHAR = &H103
        WM_DELETEITEM = &H2D
        WM_DESTROY = &H2
        WM_DESTROYCLIPBOARD = &H307
        WM_DEVICECHANGE = &H219
        WM_DEVMODECHANGE = &H1B
        WM_DISPLAYCHANGE = &H7E
        WM_DRAWCLIPBOARD = &H308
        WM_DRAWITEM = &H2B
        WM_DROPFILES = &H233
        WM_ENABLE = &HA
        WM_ENDSESSION = &H16
        WM_ENTERIDLE = &H121
        WM_ENTERMENULOOP = &H211
        WM_ENTERSIZEMOVE = &H231
        WM_ERASEBKGND = &H14
        WM_EXITMENULOOP = &H212
        WM_EXITSIZEMOVE = &H232
        WM_FONTCHANGE = &H1D
        WM_GETDLGCODE = &H87
        WM_GETFONT = &H31
        WM_GETHOTKEY = &H33
        WM_GETICON = &H7F
        WM_GETMINMAXINFO = &H24
        WM_GETOBJECT = &H3D
        WM_GETTEXT = &HD
        WM_GETTEXTLENGTH = &HE
        WM_HANDHELDFIRST = &H358
        WM_HANDHELDLAST = &H35F
        WM_HELP = &H53
        WM_HOTKEY = &H312
        WM_HSCROLL = &H114
        WM_HSCROLLCLIPBOARD = &H30E
        WM_ICONERASEBKGND = &H27
        WM_IME_CHAR = &H286
        WM_IME_COMPOSITION = &H10F
        WM_IME_COMPOSITIONFULL = &H284
        WM_IME_CONTROL = &H283
        WM_IME_ENDCOMPOSITION = &H10E
        WM_IME_KEYDOWN = &H290
        WM_IME_KEYLAST = &H10F
        WM_IME_KEYUP = &H291
        WM_IME_NOTIFY = &H282
        WM_IME_REQUEST = &H288
        WM_IME_SELECT = &H285
        WM_IME_SETCONTEXT = &H281
        WM_IME_STARTCOMPOSITION = &H10D
        WM_INITDIALOG = &H110
        WM_INITMENU = &H116
        WM_INITMENUPOPUP = &H117
        WM_INPUTLANGCHANGE = &H51
        WM_INPUTLANGCHANGEREQUEST = &H50
        WM_KEYDOWN = &H100
        WM_KEYFIRST = &H100
        WM_KEYLAST = &H108
        WM_KEYUP = &H101
        WM_KILLFOCUS = &H8
        WM_LBUTTONDBLCLK = &H203
        WM_LBUTTONDOWN = &H201
        WM_LBUTTONUP = &H202
        WM_MBUTTONDBLCLK = &H209
        WM_MBUTTONDOWN = &H207
        WM_MBUTTONUP = &H208
        WM_MDIACTIVATE = &H222
        WM_MDICASCADE = &H227
        WM_MDICREATE = &H220
        WM_MDIDESTROY = &H221
        WM_MDIGETACTIVE = &H229
        WM_MDIICONARRANGE = &H228
        WM_MDIMAXIMIZE = &H225
        WM_MDINEXT = &H224
        WM_MDIREFRESHMENU = &H234
        WM_MDIRESTORE = &H223
        WM_MDISETMENU = &H230
        WM_MDITILE = &H226
        WM_MEASUREITEM = &H2C
        WM_MENUCHAR = &H120
        WM_MENUCOMMAND = &H126
        WM_MENUDRAG = &H123
        WM_MENUGETOBJECT = &H124
        WM_MENURBUTTONUP = &H122
        WM_MENUSELECT = &H11F
        WM_MOUSEACTIVATE = &H21
        WM_MOUSEFIRST = &H200
        WM_MOUSEHOVER = &H2A1
        WM_MOUSELAST = &H20A
        WM_MOUSELEAVE = &H2A3
        WM_MOUSEMOVE = &H200
        WM_MOUSEWHEEL = &H20A
        WM_MOVE = &H3
        WM_MOVING = &H216
        WM_NCACTIVATE = &H86
        WM_NCCALCSIZE = &H83
        WM_NCCREATE = &H81
        WM_NCDESTROY = &H82
        WM_NCHITTEST = &H84
        WM_NCLBUTTONDBLCLK = &HA3
        WM_NCLBUTTONDOWN = &HA1
        WM_NCLBUTTONUP = &HA2
        WM_NCMBUTTONDBLCLK = &HA9
        WM_NCMBUTTONDOWN = &HA7
        WM_NCMBUTTONUP = &HA8
        WM_NCMOUSEHOVER = &H2A0
        WM_NCMOUSELEAVE = &H2A2
        WM_NCMOUSEMOVE = &HA0
        WM_NCPAINT = &H85
        WM_NCRBUTTONDBLCLK = &HA6
        WM_NCRBUTTONDOWN = &HA4
        WM_NCRBUTTONUP = &HA5
        WM_NEXTDLGCTL = &H28
        WM_NEXTMENU = &H213
        WM_NOTIFY = &H4E
        WM_NOTIFYFORMAT = &H55
        WM_NULL = &H0
        WM_PAINT = &HF
        WM_PAINTCLIPBOARD = &H309
        WM_PAINTICON = &H26
        WM_PALETTECHANGED = &H311
        WM_PALETTEISCHANGING = &H310
        WM_PARENTNOTIFY = &H210
        WM_PASTE = &H302
        WM_PENWINFIRST = &H380
        WM_PENWINLAST = &H38F
        WM_POWER = &H48
        WM_PRINT = &H317
        WM_PRINTCLIENT = &H318
        WM_QUERYDRAGICON = &H37
        WM_QUERYENDSESSION = &H11
        WM_QUERYNEWPALETTE = &H30F
        WM_QUERYOPEN = &H13
        WM_QUEUESYNC = &H23
        WM_QUIT = &H12
        WM_RBUTTONDBLCLK = &H206
        WM_RBUTTONDOWN = &H204
        WM_RBUTTONUP = &H205
        WM_RENDERALLFORMATS = &H306
        WM_RENDERFORMAT = &H305
        WM_SETCURSOR = &H20
        WM_SETFOCUS = &H7
        WM_SETFONT = &H30
        WM_SETHOTKEY = &H32
        WM_SETICON = &H80
        WM_SETREDRAW = &HB
        WM_SETTEXT = &HC
        WM_SETTINGCHANGE = &H1A
        WM_SHOWWINDOW = &H18
        WM_SIZE = &H5
        WM_SIZECLIPBOARD = &H30B
        WM_SIZING = &H214
        WM_SPOOLERSTATUS = &H2A
        WM_STYLECHANGED = &H7D
        WM_STYLECHANGING = &H7C
        WM_SYNCPAINT = &H88
        WM_SYSCHAR = &H106
        WM_SYSCOLORCHANGE = &H15
        WM_SYSCOMMAND = &H112
        WM_SYSDEADCHAR = &H107
        WM_SYSKEYDOWN = &H104
        WM_SYSKEYUP = &H105
        WM_TCARD = &H52
        WM_TIMECHANGE = &H1E
        WM_TIMER = &H113
        WM_UNDO = &H304
        WM_UNINITMENUPOPUP = &H125
        WM_USER = &H400
        WM_USERCHANGED = &H54
        WM_VKEYTOITEM = &H2E
        WM_VSCROLL = &H115
        WM_VSCROLLCLIPBOARD = &H30A
        WM_WINDOWPOSCHANGED = &H47
        WM_WINDOWPOSCHANGING = &H46
        WM_WININICHANGE = &H1A
    End Enum

    ''' <summary>
    ''' Raises the <see cref="Quit"/> event
    ''' </summary>
    Protected Sub OnQuit()
#If OnSourceInitialized Then
        CommandBinding_Close_Executed(Me, Nothing)
#Else
        MenuCloseTab_Click(Me, Nothing)
#End If
        RaiseEvent Quit(Me, EventArgs.Empty)
    End Sub

    ''' <summary>
    ''' Raised when the browser application quits
    ''' </summary>
    ''' <remarks>
    ''' Do not confuse this with DWebBrowserEvents2.Quit... That's something else.
    ''' </remarks>
    Public Event Quit As EventHandler
#End Region

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

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

时间: 2024-11-12 06:40:24

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 LyWeb

解决移动端页面滚动后不触发touchend事件

解决移动端页面滚动后不触发touchend事件 问题 在移动端页面进行优化时,一般使用touch事件替代鼠标相关事件.用的较多的是使用touchend事件替代PC端的click和mouseup事件. 可是,touchend事件在页面滚动时有个问题.在滚动完毕后,假设当前触点的位置所指的元素绑定了touchend事件,这时便会触发该元素的touchend事件,造成误操作. 解决方法 解决方法非常easy.就是在页面滚动时停止touchend事件冒泡.这样就能够防止触发touchend事件. 用法

进入网页开始计时,关闭页面时触发操作事件

<html> <head> <title>页面停留时间</title> </head> <body onload="init(); window.setTimeout('show_secs()',1);" > <script language="javascript"> var ap_name = navigator.appName; var ap_vinfo = navigator

iOS设置UITableView中Cell被默认选中后怎么触发didselect事件

//默认选中某个cell [self.searchResultTV selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone]; //实现该cell的didSelectRowAtIndexPath方法 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0

WPF WebBrowser Memory Leak 问题及临时解决方法

首先介绍一下内存泄漏(Memory Leak)的概念,内存泄露是指程序中已动态分配的堆内存由于某种原因未释放或者无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果. 最近在使用WPF WebBrowser时,就遇到了Memory Leak的问题. 在主窗体上通过一个按钮点击事件加载包含有WebBrowser控件的窗体,使用这个WebBrowser来浏览网页,然后调用WebBrowser的Dispose()方法,然后调用GC.Collect(),最后关闭当前包含有WebBro

WPF中实现先登录后启动主程序的方法

[转载] http://blog.csdn.net/swarb/article/details/17301167 WPF中实现先登录后启动主程序的方法 我觉得先登录后启动应用主程序是一个很经典的问题,基本上如果要写一个应用程序都会用到这个的小环节.我在这个问题上挣扎了大半天才找到解决方案,我的实现方法我觉得有点不正宗,如果有哪位高手知道更好的方法欢迎留言指导!! 首先来说一下传统C#在WinForm中的实现方法,基本上是在Main函数中根据第一个启动窗口的DialogResult来判断是否实例第

WPF应用程序防止关闭LiteDB数据库文件加载事件

在WPF应用程序的主窗口和它的Window.Loaded事件我得到一些数据从LiteDB数据库文件. var groupViewModel = new GroupsViewModel();ComboBoxGroupsName.ItemsSource = groupViewModel.GetGroups();的GetGroups方法是这样的: IEnumerable<GroupModel> groups;using (var db = new LiteDatabase(DbFilePath)){

[WPF自定义控件]?Window(窗体)的UI元素及行为

原文:[WPF自定义控件]?Window(窗体)的UI元素及行为 1. 前言 本来打算写一篇<自定义Window>的文章,但写着写着发觉内容太多,所以还是把使用WindowChrome自定义Window需要用到的部分基础知识独立出来,于是就形成了这篇文章. 无论是桌面编程还是日常使用,Window(窗体)都是最常接触的UI元素之一,既然Window这么重要那么多了解一些也没有坏处. 2.标准Window 这篇文章主要讨论标准的Window,不包括奇形怪状的无边框.非矩形Window,即只讨论W

WPF WebBrowser

XAML <Window x:Class="WpfApplication5.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="340" Width