如何使用 Zedgraph 库更改 C# 应用程序中的鼠标光标
本文关键字:应用程序 鼠标 光标 何使用 Zedgraph | 更新日期: 2023-09-27 18:32:07
我可以成功地在原版 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;
}
控件具有 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;
}