首页 文章

用图像替换光标指针后,如何将图像置于鼠标指针位置而不是图像的左上角?

提问于
浏览
2

我有一个光标图像.cur,最大宽度和高度为250像素,我完全需要 .

我已经设法用这个cur图像替换鼠标指针图像而不是右键单击 .

问题在于,当我正在使用时,指针与图像的左上角相关联,所以当我超出例如画布的边界时,cur图像消失,然后我回到正常的指针图像 .

我希望这个cur图像以鼠标指针位置为中心,而不是在它的左上角 . 我怎样才能做到这一点?

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;
    }

1 回答

  • 0

    你有两个选择:

    • 在Visual Studio中,在图像编辑器中打开光标文件或资源,然后从工具栏中选择“热点工具” . 然后单击新热点并保存文件 .

    • 使用位图实际创建游标并自己指定热点 .

    以下代码来自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));
        }
      }
    }
    

相关问题