Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KanniyappanP opened this issue Feb 11, 2021 · 9 comments
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues question team-Reach Issue for the Reach team

Comments

@KanniyappanP
Copy link

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?

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Feb 11, 2021
@StephenLPeters StephenLPeters added appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues labels Feb 12, 2021
@StephenLPeters
Copy link
Contributor

@anawishnoff do we have documentation for this somewhere? @JesseCol Do you know how to do this?

@StephenLPeters StephenLPeters added team-Reach Issue for the Reach team and removed needs-triage Issue needs to be triaged by the area owners labels Feb 12, 2021
@mdtauk
Copy link
Contributor

mdtauk commented Feb 12, 2021

I think the new WinUI 3 Window API has guidance for those functions that call "GetForCurrent" and "CoreWindow"

@JesseCol
Copy link

@marb2000 , do you have a recommendation here? Thanks!

@azchohfi
Copy link
Contributor

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.

@KarthikRajaAKR
Copy link

Hi Team,
I've used Windows.UI.Core.CoreWindow.GetForCurrentThread().PointerCursor to get the pointer, but in WinUI3 Preview 4 CoreWindow has been disabled. Is there any alternative available to get the pointer?

@jschwizer99
Copy link

@azchohfi many thanks for the valuable hint! This replaces 'Windows.UI.Core.CoreWindow.GetForCurrentThread().PointerCursor' for me. But it would be very nice if ProtectedCursor was accessible as a property to get it working in XAML without the need of subclassing each element I want to change the cursor.

@marb2000
Copy link
Contributor

@jschwizer99 @KanniyappanP and @KarthikRajaAKR it seems that your question was resolved. I'm closing this issue. Feel free to reopen it in case we miss something related or open a new one.

Thanks!

@KanniyappanP
Copy link
Author

KanniyappanP commented Feb 25, 2021

Hi @marb2000

I've tried ProtectedCursor API from UIElement as you suggested instead of using from CoreWindow (CoreWindow.GetForCurrentThread().PointerCursor), but it doesn't change the cursor pointer.

I have used like below. but not working for me.

ProtectedCursor = new CoreCursor(CoreCursorType.SizeNorthwestSoutheast, 0);

Thanks.,

@bridgesquared
Copy link

bridgesquared commented 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 ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues question team-Reach Issue for the Reach team
Projects
None yet
Development

No branches or pull requests

9 participants