9

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?

Shawn
  • 5,130
  • 13
  • 66
  • 109
  • Here's a similar question in C++ whose answers may help you: http://stackoverflow.com/questions/1888863/how-to-get-main-window-handle-from-process-id – adv12 Sep 12 '16 at 20:13
  • Have a look at this prior SO answer....http://stackoverflow.com/questions/1888863/how-to-get-main-window-handle-from-process-id It looks like it offers a solution to your issue. – STLDev Sep 12 '16 at 20:17

1 Answers1

12

Just use the Process class.

Process.GetProcessById(YourProcessID).MainWindowHandle
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Dispersia
  • 1,436
  • 9
  • 23
  • Thanks you MethodMan, That's exactly what I need: get handle by process id. Then I can do the minimize work with the handle. Thanks again. – Shawn Sep 12 '16 at 20:26