2012-01-29 37 views
3

瀏覽後,我有一個代碼來通過使用C#給出座標來以編程方式控制鼠標的移動。我想做同樣的事情,但鼠標指針應該替換爲點圖像。我不能得到它......所以請讓我知道是否有在C#中的任何方式......如何使用圖像作爲鼠標指針而不是使用C#的默認箭頭?

先謝謝:)

+3

您需要指定這是Windows窗體還是WPF或Web應用程序。 – kprobst 2012-01-29 19:41:15

回答

2

這裏是一個great tutorial,告訴您如何做到這一點。

UPDATE

漢斯·帕桑特提醒我注意,有一個更好的辦法。這裏是他的版本,從this Stack Overflow Post採取:

using System; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Reflection; 

static class NativeMethods { 
    public static Cursor LoadCustomCursor(string path) { 
     IntPtr hCurs = LoadCursorFromFile(path); 
     if (hCurs == IntPtr.Zero) throw new Win32Exception(); 
     var curs = new Cursor(hCurs); 
     // Note: force the cursor to own the handle so it gets released properly 
     var fi = typeof(Cursor).GetField("ownHandle", BindingFlags.NonPublic | BindingFlags.Instance); 
     fi.SetValue(curs, true); 
     return curs; 
    } 
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
    private static extern IntPtr LoadCursorFromFile(string path); 
} 

這裏是一個使用示例:

this.Cursor = NativeMethods.LoadCustomCursor(@"c:\windows\cursors\aero_busy.ani"); 

祝你好運!

+0

該代碼有一個令人討厭的句柄泄漏。檢查此解決方法的解決方法:http://stackoverflow.com/a/4306984/17034 – 2012-01-29 20:00:17

+0

@ HansPassant - 你是對的。我應該回答這個問題嗎?我不想引用你的*更多*正確的答案。這個信用屬於你。謝謝。 – 2012-01-29 20:14:34

+0

好吧,寫一個真正的答案,不只是一組鏈接。 – 2012-01-29 20:17:27

相關問題