我想转发MouseWheel
在表单级别生成的事件,以便它们将由嵌入式处理WebBrowser
控件,即使该控件没有焦点。
这是我所做的:
- 实施的
IMessageFilter.PreFilterMessage
.
- 注册了过滤器
Application.AddMessageFilter
.
- 在过滤器中,聆听
WM_MOUSEWHEEL
消息。
- 使用转发消息
SendMessage
到目标控件(在我的例子中WebBrowser
).
在代码中,这看起来像这样:
bool IMessageFilter.PreFilterMessage(ref Message m)
{
if (m.Msg == 0x20A) // WM_MOUSEWHEEL
{
if (this.target != null)
{
var handle = this.target.Handle;
Native.SendMessage (handle, m.Message, m.WParam, m.LParam);
return true;
}
}
return false;
}
// Registering the message filter:
System.Windows.Forms.Application.AddMessageFilter (this);
// Win32 code:
protected static class NativeMethods
{
[System.Runtime.InteropServices.DllImport ("user32.dll")]
public static extern System.IntPtr SendMessage(System.IntPtr hWnd, System.Int32 Msg, System.IntPtr wParam, System.IntPtr lParam);
}
这是行不通的。什么都没发生。
但是,如果不是WebBrowser
我指定一个Panel
作为目标,那么这效果非常好。
皮埃尔的答案对我有用(由于声誉不够,无法投票)。然而,它需要进行一些调整才能在 VB.NET 中工作,所以我想我应该发帖以防有人陷入这一点:
Imports System.Runtime.InteropServices
Public Class Form1 Implements IMessageFilter
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
System.Windows.Forms.Application.AddMessageFilter(Me)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.WebBrowser1.Navigate("D:\Development\test3.html")
End Sub
Private Function IMessageFilter_PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
If m.Msg = &H20A Then
' WM_MOUSEWHEEL
If m.HWnd <> 0 Then
Dim handle = m.HWnd
handle = NativeMethods.FindWindowEx(handle, IntPtr.Zero, "Shell Embedding", Nothing)
handle = NativeMethods.FindWindowEx(handle, IntPtr.Zero, "Shell DocObject View", Nothing)
handle = NativeMethods.FindWindowEx(handle, IntPtr.Zero, "Internet Explorer_Server", Nothing)
NativeMethods.SendMessage(handle, m.Msg, m.WParam, m.LParam)
Return True
End If
End If
Return False
End Function
Protected NotInheritable Class NativeMethods
Private Sub New()
End Sub
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Public Shared Function SendMessage(hWnd As System.IntPtr, Msg As System.Int32, wParam As System.IntPtr, lParam As System.IntPtr) As System.IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Public Shared Function FindWindowEx(hwndParent As System.IntPtr, hwndChildAfter As System.IntPtr, className As String, windowName As String) As System.IntPtr
End Function
End Class
End Class
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)