tags:

views:

289

answers:

1

I have an application that will mainly operate in the background. I want certain system events that it handles to cause the app to steal focus from the foreground app, whatever that is. But UIElement.Focus() seems to only set the focus within the WPF app. That is, if the WPF app already has the focus as a whole, then Focus() sets the focus to the right control. But I need to steal focus from another app and put it on my app. How can I do this?

I do not want a system modal dialog. That is, after the window steals focus, it should be possible for the user to immediately switch to another app without dismissing my app's window.

The app may already been in a restored, minimized, or even "minimize to tray" state when it needs to steal system focus. So assuming that it is minimized and stealing focus while restoring the window is not acceptable.

+3  A: 

If you don't mind doing som p/invokes, the SetForegroundWindow function should be able to help you:
http://pinvoke.net/default.aspx/user32/SetForegroundWindow.html

Tormod Fjeldskår
It is also worth reading the remarks in the SetForegroundWindow documentation: http://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx In some cases Windows prevents background windows from forcing themselves on top of other windows.
Mikko Rantanen