获取C#中的活动窗口坐标和高度宽度

我只是在这里查看一些post,但没有一个对我有帮助。

我想要做的是运行Screen Capturing的后台进程。 现在我想要一段代码,它会给我X,Y或任何打开的Active / Current Window(Say Notepad)及其高度和宽度。

就是这样,没有别的。

[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); private IntPtr GetActiveWindow() { IntPtr handle = IntPtr.Zero; return GetForegroundWindow(); } 

然后使用GetWindowRect获取窗口位置。

 [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } 
  [DllImport("user32.dll")] private static extern bool SetProcessDPIAware(); [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect); static void Main() { SetProcessDPIAware(); Rectangle t2; GetWindowRect(GetForegroundWindow(),out t2); }