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

Question: WinUI3 - Windows.Current is null, then how to set mouse pointer position? #7947

Closed
super-brian opened this issue Nov 21, 2022 · 7 comments
Labels
needs-author-feedback Asked author to supply more information. no-recent-activity question

Comments

@super-brian
Copy link

Describe the bug

Maybe similar to this question: #2609
But here, it is focused on moving mouse pointer to a position.

For UWP, I did like this:
Window.Current.CoreWindow.PointerPosition = new Point(x, (y + 25.0) / scaleMultiplier);
But VS complains that Window.Current is null.

From this issue, #4152
I see that ProtectedCursor is there, but it is for the shape of mouse pointer.
Then, how can I move mouse pointer?

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Create new Blank App, Packaged (WinUI in Desktop) app
  2. Open MainWindow.xaml.cs and try get Window.Current

Expected behavior

Window.Current in Blank App, Packaged (WinUI in UWP) returns a window instance

Screenshots

No response

NuGet package version

No response

Windows version

No response

Additional context

No response

@super-brian super-brian added the bug Something isn't working label Nov 21, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Nov 21, 2022
@DarranRowe
Copy link

CoreWindow is documented to be outright unsupported in desktop applications. So yes, this is expected.

As for moving the mouse pointer, don't forget, you are now in desktop application land. This means that SetCursorPos is available. The .NET framework also has classes like Cursor, or you could just platform invoke SetCursorPos.

@super-brian
Copy link
Author

@DarranRowe Could you provide me with some sample code to do 'platform invoke' for WinUI (.Net6)?

@DarranRowe
Copy link

It is no different from using platform invoke in any other situation.

You first set up the class to reference the native function:

namespace Win32
{
    public sealed class Win32Invoke
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetCursorPos(int x, int y);
    }
}

and just call it:
Win32.Win32Invoke.SetCursorPos(0, 0);

There is nothing special about the namespace or class name used here. You can also use using to get rid of the namespace name as you normally can with C#. The function name needs to match the exported if you don't provide the entry point in the DllImport attribute though.

@bpulliam bpulliam added needs-author-feedback Asked author to supply more information. question and removed bug Something isn't working needs-triage Issue needs to be triaged by the area owners labels Mar 14, 2023
@microsoft-github-policy-service
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 7 days of this comment.

3 similar comments
@microsoft-github-policy-service
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 7 days of this comment.

@microsoft-github-policy-service
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 7 days of this comment.

@microsoft-github-policy-service
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 7 days of this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-author-feedback Asked author to supply more information. no-recent-activity question
Projects
None yet
Development

No branches or pull requests

3 participants