Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Customize and add Cursor Files to a project?

Quick question:

I am on a Wpf application and I use this routine to get my cursor shape changed on mouse hoover over my Image:

private void mainGrid_MouseEnter(object sender, MouseEventArgs e)
{
    mainImage.Cursor = Cursors.Hand;
}

private void mainGrid_MouseLeave(object sender, MouseEventArgs e)
{
    mainImage.Cursor = Cursors.Arrow;
}

Output:

enter image description here

How can I get the below shape instead?

enter image description here

I wish that this could simply work:

foreach (var finger in fingers.Skip(2)) { finger.Extend(); }

But no.. Programming does not work like that..

My desired shape is not available in cursors: Cursors Class

like image 682
Khalil Khalaf Avatar asked Jul 14 '16 15:07

Khalil Khalaf


1 Answers

In order to get a cursor like that you would need to get (or create) a custom one. There's no such cursor built into Windows. Creating your own cursor can be done using Visual Studio's built-in Image Editor for Icons.

Creating your own cursor

  1. To start with open the Add New Item dialog.

    Add New Item


  1. Then scroll down and select Cursor File, give it a name of your choice.

    Select "Cursor File" in the dialog.


  1. It'll now open your new cursor file in the so called Image Editor for Icons. Now you can just start drawing using the pencil or any of the other different drawing tools (images can also be pasted, but as you see the cursor initially has the 1 bit color format - meaning there's only two colors: black and white. This can be changed by adding a new image type).

    Are you reading these descriptions? :)


  1. Now we must specify a hot spot for the cursor. The hot spot is the position in the icon which Windows uses to track where the pointer is actually located (for simplicity you could call it the cursor's click point). The normal Windows 7 Aero Arrow has its hot spot at (0, 0) - the top left corner.

    To specify the cursor's hots pot we must use the Set Hot Spot Tool. Then you click at the specific pixel that you want to use as the hot spot in your cursor icon. For this cursor I chose (9, 8).

    Set Hot Spot Tool

    Setting the hot spot at (9, 8):

    Setting the actual hot spot


  1. Save everything, then go to the Solution Explorer, right-click your project and press Properties. Then go to the Resources tab and click Add Resource and Add Existing File....

    Now locate your project's folder and select and open your cursor file.

    Add a new resource

    Select your cursor file


  1. The final non-code step is to select your cursor in the Solution Explorer, go to the Properties pane and set Build Action to None. This is to prevent it from being added two times to your compiled executable, since it's already added as a resource.

    IMPORTANT: Do NOT do this for the cursor file located in the Resources folder!

    Set Build Action to None for your cursor.


Using your custom cursor in your application

Now onto the code, which is rather simple actually. As your cursor is now added as a byte array resource you only have to load that into a MemoryStream, then pass that memory stream into the constructor of a Cursor class. For simplicity and readability I put this code in another class.

public static class CursorHelper
{
    public static Cursor FromByteArray(byte[] array)
    {
        using (MemoryStream memoryStream = new MemoryStream(array))
        {
            return new Cursor(memoryStream);
        }
    }
}

Now you can just go ahead and declare your cursor at class-level in your form/control/etc. and you'll be ready to use it!

private Cursor OpenHand = CursorHelper.FromByteArray(Properties.Resources.CursorOpenHand);

public MainWindow()
{
    InitializeComponent();
    this.Cursor = OpenHand;
}

Example usage

I made an example per your specifications; a normal open hand that changes to a grabbing hand when you hold down your mouse:

private Cursor OpenHand = CursorHelper.FromByteArray(Properties.Resources.CursorOpenHand);
private Cursor GrabbingHand = CursorHelper.FromByteArray(Properties.Resources.CursorGrabbingHand);

public MainWindow()
{
    InitializeComponent();
    this.Cursor = OpenHand;
    this.MouseDown += this.MainWindow_MouseDown;
    this.MouseUp += this.MainWindow_MouseUp;
}

private void MainWindow_MouseDown(object sender, MouseEventArgs e)
{
    ((Control)sender).Cursor = GrabbingHand;
}

private void MainWindow_MouseUp(object sender, MouseEventArgs e)
{
    ((Control)sender).Cursor = OpenHand;
}

Profit!

Animation of the custom cursor


Here are the cursors that I used:

  • http://www.mydoomsite.com/sourcecodes/HandOpen.cur

  • http://www.mydoomsite.com/sourcecodes/HandGrabbing.cur

like image 92
Visual Vincent Avatar answered Oct 19 '22 23:10

Visual Vincent