Home > Net >  UWP: how to retrieve the size and scale factor of a second window in a separate monitor
UWP: how to retrieve the size and scale factor of a second window in a separate monitor

Time:11-03

I have a UWP app that opens a secondary window using AppWindow.TryCreateAsync.

I need to detect the actual size (and possible the scale factor) of the newly created window, as well as a few other details of the monitor that hosts this second window.

Usually, I use DisplayInformation.GetForCurrentView(), but this doesn't work in the following scenario:

  • Main App Window on the first monitor (e.g. resolution 1024 x 768, scale factor 150%)
  • Secondary Window on the second monitor (e.g. resolution 1920 x 1080, scale factor 250%)

In this case, DisplayInformation.GetForCurrentView() returns the value of the first monitor, where the Main App Window is hosted.

How can I retrieve the info for the monitor that hosts the Secondary Window?

Specifically, the actual size of the secondary window is the most important information, the others are accessory...

Thank you very much!

CodePudding user response:

I don't know how you can get the Scale Factor for an AppWindow placed on a generic monitor. However, if you create the AppWindow wrapping its content into a Frame, then you can use Frame.SizeChanged event to get the correct size of the secondary window:

UIElement myContent; // Create your content here

AppWindow myAppWindow = await AppWindow.TryCreateAsync();
Frame myContentFrame = new Frame();
myContentFrame.Content = myContent;

myContentFrame.SizeChanged  = (object sender, SizeChangedEventArgs e)
        {
            // Here you can get the new size of the Frame
            Size newFrameSize = Encoders.BuildLTSize(e.NewSize.Width, e.NewSize.Height);
            // Update the UI as needed
            UpdateView(newFrameSize);
        };
  • Related