2012-04-25 98 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

相關問題