Set active window

I am trying to create an application that issues a HUD dropdown console. I can make it show and hide the window, but I cannot figure out how to set it as the active window after it is shown. I use Win API calls to show and hide a window. I tried SetForegroundWindow (IntPtr hWnd) and SetFocus (IntPtr hWnd) to no avail. Does anyone have any idea?

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); } 
+7
source share
4 answers

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

The winAPI approaches did not work correctly for me, but this happened:

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

I initially only set TopMost to true, but I had problems displaying dialog boxes behind the form. Setting TopMost to true seems to bring the form to the fore and hold it there. Setting it to false does not repel it, but allows other forms to be shown in front. I still had problems with focus, so I ended up working with the following:

 form.Activate(); 
+3
source

Try it (works for me):

 public static void ShowApp() { IntPtr h = FindWindow(null, "C:\\Windows\\system32\\cmd.exe"); ShowWindow(h, ShowWindowCommands.Show); SetForegroundWindow(h); SetFocus(h); System.Diagnostics.Debug.WriteLine(h); } 
+1
source

You can use the SetActiveWindow method. Hope this helps ...

+1
source

Is there a reason why you cannot implement your own console window? I mean a simple form with a text box set to the correct style. You will probably have more control over how this works than trying to use the cmd process.

Just a thought.

-one
source

All Articles