Shawn Shawn - 1 year ago 186
C# Question

C# How to minimize another application by a given process id?

I would like to minimize an application by its process id. I have searched on SO and found the following code

private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

public void MinimizeWindow()
IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "NotePad");
ShowWindow(hwnd, SW_MINIMIZE);

But I don't want to find window by caption because the caption for an app changes. I have a given process id that is coming from another module that request minimize the app with the given process id.

Is there such a thing like this?

public static extern IntPtr FindWindowByProcess(IntPtr ZeroOnly, int lpProcessID);

Or if not, anyway to go around?

Answer Source

Just use the Process class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download