How to find the window MS Word in order to sub-class and override WndProc

190 Views Asked by At

I need to use custom event processing for ebedded Office applications in a WebBrowser ActiveX control. I have made great progress using the WebBrowser as a container for Word, Excel and PowerPoint and so far with some work I'm able to get almost all the behavior I require. However, the one thing I need to be able to do is to detect mouse clicks in a document and differentiate those that are on hyperlinks from those that are not.

I've been able to get close to a solution using a global hook but the process/logic I use for Word doesn't work for Excel.

In any case, after reading this I decided overriding WndProc might be a better approach. Using the following code I put together a NativeWindow sub-class and an override for the WndProc. Unfortunately, it is never called so I assume I'm not using the correct window handle to instantiate it.

I use the class like this:

wordDocument.Application.ActiveDocument.ActiveWindow.SetFocus()
hWndOffice = GetFocus()
officeWindow = New OfficeWindow(hWndOffice)

I'm using VB .NET but not VSTO...

Any ideas how to get the correct window handle?

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

End Class

0

There are 0 best solutions below