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:
How can I get the below shape instead?
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
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.
To start with open the Add New Item
dialog.
Then scroll down and select Cursor File
, give it a name of your choice.
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).
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).
Setting the hot spot at (9, 8):
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.
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!
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;
}
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!
Here are the cursors that I used:
http://www.mydoomsite.com/sourcecodes/HandOpen.cur
http://www.mydoomsite.com/sourcecodes/HandGrabbing.cur
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With