Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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);
[DllImport("user32.dll")]
[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?

like image 979
Shawn Avatar asked Sep 12 '16 20:09

Shawn


1 Answers

Just use the Process class.

Process.GetProcessById(YourProcessID).MainWindowHandle
like image 116
Dispersia Avatar answered Sep 18 '22 14:09

Dispersia