Skip to content

Fixed Incorrect Window.Y and Window.Height values when closing a maximized window #29253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Dhivya-SF4094
Copy link
Contributor

@Dhivya-SF4094 Dhivya-SF4094 commented Apr 29, 2025

Issue Detail:

On the Windows platform, if the application window is maximized, the main window X,Y values are in negative (-7, -7). For a non-maximized window, values are correct.

Root Cause

On Windows, when the application window is maximized, the values of Window.Y and Window.Height reported during Window_Destroying are offset by 8 pixels. This occurs because the default window bounds include the non-client area (e.g., title bar and borders), which is excluded by the OS when maximizing the window. As a result, the bounds do not accurately reflect the actual client area used by the application, leading to incorrect frame values being reported to the virtual view.

Description of Change

The issue is resolved by using the Desktop Window Manager (DWM) API to retrieve the extended frame bounds of the window, which represent the true screen-space rectangle of the client area, even when the window is maximized. This is done via DwmGetWindowAttribute with the DWMWA_EXTENDED_FRAME_BOUNDS attribute. The retrieved bounds are then normalized by the display density and used to update the virtual view frame, ensuring accurate X, Y, Width, and Height values.
Validated the behaviour in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Issues Fixed:

Fixes #29066

Screenshots

Before  After 
     

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Apr 29, 2025
Copy link
Contributor

Hey there @@Dhivya-SF4094! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Apr 29, 2025
int cbAttribute);

internal static Rect GetExtendedFrameBounds(this IntPtr hwnd)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Can check hwnd to avoid unnecessary calls with invalid handles.

if (hwnd == IntPtr.Zero)
{
    return Rect.Zero;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz Added a check to avoid unnecessary calls.


internal static Rect GetExtendedFrameBounds(this IntPtr hwnd)
{
if (DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, out PlatformMethods.RECT rect, Marshal.SizeOf<PlatformMethods.RECT>()) == 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we wrapped in a try-catch and return an empty Rect if there are any exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz Wrapped the logic in a try-catch block to return an empty Rect in case of exceptions.

var size = appWindow.Size;
var pos = appWindow.Position;
var hwnd = PlatformView.GetWindowHandle();
var bounds = hwnd.GetExtendedFrameBounds();
Copy link
Contributor

Choose a reason for hiding this comment

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

I would include a check to validate that the return Rect is empty, in that case we need a fallback, like:

var size = appWindow.Size;
var pos = appWindow.Position;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz Added fallback to use appWindow.Position and appWindow.Size when Rect is empty.

@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect Window.Y and Window.Height values when closing a maximized window
2 participants