How do I set focus to an external application?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    How do I set focus to an external application?

    I've tried

    [DllImport("User 32.dll")]
    private static extern IntPtr SetFocus(Handle Ref hWnd);

    And it's not wanting to work. I've tried passing both the process handle and the main window handle to the method with this code

    SetFocus(new HandleRef(null, Process.MainWin dowHandle));

    And it just won't do it. I know I have the right handle because I'm using GetWindowRect() to get the size and location of the window, so why can't I set it's focus?
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Use SetForegroundWi ndow.

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      Ok that works, but only if the window in question is not minimized. Is there a way to detect the windowstate of an external application? I'm looking at GetWindowInfo() , but it returns a WINDOWINFO object and I don't know how to get at that.

      Comment

      Working...