简体   繁体   中英

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

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 (eg resolution 1024 x 768, scale factor 150%)
  • Secondary Window on the second monitor (eg 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!

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);
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM