private NativeMethods.Rectangle GetTargetProcessWindowData() { // What to capture. if (windowsSourceHandle == IntPtr.Zero) { windowsSourceHandle = GetWindowHandle(Settings.ProcessNameScreen); } // Get capture area. NativeMethods.GetWindowRect(windowsSourceHandle, out NativeMethods.Rectangle captureAreaTemp); // We need to get real area of what we need to capture, excluding window title border etc. So getting the correction. NativeMethods.ClientToScreen(windowsSourceHandle, out NativeMethods.Point pnt); // Left and right borders of the window if it has any var windowMetricsCorrectionX = (pnt.x - captureAreaTemp.left) * 2; // Caption + bottom border of the window if it has any var windowMetricsCorrectionY = (pnt.y - captureAreaTemp.top) + (pnt.x - captureAreaTemp.left); // Calculate the window size taking offsets from 0,0 and also applying correction. var rect = new NativeMethods.Rectangle { left = captureAreaTemp.left + (pnt.x - captureAreaTemp.left), right = (captureAreaTemp.right - captureAreaTemp.left) - windowMetricsCorrectionX, top = captureAreaTemp.top + (pnt.y - captureAreaTemp.top), bottom = (captureAreaTemp.bottom - captureAreaTemp.top) - windowMetricsCorrectionY }; return(rect); }
private void KeepWindowInPlace() { targetWindowRect = GetTargetProcessWindowData(); if (targetWindowRect.bottom == 0) { Location = new Point(0, 0); Width = 1600; Height = 900; } else { Location = new Point(targetWindowRect.left, targetWindowRect.top); Width = targetWindowRect.right; Height = targetWindowRect.bottom; } }