18

I am searching for alternatives to the old User32.dll version of switching to a different application with FindWindow() and SetForegroundWindow().

I did find an alternative to the first with the usage of Process.GetProcessesByName() but I do not see the corresponding method to switch (set active/foreground) to that application.

Is there a way of doing that without using the old way with the User32.dll?

Thank you for your help.

EDIT

I accepted the answer of @Sorceri although it is not the answer I was looking for.

4
  • 2
    What's wrong with using SetForegroundWindow? The Windows OS keeps track of the foreground window in the Win32 subsystem, so however you do it is just going to be P/Invoking to user32.dll anyway. Jul 16, 2012 at 23:42
  • @MichaelGraczyk: Or at least, we assume that safely... :) Jul 17, 2012 at 0:26
  • 1
    On the Windows Desktop I have to use user32.dll on Windows CE I have to use coredll.dll. So using a DLLImport always shows that you add static dependency which is wrong.
    – fdomig
    Jul 17, 2012 at 8:27
  • If you need to switch to YOUR application this answers your question: stackoverflow.com/a/32322918/463464 Sep 1, 2015 at 3:17

4 Answers 4

39

Answer: No.

But, to help the next wonderer looking to find a window and activate it from C# here's what you have to do:

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

void ActivateApp(string processName)
{
    Process[] p = Process.GetProcessesByName(processName);

    // Activate the first application we find with this name
    if (p.Count() > 0)
        SetForegroundWindow(p[0].MainWindowHandle);
}

To bring notepad to the front, for example, you would call:

ActivateApp("notepad");

As a side note - for those of you who are trying to bring a window within your application to the foreground just call the Activate() method.

3
  • So after I use this method to bring up the desired process, could I feasibly use SendKeys.SendWait("{ENTER}"); with no qualms, provided my application actually responds to the keystrokes?
    – HanH1113
    Jul 28, 2014 at 19:42
  • The documentation on SendWait suggests you can - let us know if it works!
    – noelicus
    Jul 28, 2014 at 20:26
  • I've tried it and the weird thing is that it works SOMETIMES, which I find really strange for machines. The problem is always computers do what we tell them to, not necessarily what we want. I may have to ask a brand new question.
    – HanH1113
    Jul 28, 2014 at 20:37
3

You could use SetActiveWindow as an alternative to SetForeGroundWindow. I'd say you should go through all the Windows Manipulation Api Functions and see if there's something you're missing out.

Also, note that you can obtain the handle of the System.Diagnostics.Process object via the Process.Handle property.

3

An alternative to SetForeGroundWindow is VisualBasic's AppActivate

Call it like this

Microsoft.VisualBasic.Interaction.AppActivate("WindowTitle")

Just because it is in the VisualBasic namespace doesn't mean you can't use it in C#.

Full Documentation here

1

You can use System.Diagnostics.Process Object for a FindWindow equivalent. There currently is no equivalent for SetForegroundWindow. You will want use Pinvoke with SetForgroundWindow.

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
1
  • 1
    Which means I still have to deal with user32.dll after all? It does sound somehow weird that there is no alternative ...
    – fdomig
    Jul 16, 2012 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.