fredirty2017
fredirty2017

Reputation: 103

How to capture a maximized window accurately?

Upvotes: 0

Views: 444

Answers (1)

Strive Sun
Strive Sun

Reputation: 6299

whether use BitBlt or PrintWindow, the boundary of the result has a few pixels black.

GetWindowRect:

In Windows Vista and later, the Window Rect now includes the area occupied by the drop shadow.

Calling GetWindowRect will have different behavior depending on whether the window has ever been shown or not. If the window has not been shown before, GetWindowRect will not include the area of the drop shadow.

To get the window bounds excluding the drop shadow, use DwmGetWindowAttribute, specifying DWMWA_EXTENDED_FRAME_BOUNDS. Note that unlike the Window Rect, the DWM Extended Frame Bounds are not adjusted for DPI. Getting the extended frame bounds can only be done after the window has been shown at least once.

The test found that the relative positions obtained by GetWindowRect and DwmGetWindowAttribute(...DWMWA_EXTENDED_FRAME_BOUND), GetCroppedWindowRect are not correct.

The GetCroppedWindowRect function is actually a wrapper for GetWindowRect.

bool GetCroppedWindowRect(HWND window,
                          bool avoid_cropping_border,
                          DesktopRect* cropped_rect,
                          DesktopRect* original_rect) {
  DesktopRect window_rect;
  if (!GetWindowRect(window, &window_rect)) {
    return false;
  }
  if (original_rect) {
    *original_rect = window_rect;
  }
  *cropped_rect = window_rect;
  bool is_maximized = false;
  if (!IsWindowMaximized(window, &is_maximized)) {
    return false;
  }

DwmGetWindowAttribute() returns physical coordinates, and GetWindowRect() returns logical coordinates.

Windows Vista introduces the concept of physical coordinates. Desktop Window Manager (DWM) scales non-dots per inch (dpi) aware windows when the display is high dpi. The window seen on the screen corresponds to the physical coordinates. The application continues to work in logical space. Therefore, the application's view of the window is different from that which appears on the screen. For scaled windows, logical and physical coordinates are different.

Refer: PhysicalToLogicalPoint

Upvotes: 0

Related Questions