繁体   English   中英

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

[英]How to change the mouse cursor in a C# application using Zedgraph library?

我可以成功改变鼠标光标在香草C#应用程序作为解释在这里 我正在使用使用Zedgraph dll绘制图表的C#应用​​程序。 当鼠标指针位于图表上方时,它会变成交叉线。 我需要将光标更改为另一个图像。 但是,我无法使用较早的代码示例来执行此操作。 我怀疑这是因为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;
}

ZedGraph控件具有Cursor属性。 将其设置为任何您想要的。

终于解决了问题,如下图

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM