Skip to content

[Crop and Lock] Use GetWindowRect instead of DwmGetWindowAttribute for window bounds - #49736

Merged
Muyuan Li (MuyuanMS) merged 2 commits into
microsoft:mainfrom
langningchen:main
Sep 3, 2026
Merged

Muyuan Li (MuyuanMS) merged 2 commits into
microsoft:mainfrom
langningchen:main

Conversation

@langningchen

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

This PR fixes the coordinate misalignment and cropping inaccuracies in the Crop and Lock utility by aligning the coordinate calculation system with GDI boundaries.

The root cause was a mismatch between GDI and DWM coordinate spaces. PrintWindow draws the target window using standard GDI boundaries (which include invisible resize borders and drop shadows). However, the code was calculating windowRect using DwmGetWindowAttribute with DWMWA_EXTENDED_FRAME_BOUNDS (DWM visible boundaries). This discrepancy caused the calculated offsetX and offsetY offsets to be too small, resulting in the cropped image being shifted (capturing empty margins/shadows on one side and clipping the actual content on the other).

By switching to ::GetWindowRect, all coordinate and offset math is unified into the GDI coordinate space, ensuring pixel-perfect cropping alignment.

PR Checklist

  • Communication: I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected
  • Tests: Added/updated and all pass
  • Localization: All end-user-facing strings can be localized
  • Dev docs: Added/updated
  • New binaries: Added on the required places
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

  • Modified both ScreenshotCropAndLockWindow.cpp and ThumbnailCropAndLockWindow.cpp to use ::GetWindowRect instead of DwmGetWindowAttribute when querying target window bounds.
  • Updated comments in ThumbnailCropAndLockWindow.cpp to reflect that bounds are now reported by GetWindowRect.
  • Resolves the clipping issues particularly noticeable under high DPI scaling environments (e.g., 125%, 150%).

Validation Steps Performed

  • Manual verification: Built and ran the project on a scaled high-DPI monitor.
  • Verified that Crop and Lock now accurately crops the targeted window precisely matching the red selection overlay box.
  • Attached a comparison GIF below (left: Before, right: After).
    (Note: The thin black borders remaining on the left and right sides in the modified version (right) are not a cropping alignment artifact; they are the actual internal borders/padding belonging to the targeted application window itself).
output_extended

Copilot AI lite review requested due to automatic review settings August 7, 2026 02:28
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@langningchen

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Crop and Lock’s window-bound calculations to use GetWindowRect (GDI-style window bounds) instead of DwmGetWindowAttribute(DWMWA_EXTENDED_FRAME_BOUNDS), aligning the coordinate space used for cropping math with what PrintWindow/GDI capture produces and addressing misalignment under DPI scaling.

Changes:

  • Switched window bounds retrieval to GetWindowRect in the screenshot capture path.
  • Switched window bounds retrieval to GetWindowRect in the DWM thumbnail crop path and updated the related inline comment.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/modules/CropAndLock/CropAndLock/ThumbnailCropAndLockWindow.cpp Uses GetWindowRect for target window bounds when translating the overlay’s client-relative crop rect into window-space for DWM thumbnail source rects.
src/modules/CropAndLock/CropAndLock/ScreenshotCropAndLockWindow.cpp Uses GetWindowRect for target window bounds to compute correct client-to-window offsets before cropping the PrintWindow bitmap.

@XinXiaoMoDear

Copy link
Copy Markdown

č́æ˜Ŋčƒ―åŋŦį‚đ写å…Ĩæ­Ģåžį‰ˆå°ąåĨ―䚆😭

@MuyuanMS

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@MuyuanMS
Muyuan Li (MuyuanMS) enabled auto-merge (squash) September 3, 2026 02:12
@MuyuanMS
Muyuan Li (MuyuanMS) merged commit 267af55 into microsoft:main Sep 3, 2026
9 checks passed
@LegendaryBlair Boliang Zhang (LegendaryBlair) added the Product-CropAndLock Refers to the Crop and Lock PowerToys label Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-CropAndLock Refers to the Crop and Lock PowerToys

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crop And Lock isn’t accurate enough.įŠ—åĢčĢå‰Šå™Ļäļįēū准

5 participants