dotnet / dotnet/wpf

SystemParameters.WorkArea is broken on PerMonitorV2 environment.

Open
#10,459 4 comments 0 reactions 0 assignees View on GitHub
.NET Framework Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

When display setting is changed or display scale(dpi) is changed while the application is running, `SystemParameters.WorkArea` does not return proper value.

If you programatically change Top or Left property of Window that are relative to `WorkArea.Width`/`WorkArea.Height`, position of the Window would be absurd.
Width and Height are scaled without any problem. they will be just bigger and smaller to display ratio.

### Reproduction Steps

0. Build app with PerMonitorV2 enabled.
2. Run app with 100% or 150% display scale ratio.
3. Change display scale ratio 100% to 150% or 150% to 100%.
4. Change Window's Top or Left property programatically that are relative to WorkArea's value. (i.e. right bottom corner or work area)
5. voilà!

### Expected behavior

SystemParameters.WorkArea returns proper value.

### Actual behavior

awkwardly scaled RECT is returned.

### Regression?

I only tested on .net framework environment that supports PerMonitorV2.

### Known Workarounds

1. Use same API that SystemParameters.WorkArea uses. `SystemParametersInfo`
2. `SystemParameters(SPI_GETWORKAREA, 0, ref RECT, 0)`
3. Get current display scale with `VisualTreeHelper.GetDpi()` or `DpiChanged` event.
4. adjust value of returned `RECT` from `SystemParameters()` with `DpiScale` instance.
5. `RECT.Left`/`RECT.Right` will be divided with `DpiScale.DpiScaleX` and `RECT.Top`/`RECT.Bottom` will be divided with `DpiScale.DpiScaleY`
6. divided values are correct WorkArea information.

### Impact

_No response_

### Configuration

- .NET framework (4.7.2, 4.8.1)
- Windows 10/11 (x64)
- AnyCPU builds

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.