Link to home
Start Free TrialLog in
Avatar of dominicwong
dominicwong

asked on

How to prevent background winform application from stealing focus

Hi experts
My winform application consist of menu items and menu buttons.
While my application is running, I cover half of it by another program so that my application has lost its focus and goes to the background.

The problem is when my mouse hovers over the menu items and menu buttons of my (background) application, it would steal focus (by highlighting itself) even though it is still half-covered by another program.

I don't want my application to steal any focus while running on the background. I'd tried this in my main form but doesn't work:
protected override bool ShowWithoutActivation { get { return true; } }

Open in new window


Any idea please. Thanks in advance.
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

First of all a form cannot have the focus unless its shows only controls that cannot have the focus. This would typically be a form that contains only Labels control. As soon as you have an enabled TextBox or a ComboBox or any enabled focusable control on the form, it's one of these controls that has the Focus, not the form.

Also, as a side information, the notion of Focus is often misunderstood. There is not one focus, but there are many. Each form has one focus, on its control that was last used.

Thus, most forms never get the Focus. They got Activated or Deactivated, but they don't get the focus. And you can have only one form activated at a time.

Hovering over a form should not steal the activated state from the currently activated form. The user has to click on a form or one of its controls to activate it and "steal" the activation from another one.

Are you sure that you do not react to some event such as MouseMove or MouseOver in your form?

What I would do is put a breakpoint in the Activated event of the form that causes the problem. When you hit the breakpoint, give a look at the Call Stack in the debugger. You will see what was called in the application just before the Activated event, and it should point to what causes the activation.
Avatar of dominicwong
dominicwong

ASKER

Thanks James Burger for your quick response.
I apologize that I didn't explain my issue clearly.

It wasn't the form of my application that had stolen the focus.
What I meant was the menu items and the menu buttons on my form that had stolen the focus when my mouse hovered over these controls while the form itself is on the background.
 (The form itself didn't steal the focus. These controls on the form did.)

What I want is that these controls will not steal any focus while the form is still in the background.

I've checked that I haven't got any handlers to MouseMove or MouseHover for these controls.

Thanks again.
Avatar of ste5an
It's not clear what your problem is.

But the solution is simple: Just deactivate all controls on your form, when it gets deactivated (loses focus).  Enable them, when your form gets activated.
Sorry for my own late answer, I was out of town, in another environment, and somehow your second post did not reach me.

A Control cannot steal the focus from another contorl or deactivate another form unless you click on it, move to it with Tab or get activated through a shortcut.

Menu items and buttons in a Windows Form are highlighted when you mouse over them, a good way to show the user which one would be activated if he clicks. But they do not get the focus unless you click on them.

If what you mean by "steal the focus" is what it really means, then there is some code somewhere that does it.

You should not only give a look to the MouseHover or MouseMove of the menus themselves. To go to any control, you need to mouse over the form itself and any other control that is in the path of the mouse cursor. So MouseHover and MouseMove events from the form or another control could also be the culprit. And I told you about these 2 events because they are the most evident from your description of the problem. It can come from somewhere else.

Are there Select, Activate or Focus calls in your application? You might have a Timer that triggers the thing.

One thing is sure, you cannot steal the focus from a control or deactivate another form only by hovering over a control unless there is code that does it somewhere.
Thanks James for pointing out the difference between items "stealing focus" and "got highlighted".

My problem is the latter.  I noticed that items and buttons in different applications responded differently to mouse hovering. I did an experiment on (1) Microsoft Outlook and Microsoft VS2008, (2) Internet Explorer and Microsoft VS2008.

When the Microsoft Outlook had the focus, hovering mouse over the Microsoft VS2008 did not highlight any menu items or buttons on the Microsoft VS2008. (The only items got highlighted were the Minimize/Maximize/Close buttons at the top right hand corner of the Microsoft VS2008 ).

When the Microsoft VS2008 had the focus, hovering mouse over the Internet Explored highlighted every buttons on the Internet Explorer.

Is there any way to make menu items or buttons not to get highlighted to mouse hovering (like the Microsoft VS2008) when the application is not under focus?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks James. I will take your advice and leave it as is. Thanks again for your help. Much appreciated!