Skip to content

Commit

Permalink
Fix window capture not working on Windows 7
Browse files Browse the repository at this point in the history
  • Loading branch information
pol-rivero committed Sep 11, 2023
1 parent c42c40f commit 59cb19a
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions DiscordAudioStream/ScreenCapture/CaptureStrategy/WindowCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,40 @@ protected static Rectangle GetWindowArea(IntPtr windowHandle)
{
// Get size of client area (don't use X and Y, these are relative to the WINDOW rect)
bool success = User32.GetClientRect(windowHandle, out User32.Rect clientRect);
if (!success)
CheckSuccess(success);

User32.Rect frame;
try
{
throw new InvalidOperationException("Window was closed");
// Get frame size and position (generally more accurate than GetWindowRect)
frame = Dwmapi.GetRectAttr(windowHandle, Dwmapi.DwmWindowAttribute.EXTENDED_FRAME_BOUNDS);
}

// Get frame size and position (generally more accurate than GetWindowRect)
User32.Rect frame = Dwmapi.GetRectAttr(windowHandle, Dwmapi.DwmWindowAttribute.EXTENDED_FRAME_BOUNDS);

catch (InvalidOperationException)
{
return GetWindowAreaFallback(windowHandle);
}

// Trim the black bar at the top when the window is maximized,
// as well as the title bar for applications with a defined client area
int yOffset = frame.Height - clientRect.Height;

return new Rectangle(frame.left + 1, frame.top + yOffset, clientRect.Width, clientRect.Height);
}

private static Rectangle GetWindowAreaFallback(IntPtr windowHandle)
{
// GetWindowRect is not always accurate when the window is maximized, but doesn't fail on Windows 7
bool success = User32.GetWindowRect(windowHandle, out User32.Rect windowRect);
CheckSuccess(success);
return new Rectangle(windowRect.left, windowRect.top, windowRect.Width, windowRect.Height);
}

private static void CheckSuccess(bool success)
{
if (!success)
{
throw new InvalidOperationException("Window was closed");
}
}
}
}

0 comments on commit 59cb19a

Please sign in to comment.