Skip to content

Instantly share code, notes, and snippets.

@STOL4S
Last active November 7, 2023 21:58
Show Gist options
  • Save STOL4S/445e2715396e0a471021bdbe1bdef834 to your computer and use it in GitHub Desktop.
Save STOL4S/445e2715396e0a471021bdbe1bdef834 to your computer and use it in GitHub Desktop.
Get client bounds of another process running on the same system.
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
namespace ClientExtensions
{
public static class ClientBounds
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
private struct Rect
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}
public static Rectangle Get(string _ProcessName)
{
//NAME WITHOUT .EXE EXTENSION
Process[] Processes = Process.GetProcessesByName(_ProcessName);
if (Processes.Length > 0)
{
Process HookProcess = Processes[0];
IntPtr ProcessPtr = HookProcess.MainWindowHandle;
Rect ProcessRect = new Rect();
GetWindowRect(ProcessPtr, ref ProcessRect);
Rectangle DClientBounds = new Rectangle(
ProcessRect.Left, ProcessRect.Top, ProcessRect.Right - ProcessRect.Left,
ProcessRect.Bottom - ProcessRect.Top);
return DClientBounds;
}
else
{
throw new InvalidProgramException("A program with the name " + _ProcessName
+ " is not currently running on this device.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment