One of the things we have wanted to do in Word is get an event just before a drop-down menu is displayed. Word has an event just before a RMB popup menu is displayed, but nothing for the main menu.
BTW - we need it for functionality like enabling/disabling the cut/copy menu items, they are enabled/disabled based on the selection state. But in our case, the code to determine if the menu item should be enabled/disabled is slow so we don't want to call it every time the caret moves.
The natural answer is to enable/disable the menu items just before they are displayed. But, easier said than done. Word draws it's own menus and generates its own events.
So I intercepted the Word message pump. Still not great. If you grab the message pump for the menu window, there are events that are generated after the menu is displayed. Calling menu.Enabled=true/false does no good. That just affects how the menu is displayed nest time.
Intercepting the OpusApp (main) window is better. You will get the WM_PARENTNOTIFY message from the left mouse click. But you get that message for a left mouse click anywhere. I added a hack that assumes the menu is at the top of the window. So if someone moves their window, this won't work.
As for bringing up the menu with keyboard accelerators, I couldn't find any way to see that before the menu was displayed. So for that you are SOL.
I am hoping that someone else will look at this and find a way to improve it. If so, please post a comment here!
The sample code is at http://www.windward.net/MyWordAddin.zip
Do you find this useful? If so please check out Windward Reports.

Hi Dave - Is this source code still available? I am trying to intercept the message pump in Word 2007. I am able to get a handle to main Word window as follows:
MyNativeWindow w = new MyNativeWindow();
IntPtr wordWindow = MyNativeMethods.FindWindow("OpusApp", this.Application.ActiveWindow.Caption + " - Microsoft Word");
w.AssignHandle(wordWindow);
MyNativeWindow is a simple subclass of NativeWindow:
public class MyNativeWindow : System.Windows.Forms.NativeWindow
{
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// Some logging code here to capture messages
base.WndProc(ref m);
}
}
I am successfully getting the handle to the Word app, but I am only getting one WM_PARENTNOTIFY message no matter what I do in Word 2007. I can click around, open documents, etc, but a single WM_PARENTNOTIFY message is sent on start-up.
Any clue what I'm missing here?
Thanks!
Posted by: Bryan | July 20, 2010 at 04:15 PM