我需要对 WebBrowser ActiveX 控件中的嵌入式 Office 应用程序使用自定义事件处理。使用 WebBrowser 作为 Word、Excel 和 PowerPoint 的容器,我取得了很大的进步,到目前为止,通过一些工作,我能够获得几乎所有我需要的行为。但是,我需要做的一件事是检测文档中的鼠标点击,并将超链接上的点击与非超链接上的点击区分开来。
我已经能够使用全局挂钩接近解决方案,但我用于 Word 的流程/逻辑不适用于 Excel。
无论如何,在阅读完这篇文章后,我决定重写 WndProc 可能是一种更好的方法。使用以下代码,我将 NativeWindow 子类和 WndProc 的覆盖放在一起。不幸的是,它从未被调用,所以我假设我没有使用正确的窗口句柄来实例化它。
我使用这样的类:
wordDocument.Application.ActiveDocument.ActiveWindow.SetFocus()
hWndOffice = GetFocus()
officeWindow = New OfficeWindow(hWndOffice)
我正在使用 VB .NET 但不是 VSTO ......
任何想法如何获得正确的窗口句柄?
Friend Class OfficeWindow
Inherits NativeWindow
Implements IDisposable
Public Sub New(handle As IntPtr)
Me.AssignHandle(handle)
End Sub
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Console.WriteLine(m.Msg)
Select Case m.Msg
Case &H200
Console.WriteLine("WM_MOUSEMOVE")
End Select
MyBase.WndProc(m)
End Sub
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False
' Public implementation of Dispose pattern callable by consumers.
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
' Protected implementation of Dispose pattern.
Protected Overridable Sub Dispose(disposing As Boolean)
If disposed Then Return
If disposing Then
' Free any other managed objects here.
'
Me.DestroyHandle()
End If
' Free any unmanaged objects here.
'
disposed = True
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
End Sub
结束类