10

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?

Kanniyappan P
  • 281
  • 1
  • 6

2 Answers2

1

To get information about displays connected using WinUI 3, the simplest I can think of is to use
Nugget package: WindowsDisplayApi
Info here

It's a older package, however it works really well.

Once installed, you can do for example:

using System.Text.RegularExpressions;
using CommunityToolkit.Mvvm.ComponentModel;
using WindowsDisplayAPI;

namespace EasyDisplay.ViewModels;

public partial class DemoViewModel : ObservableObject
{

    [ObservableProperty]
    private string displayName = string.Empty;

    [ObservableProperty]
    private string friendly = string.Empty;

    [ObservableProperty]
    private string resolution = string.Empty;

    [ObservableProperty]
    private string position = string.Empty;

    public DemoViewModel()
    {
        foreach (Display display in Display.GetDisplays())
        {
            DisplayName = display.DisplayName;
            Friendly = GetFriendly(display.DisplayName);
            Resolution = GetResolution(display);
            Position = GetPosition(display);
        }
    }

    private static string GetFriendly(string value)
    {
        return Regex.Replace(value, @"[^A-Za+Z0-9 ]", "");
    }

    private static string GetResolution(Display display)
    {
        return display.CurrentSetting.Resolution.Width.ToString() + " x " + display.CurrentSetting.Resolution.Height.ToString();
    }

    private static string GetPosition(Display display)
    {
        return display.CurrentSetting.Position.X.ToString() + " x " + display.CurrentSetting.Position.Y.ToString();
    }
}

and more as per below:

enter image description here



A second option using the Win32 APIs is available here, and may give you more functionalities.

enter image description here

Franck E
  • 629
  • 8
  • 26
0

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.

demonplus
  • 5,613
  • 12
  • 49
  • 68
  • Regarding `Cursor`: You need to subclass the UI Element for this to work [source](https://github.com/microsoft/WindowsAppSDK/discussions/1816#discussioncomment-1661354). Or you use reflection... but that's not a great way to do things. – JHBonarius Apr 18 '23 at 07:17