dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.03k stars 1.16k forks source link

Provide an equivalent for the WinForms Screen class #2012

Open terrajobst opened 4 years ago

terrajobst commented 4 years ago

WPF generally doesn't include Windows Forms (the developer has to reference WinForms explicitly). We do this (among other reasons) so that we don't pollute IntelliSense with two UI stacks.

One common reason why people end up having to reference WinForms is so that they can enumerate screens and put windows on specific screens. But even with WinForms, the APIs aren't exactly super usable from WPF, due to mismatches in primitive types. It would be good if WPF would provide an equivalent type.

WamWooWam commented 4 years ago

I think this class should also return values in WPF’s DPI independent units rather than a straight port of how these APIs work in WinForms, especially considering that the Left and Top properties on Window are also DPI independent.

dopsun commented 4 years ago

For my case, user may have more than 3 monitors, and they are stacked and make the visible workarea as Polygon, instead of Rectangle.

Lacking Screen equivalent, WPF apps may not be able to tell whether window is actually visible in any monitors.

As illustrated below, if Window is position between monitor 4 & 5, then there are no existing APIs in pure WPF way, to tell window is invisible.

+-----+     +-----+
|  4  |     |  5  |
+-----+-----+-----+
|  1  |  2  |  3  |
+-----+-----+-----+
rampaa commented 1 year ago

Any progress on this?

pchaurasia14 commented 1 year ago

None currently, but you can submit an API proposal and we can review it.

rampaa commented 1 year ago

Having a class like this that uses device-independent units whenever applicable would allow me to not reference WinForms needlessly.

I guess I could copy-paste the whole thing as an API proposal but I doubt that would be any more helpful.

miloush commented 1 year ago

Tangentially relevant discussion in #6841. @rampaa what is your use case? How do you expect mixed DPI scenarios to work? API proposal should be a bit more than list of members.

rampaa commented 1 year ago

what is your use case?

I need to get the bounds of the screen (in device-independent units) in which a specific window is located. To do that I currently need to do the following:

System.Windows.Forms.Screen ActiveScreen = System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(SpecificWindowInQuestion.Instance).Handle);
DpiScale DpiOfTheSpecificWindowInQuestion = VisualTreeHelper.GetDpi(SpecificWindowInQuestion.Instance);
double DpiAwareActiveScreenWidth = ActiveScreen.Bounds.Width / DpiOfTheSpecificWindowInQuestion.DpiScaleX;
double DpiAwareActiveScreenHeight = ActiveScreen.Bounds.Height / DpiOfTheSpecificWindowInQuestion.DpiScaleY

Which, obviously, is not ideal. I shouldn't need to reference WinForms for this simple use case and a WPF version of the Screen class should use the device-independent units from the get go.

How do you expect mixed DPI scenarios to work?

I am not sure what you have in mind but I would expect the WPF version of Screen class to just use/return device-independent units and I don't see the relevance of multiple monitors having different DPIs.