question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get screen bounds and change cursors in WinUI3 Desktop applications?

See original GitHub issue

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

  1. How to get display screen bounds?

  2. How to change windows cursor type in desktop environment?

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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
azchohficommented, Feb 12, 2021

For the Cursor, there is a new API for this:

Microsoft.UI.Xaml.UIElement
{
    protected CoreCursor ProtectedCursor { get; set; }
}

You can set this property and it will handle it all for you. The only problem is that you need to have a subclass of UIElement to access it.

For DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel, you need to get the XamlRoot.RasterizationScale.

0reactions
bridgesquaredcommented, Mar 2, 2021

Hi @KanniyappanP

I had the same experience of using ProtectedCursor and finding that the mouse pointer didn’t change, really wound me up for a while but I needed to give the user the visual feedback of being busy so perservered.

Firstly I can confirm that ProtectedCursor does work, so stick with it.

The issue I found was as part of the action setting the cursor, I was also IsEnabled = false the containing UserControl as a quick way of showing the user that the TextBoxes were not available. By removing the IsEnabled = false it started to work.

I would recommend you go back to small steps, just set the cursor with no other side affects, convince yourself that it works and then build up the functionality you want and find out where the conflict occurs.

Another thought is that you might find that your action is fully synchronous and never yields the thread to change the pointer. You might try inserting a await Task.Delay(1000) to force a yield? Or do the work on another thread using Task.Run ...?

Hope that helps, best of luck …

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get screenbounds for WinUI3 Desktop applications?
I need to achieve below requirements in WinUI 3 Desktop applications. How to get screen bounds? How to change windows cursor type in...
Read more >
How to get screen bounds for WinUI 3 Desktop application?
How to change windows cursor type in runtime? I already did this in WinUI UWP applications. For Screen bounds, var visibleBounds = Windows.UI....
Read more >
Untitled
Wpf change cursor in xaml WebMay 20, 2013 · You can set the Cursor property ... How to get screen bounds and change...
Read more >
How to Highlight Mouse Pointer Windows 10 - YouTube
In this step-by-step tutorial video, learn how to highlight your mouse cursor to make it easier for your audience to follow your mouse ......
Read more >
Getting Display Information in Windows UI 3
How to obtain information on the active display in a Windows UI 3 app.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found