Handle Mouse / Keyboard Events in Office Object Model

OUTSIDE OF GFW:http://aritrasaha.wordpress.com/2009/04/02/handle-mouse-keyboard-events-in-office-object-model/

 

The Office Object Model does not expose any scroll, mouse or keyboard events and the only option to use any of these is by hooking into the Windows API. This essentially involves the following steps:-

1.       Subclass the Workbook window (Excel) or Document window (Word) by creating a class that derives from System.Windows.Forms.NativeWindow

 

public class ScrollEvent : System.Windows.Forms.NativeWindow

 

{

 

}    

 

2.      Override the WndProc() method to handle the messages raised e.g. WM_HSCROLL / WM_VSCROLL for horizontal and vertical scrolling respectively, WM_MOUSEWHEEL for Mouse Wheel Scroll, etc. 

Detailed list of all the Windows Messages and their hex-codes are available at:-http://wiki.winehq.org/List_Of_Windows_Messages.

public class ScrollEvent : System.Windows.Forms.NativeWindow

      {        

        protected override void WndProc(ref System.Windows.Forms.Message m)

        {

            // *always* let the base class process the message

            base.WndProc(ref m);

            const int WM_VSCROLL = 0×115;

            const int WM_HSCROLL = 0×114;

if (m.Msg == WM_VSCROLL)

                {

                   // do something when the document is scrolled vertically

                }

                else if (m.Msg == WM_HSCROLL)

                {

                   // do something when the document is scrolled horizontally

                }      

   }

}

3.       Hook the class to the Document or Workbook window by creating an instance of the class and call the AssignHandle()method – passing the handle to the Document or Workbook window 

 

 

ScrollEvent sRev = new ScrollEvent();

sRev.AssignHandle(h);

// h is of type IntPtr , and the handle of the Word Document or Excel Workbook window 

To find the handle of the document / workbook window

To find the parent window we need to use the native method FindWindowExW() function present in user32.dll :-

public partial class NativeMethods

    {

        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "FindWindowExW")]

        public static extern System.IntPtr FindWindowExW([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWndParent, [System.Runtime.InteropServices.InAttribute()] System.IntPtr hWndChildAfter, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]string lpszClass, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]string lpszWindow);

}

After this, use SPY++ to retrieve the windows Class and Caption Information, then call FindWindowExW to retrieve the handle.

For Word Document Window:-

   IntPtr h = Process.GetCurrentProcess().MainWindowHandle; 

   h = NativeMethods.FindWindowExW(h, new IntPtr(0), "_WwF", ""); 

   h = NativeMethods.FindWindowExW(h, new IntPtr(0), "_WwB", "Document1"); 

   ScrollEvent s = new ScrollEvent(); 

   s.AssignHandle(h);

The Mouse Wheel scroll messages are raised in Word from the Child Window with Class Name "_WwG" and Caption "Microsoft Word Document". Hence, we need to add the following line of code before the AssignHandle() method call.

h = NativeMethods.FindWindowExW(h, new IntPtr(0), "_WwG", "Microsoft Word Document");

For Excel Document Window:-

      IntPtr h = Process.GetCurrentProcess().MainWindowHandle; 

   h = NativeMethods.FindWindowExW(h, new IntPtr(0), "XLDESK", ""); 

   h = NativeMethods.FindWindowExW(h, new IntPtr(0), " EXCEL7", "Book1"); 

   ScrollEvent s = new ScrollEvent(); 

   s.AssignHandle(h);

posted @ 2012-03-26 20:44  BinSys  阅读(371)  评论(0编辑  收藏  举报