2

I have a cursor image .cur with maximum Width and Height of 250 pixels which i fully need.

I already managed to the replace the mouse pointer image with this cur image instead when holding right click.

The problem is that when using I'm doing that the pointer is associated with the top left corner of the image so when I go beyond for example the bounds of the canvas the cur image disappear and I go back to the normal pointer image.

I want this cur image to be centered on the mouse pointer location, not on it's top left corner. How can I do that?

private void canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        Cursor cPro = new Cursor(@"C:\Users\Faris\Desktop\C# Testing Projects\cPro.cur");

        globalValues.cursorSave = canvas.Cursor;

        canvas.Cursor = cPro;
    }


private void canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
    {
        canvas.Cursor = globalValues.cursorSave;
    }
Process
  • 105
  • 2
  • 9
  • http://msdn.microsoft.com/en-us/library/0b1674x8.aspx – Hans Passant Aug 22 '14 at 17:41
  • possible duplicate of [Change Cursor HotSpot in WinForms / .NET](http://stackoverflow.com/questions/550918/change-cursor-hotspot-in-winforms-net) – Matin Lotfaliee Aug 22 '14 at 18:04
  • Ok, I did it in the image editor program itself that created the cur and it worked. Hotspot was the keyword I wasn't familiar with. Thanks Hans and Matin. But Matin the code below is for WinForms not WPF. That same website has another for WPF but just fixing it in the image editor is much easier. Can one upvote a comment? I don't see any arrows. – Process Aug 22 '14 at 18:37
  • @Process It was just an example. I am sure you can handle it in WPF. – Matin Lotfaliee Aug 22 '14 at 18:44
  • I appreciate the help. For completeness here is the other link as well if someone looks for this. [link] (http://tech.pro/tutorial/751/wpf-tutorial-how-to-use-custom-cursors). – Process Aug 22 '14 at 18:51

1 Answers1

0

You have two options:

  1. In Visual Studio, open the cursor file or resource in the image editor and select the Hotspot Tool from the toolbar. Then click on the new hotspot and save the file.

  2. Create a cursor pragmatically using a bitmap and specify the hot spot yourself.

The code below is from here:

namespace CursorTest
{
  public struct IconInfo
  {
    public bool fIcon;
    public int xHotspot;
    public int yHotspot;
    public IntPtr hbmMask;
    public IntPtr hbmColor;
  }

  public class CursorTest : Form
  {
    public CursorTest()
    {
      this.Text = "Cursor Test";

      Bitmap bitmap = new Bitmap(140, 25);
      Graphics g = Graphics.FromImage(bitmap);
      using (Font f = new Font(FontFamily.GenericSansSerif, 10))
        g.DrawString("{ } Switch On The Code", f, Brushes.Green, 0, 0);

      this.Cursor = CreateCursor(bitmap, 3, 3);

      bitmap.Dispose();
    }

    [DllImport("user32.dll")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

    public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
    {
      IconInfo tmp = new IconInfo();
      GetIconInfo(bmp.GetHicon(), ref tmp);
      tmp.xHotspot = xHotSpot;
      tmp.yHotspot = yHotSpot;
      tmp.fIcon = false;
      return new Cursor(CreateIconIndirect(ref tmp));
    }
  }
}
Matin Lotfaliee
  • 1,745
  • 2
  • 21
  • 43