How to get screen bounds for WinUI 3 Desktop application?

Viewed 1029

I need to achieve below requirements in WinUI 3 Desktop applications.

  1. How to get screen bounds?
  2. How to change windows cursor type in runtime?

I already did this in WinUI UWP applications.

For Screen bounds,

var visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
Size screenSize = new Size((visibleBounds.Width * scaleFactor), (visibleBounds.Height * scaleFactor));

For Cursor:

Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.SizeNorthwestSoutheast, 0);

Anyone please suggest how to achieve same requirement in WinUI Desktop applications?

1 Answers

I've added the following to App.xaml.cs:

private static MainWindow m_window;

public static MainWindow MainWindow { get { return m_window; } }

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs e)
{  
   m_window = new MainWindow();        
      
   m_window.Activate();
}      

And when I need bounds anywhere, I can use MainWindow.Bounds.

Regarding cursor, you need ProtectedCursor

this.ProtectedCursor = InputSystemCursor.Create(InputSystemCursorShape.Arrow);

Only thing it is protected, so you need to use it from right class.

Related