Friday, October 5, 2007

Why ToolStrip responses to WM_MOUSEACTIVATE differently?

When a user clicks on a .Net ToolStrip (or MsoCommandBar) on which the top-level window is not focused, the window will be focused (duh) but the ToolStrip will not process the associated mouse event. For example, when a user clicks on the New button when Microsoft Outlook is not focused, the new message dialog will not appear.

This is apparently due to the ToolStrip returning MA_ACTIVATEANDEAT on WM_MOUSEACTIVATE. I found this when I was coding my ToolStrip based tab control. A simple fix would be to return MA_ACTIVATE instead:

protected const int WM_MOUSEACTIVATE = 0x0021;
protected const int MA_ACTIVATE = 1;
protected override void WndProc(ref Message m) {
   if (m.Msg == WM_MOUSEACTIVATE) {
       m.Result = new IntPtr(MA_ACTIVATE);
       return;
   }
   base.WndProc(ref m);
}


Annoying, and won't be cross platform, but at least it works!