views:

38

answers:

1
+1  Q: 

AppBar focus issue

I have created an AppBar. The AppBar always stays on top, when it loses focus, then to get back to the AppBar requires two clicks instead of one. I believe the first click activates the form and then with second click I receive the mouse click events (as expected). Can you give any ideas, what can be wrong here? My problem is similar to this one Appbar and focus issues. Unfortunately the OP in that link hasn't posted his solution, sad.

I created the AppBar following this article from CodeProject C# does Shell, Part 3.

I looked into it with Spy++ but don't have any idea why this is happening. Here is the Spy++ log which I obtained testing out this scenario (first click doesn't works, second does).

<00001> 00090CFE S WM_PARENTNOTIFY fwEvent:WM_LBUTTONDOWN xPos:25 yPos:17
<00002> 00090CFE R WM_PARENTNOTIFY
<00003> 00090CFE S WM_WINDOWPOSCHANGING lpwp:0418EAE4
<00004> 00090CFE R WM_WINDOWPOSCHANGING
<00005> 00090CFE S WM_ACTIVATEAPP fActive:True dwThreadID:00000000
<00006> 00090CFE R WM_ACTIVATEAPP
<00007> 00090CFE S WM_NCACTIVATE fActive:True
<00008> 00090CFE R WM_NCACTIVATE
<00009> 00090CFE S WM_ACTIVATE fActive:WA_CLICKACTIVE fMinimized:False hwndPrevious:(null)
<00010> 00090CFE S WM_IME_SETCONTEXT fSet:1 iShow:C000000F
<00011> 00090CFE S WM_IME_NOTIFY dwCommand:IMN_OPENSTATUSWINDOW dwCommand:00000002 dwData:00000000
<00012> 00090CFE R WM_IME_NOTIFY
<00013> 00090CFE R WM_IME_SETCONTEXT
<00014> 00090CFE S WM_SETFOCUS hwndLoseFocus:(null)
<00015> 00090CFE R WM_SETFOCUS
<00016> 00090CFE R WM_ACTIVATE

Now happens the second click (which works)
<00017> 00090CFE S WM_PARENTNOTIFY fwEvent:WM_LBUTTONDOWN xPos:25 yPos:17
<00018> 00090CFE R WM_PARENTNOTIFY
<00019> 00090CFE S WM_WINDOWPOSCHANGING lpwp:0418E40C
<00020> 00090CFE R WM_WINDOWPOSCHANGING

Looking at the Spy++ logs, I believe the problem is with activation, I think that it only receives WM_LBUTTONDOWN event if the window is activated. But how come other windows aren't activated but they still work with one click (i.e. I don't have to click it first).

EDIT: I think the problem is with the ToolStrip. I have a ToolStrip in my AppBar. Now how I verified this? Well on the same form I created a button and showed a message box in its click event and it is working fine. Now how to remedy that?

A: 

Alrighty, I figured it out finally and (in the interest of posterity) this is what's happening. The issue is not with the Form rather with the ToolStrip (can be reproduced in Word, I believe). This feature is by design and not a bug. In the WM_MOUSEACTIVATE message, inside ToolStrip's WndProc, it returns the MA_ACTIVATEANDEAT which activates your window, i.e. gives it focus, but discards the mouse message that's why we have to click twice because the first mouse message is discarded.

Now the solution? Override the WndProc of ToolStrip in your derived class and instead of setting the Message.Result property to MA_ACTIVATEANDEAT, set it to MA_ACTIVATE. Here is a tutorial on how to do it. How to enable "click through" for .NET 2.0 ToolStrip and MenuStrip

Hope that helps :)

hab