Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set active window

Tags:

c#

winapi

I'm trying to make an app that gives a quake style drop-down HUD console. I can get it to show and hide the window, but I can't figure out how to set it as the active window after showing it. Im using Win API calls to show and hide the window. I've tried SetForegroundWindow(IntPtr hWnd) and SetFocus(IntPtr hWnd) to no avail. Anyone have any ideas?

http://pastebin.com/DgtJJGiv

public void ShowApp()
{
    IntPtr h = FindWindow(null, "C:\\Windows\\system32\\cmd.exe");
    ShowWindow(h, SW_SHOW);
    //EnableWindow(h, true);
    isHidden = false;
        // set focus to console window

    SetForegroundWindow(h);
    System.Diagnostics.Debug.WriteLine(h);
}
like image 982
Michael Avatar asked Jun 22 '11 03:06

Michael


1 Answers

I found an answer here: How to show form in front in C#

The winAPI approaches were not working correctly for me but this did:

form.TopMost = true;
form.TopMost = false;

I originally was only setting TopMost to true but I ran into problems with dialog boxes displaying behind the form. It appears that setting TopMost to true pulls the form to the front and holds it there. Setting it to false doesn't push it back but does allow other forms to be shown in front. I was still having problems with focus so I ended up going with the following:

form.Activate();
like image 84
Ryan Taylor Avatar answered Sep 30 '22 16:09

Ryan Taylor