Is there a reliable way to activate / set focus to a window using C#?

Posted by gtaborga on Stack Overflow See other posts from Stack Overflow or by gtaborga
Published on 2010-04-19T23:52:06Z Indexed on 2010/04/20 0:03 UTC
Read the original article Hit count: 247

Filed under:
|
|
|

Hey everyone,

I'm trying to find a reliable way to activate / set focus a window using C#. Currently I'm attempting to achieve this using the following Windows API calls:

SetActiveWindow(handle); SwitchToThisWindow(handle, true);

Previously I also had ShowWindow(handle, SW_SHOWMAXIMIZED); executing before the other 2, but removed it because it was causing strange behavior.

The problem I'm having with my current implementation is that occasionally the focus will not be set correctly. The window will become visible but the top part of it will still appear grayed out as if it wasn't in focus.

Is there a way to reliably do this which works 100% of the time, or is the inconsistent behavior a side-effect I can't escape? Please let me know if you have any suggestions or implementations that always work. Thank you for your time.

© Stack Overflow or respective owner

Related posts about focus

Related posts about Windows