如何使用Zedgraph库在C#应用程序中更改鼠标光标?

时间:2012-04-25 10:20:22

标签: c# zedgraph

我可以按照here解释,在vanilla C#app中成功更改鼠标光标。我正在使用一个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;
}

2 个答案:

答案 0 :(得分:1)

ZedGraph控件具有Cursor属性。将它设置为你想要的任何东西。

答案 1 :(得分: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;
}