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.

fdomig
  • 4,417
  • 3
  • 26
  • 39
  • 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. – Michael Graczyk Jul 16 '12 at 23:42
  • @MichaelGraczyk: Or at least, we assume that safely... :) – Chibueze Opata Jul 17 '12 at 00: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 '12 at 08:27
  • If you need to switch to YOUR application this answers your question: http://stackoverflow.com/a/32322918/463464 – Randall Flagg Sep 01 '15 at 03:17

4 Answers4

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.

noelicus
  • 14,468
  • 3
  • 92
  • 111
  • 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 '14 at 19:42
  • The documentation on SendWait suggests you can - let us know if it works! – noelicus Jul 28 '14 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 '14 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.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
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

Mark Arnott
  • 1,975
  • 3
  • 17
  • 28
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);
Sorceri
  • 466
  • 2
  • 4
  • 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 '12 at 21:30