2012-04-25 106 views
0

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

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

回答

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

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

+0

我不工作。看起来很明显但很奇怪。我在“鼠标向下”事件中更改了光标,但如果我完全移动鼠标,则光标会变回十字,即使我不在任何位置执行此操作。 – 2013-10-24 19:25:27