0

如此处所述,我可以成功地更改香草 C# 应用程序中的鼠标光标。我正在使用一个 C# 应用程序,它使用Zedgraph dll 来绘制图表。当鼠标指针位于图表顶部时,它会变成十字线。我需要将光标更改为另一个图像。但是,我无法使用较早的代码示例来执行此操作。我怀疑这是因为Zedgraph库已经重载了光标更改事件。zgObj 是下面给出的代码中的 Zedgraph 对象。有任何想法吗?

void ToggleCursor()
{
   Bitmap bitmap = new Bitmap(@"C:\Documents and Settings\Martin\My Documents\My Pictures\line.bmp");

   zgObj.Cursor = XCursor.CreateCursor(bitmap, 0, 0);

   bitmap.Dispose();
}

public class XCursor : Form
{
   [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)
   {
      IntPtr ptr = bmp.GetHicon();
      IconInfo tmp = new IconInfo();
      GetIconInfo(ptr, ref tmp);
      tmp.xHotspot = xHotSpot;
      tmp.yHotspot = yHotSpot;
      tmp.fIcon = false;
      ptr = CreateIconIndirect(ref tmp);
      return new Cursor(ptr);
   }
}

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

2 回答 2

1

ZedGraph 控件有一个 Cursor 属性。将其设置为您想要的任何内容。

于 2012-04-26T14:20:00.347 回答
0

终于解决了问题,如下图

Cursor lineCursor = null; //declared in the main application class
    bShowLineCursor = false;  

private void zgObj_CursorChanged(object sender, EventArgs e)
{
  if (bShowLineCursor)
  {
     bShowLineCursor = false;
     Bitmap bitmap = new Bitmap(@"C:\Documents and Settings\My Documents\My Pictures\line.bmp");
     lineCursor= XCursor.CreateCursor(bitmap, 0, 0);
     bitmap.Dispose();
  }

  if (lineCursor != null)
     zgObj.Cursor = lineCursor;

}

void ToggleCursor() 
{
   bShowLineCursor = true;
}

public class XCursor : Form
{
   [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)
   {
      IntPtr ptr = bmp.GetHicon();
      IconInfo tmp = new IconInfo();
      GetIconInfo(ptr, ref tmp);
      tmp.xHotspot = xHotSpot;
      tmp.yHotspot = yHotSpot;
      tmp.fIcon = false;
      ptr = CreateIconIndirect(ref tmp);
      return new Cursor(ptr);
   }
}

public struct IconInfo
{
   public bool fIcon;
   public int xHotspot;
   public int yHotspot;
   public IntPtr hbmMask;
   public IntPtr hbmColor;
}
于 2012-04-26T14:37:33.387 回答