Patrik Karlsson
Patrik Karlsson

Reputation: 23

How to create a custom cursor in C# UWP with CoreCursorType.Custom?

As far as I know "Cursor" don't exits in UWP.

I can change cursor with this code:

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

How do I create a custom cursor with CoreCursorType.Custom?

CoreCursorType.Custom

Upvotes: 2

Views: 1325

Answers (2)

NoWar
NoWar

Reputation: 37633

Here is the answer https://learn.microsoft.com/en-us/uwp/api/windows.ui.core.corecursor?view=winrt-20348

To use a custom cursor, use the CoreCursorType.Custom enumeration option, and specify the Id of the custom cursor. You can add a .res resource file that contains the custom cursor to your project and include it in your assembly with the /win32res compiler option. The Id is the Cursor Resource ID specified in the .res file.

Personally I have used this working manual https://social.msdn.microsoft.com/Forums/en-US/14001796-bcd5-4b9d-9d7e-13bc6ba59d2d/uwp-how-to-set-a-pointer-cursor-as-a-circle-instead-of-a-window-default-cursor?forum=wpdevelop

We need just proper .res file

For that we should do following

  1. Create any C++ project. For example Dll, Add new C++ Dll project name it for example as ResourcesComponent.

  2. Add a resource file to the project. [right-mouse] the project name "ResourcesComponent" => Add New Item => Visual C++ => Resource File (.rc)

  3. Name the resource file for example Resources.rc

  4. Add a cursor resource. Double click on Resources.rc the it will open in Resource View tab then Right click on "Resources.rc" => Add Resource => Cursor => New (Here you can modify cursor appearance by pen)

  5. Go back to solution explorer and now you will see resource.h file where you can find its id

#define IDC_CURSOR1 101

  1. Build dll in Release mode

  2. In Release sub folder you can find Resource.res file

Now we have proper .res file, We should add it to our UWP project root folder.

  1. Copy Resource.res file to UWP project root directory. (You can delete DLL project as it is no longer needed )

  2. Unload UWP project in VS.

  3. Rigth click on unloaded project and select edit .csproj

  4. in first or second PropertyGroup (where is Appname, TargetPlatform, etc ) add

  <PropertyGroup>
    .....
  <Win32Resource>Resource.res</Win32Resource>
  </ PropertyGroup>
  1. Reload project

  2. Use following code for set cursor in your image PointerEnter event handler

    Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Custom, 101);

If you need to create a colorized *.cur file use any free tools like I did here. And just replace the source *.cur with a new one. And if you need a small *.cur file just remember that by default its size is 32 x 32 pixels and you have draw your small image like I did.

Enjoy!

enter image description here

Upvotes: 0

Breeze Liu - MSFT
Breeze Liu - MSFT

Reputation: 3808

You should create your own cursor *.res resource file and contains the custom cursor to your project by add it to your package.manifest file. After that, you can use CoreCursorType.Custom enumeration option, and specify the Id of the custom cursor to use the custom cursor. There also have a thread in which @Azat Tazayan has introduced the detailed steps to implement it and you can refer to it:

https://social.msdn.microsoft.com/Forums/en-US/14001796-bcd5-4b9d-9d7e-13bc6ba59d2d/uwp-how-to-set-a-pointer-cursor-as-a-circle-instead-of-a-window-default-cursor?forum=wpdevelop

Upvotes: 1

Related Questions