我可以成功地在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;
}
我不工作。看起來很明顯但很奇怪。我在「鼠標向下」事件中更改了光標,但如果我完全移動鼠標,則光標會變回十字,即使我不在任何位置執行此操作。 – 2013-10-24 19:25:27