tags:

views:

49

answers:

1

I want to use API functions to set focus on one window under Windows (XP , Vista , 7 (whatever , any version who support .NET 3.0 or later)). So that when you press a key , the OS changes focus to another window.

Thanks

P.S: If you know any class in .NET 2.0 (or later) with which I can develop this solution I'd like you to tell me about it.

+1  A: 

You could p/invoke SetForegroundWindow. This will bring a window forward and focus on it.

This does require that you have the windows HWND.

As a side note, user's do not appreciate unexpectedly focusing a window; so use with caution. Additionally, Windows will not necessarily actually bring the window forward; it may instead just "flash" the title bar under some circumstances.

Kevin Montrose
Yes, flashing the title bar is supposed to be the correct (documented) behavior.
Ed Swangren
There are still conditions where the foreground window will change. They are documented. Generally, Window's tries to make it hard to abuse focus but its hardly 100% successful in that endeavor.
Kevin Montrose
Yeah, I know; one of our apps does this and I hate it.
Ed Swangren