Skip to content
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

DisplayInformation throws exception #1609

Open
selastingeorge opened this issue Oct 8, 2021 · 5 comments
Open

DisplayInformation throws exception #1609

selastingeorge opened this issue Oct 8, 2021 · 5 comments
Assignees
Labels
area-interop Interoperation support for Windows APIs that need context, like an HWND theme-UwpParity

Comments

@selastingeorge
Copy link

I am working on a .net5 project which uses winrt, I was trying to get the scalefactor from the DisplayInformation Class but calling DisplayInformation.GetForCurrentView() throws exception, is it because core window, core application view is not supported in WPF, it throws an COM exception stating "element not found". Is there any way to overcome this, or should i need to continue with win32 methods itself.

@jonwis
Copy link
Member

jonwis commented Oct 12, 2021

@AdamBraden - this should probably go into the Interop bucket.

@Extrimis - which specific property on DisplayInformation were you looking for? Maybe @zhuman can advise on other means to get the Scale Factor from a .NET5 WPF project?

@selastingeorge
Copy link
Author

I Was looking for the ResolutionScale Property from DisplayInformation Class, I Was trying to resize the application window automatically to the screen resolution so in devices which uses 125% text scaling the value of SystemParameters.VirtualScreenWidth and SystemParameters.VirtualScreenHeight seems to give wrong values, currently i have a got a solution from WPF, most of the parts of my application is built using WinRT so i am looking for an alternate code using WinRT. Here is the current code

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;
using System.Windows.Media;
using static FluentCompositor.Interop.NativeMethods;

namespace FluentCompositor.Utilities
{
    internal static class DpiHelper
    {
        private static readonly double dpi = 96.0;

        internal static uint GetWindowDpi(IntPtr hwnd)
        {
            return GetDpiForWindow(hwnd);
        }

        internal static double GetCompositionTargetDpi(CompositionTarget compositionTarget)
        {
            return dpi * compositionTarget.TransformFromDevice.M11;
        }

        internal static double GetScalefactor(HwndSource source)
        {
            return GetWindowDpi(source.Handle) / GetCompositionTargetDpi(source.CompositionTarget);
        }
    }
}

The GetScalefactor() function provides me a value that i can multiply with the SystemParameters.VirtualScreenWidth or SystemParameters.VirtualScreenHeight, which gives me the exact screen resolution

@angelazhangmsft angelazhangmsft transferred this issue from microsoft/CsWinRT Oct 18, 2021
@asklar
Copy link
Member

asklar commented Oct 18, 2021

Since there is no more CoreWindow, any GetForCurrent* methods won't work. I believe there is a note about this in the docs but maybe worth making it more visible (e.g. can we detour those methods to crash with an appropriate error message?)

@zhuman
Copy link

zhuman commented Jul 7, 2022

As of Insider builds from the last few months, we've added a new IDisplayInformationStaticsInterop interface that you can use in your app to create a DisplayInformation instance for your HWND. We recommend using its GetForWindow method with an HWND if possible, since that gets you events for when your window moves around automatically. If you have a scenario where you don't use an HWND and you manage monitor placement directly, there is also a GetForMonitor API on this interface that takes an HMONITOR.

@riverar
Copy link
Contributor

riverar commented Jul 7, 2022

@zhuman That's not particularly helpful for Windows App SDK devs that are targeting 17763+. Maybe the team could consider undocking that impl. and putting it into the Windows App SDK?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-interop Interoperation support for Windows APIs that need context, like an HWND theme-UwpParity
Projects
None yet
Development

No branches or pull requests

8 participants