天天看點

在.net桌面程式中自定義滑鼠光标

原文: 在.net桌面程式中自定義滑鼠光标

有的時候,一個自定義的滑鼠光标能給你的程式增色不少。本文這裡介紹一下如何在.net桌面程式中自定義滑鼠光标。由于.net的桌面程式分為WinForm和WPF兩種,這裡分别介紹一下。

WinForm程式

對于WinForm程式,可以通過修改Control.Cursor屬性來實作光标的修改,如果我們有光标檔案的話,可以直接通過如下代碼實作自定義光标:

    this.Cursor = new Cursor("myCursor.cur");

但這種方式不是本文介紹的重點,本文主要介紹如何自己繪制光标,這樣則具有更多的可控性和靈活性。

建立一個自定義光标,首先需要定義需要一個光标結構 

ICONINFO

 ,它的.net版本如下:

    public struct IconInfo

    {

        public

bool

fIcon;

int

xHotspot;

int

yHotspot;

IntPtr

hbmMask;

IntPtr

hbmColor;

    }

然後通過

GetIconInfo  and  CreateIconIndirect 兩個函數來合成光标。完整代碼如下: 
在.net桌面程式中自定義滑鼠光标
在.net桌面程式中自定義滑鼠光标

1     public class CursorHelper
 2     {
 3         static class NativeMethods
 4         {
 5             public struct IconInfo
 6             {
 7                 public bool fIcon;
 8                 public int xHotspot;
 9                 public int yHotspot;
10                 public IntPtr hbmMask;
11                 public IntPtr hbmColor;
12             }
13 
14             [DllImport("user32.dll")]
15             public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
16 
17 
18             [DllImport("user32.dll")]
19             [return: MarshalAs(UnmanagedType.Bool)]
20             public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
21         }
22 
23         public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
24         {
25             var icon = new NativeMethods.IconInfo
26             {
27                 xHotspot = xHotSpot,
28                 yHotspot = yHotSpot,
29                 fIcon = false
30             };
31 
32             NativeMethods.GetIconInfo(bmp.GetHicon(), ref icon);
33             return new Cursor(NativeMethods.CreateIconIndirect(ref icon));
34         }
35     }      

View Code

測試代碼為:

    using (Bitmap bitmap = new Bitmap(21, 26))

    using (Graphics g = Graphics.FromImage(bitmap))

        g.DrawRectangle(Pens.Red, 0, 0, 20, 25);

        this.Cursor = CursorHelper.CreateCursor(bitmap, 3, 3);

WPF程式

至于WPF程式,和WinForm程式是非常類似的,一方面,它也可以通過光标檔案來實作寫入Cursor屬性來自定義光标檔案。

至于自己繪制光标,上面的代碼基本上也是可以複用的,不過相對的要重新封裝一下,完整代碼如下: 

在.net桌面程式中自定義滑鼠光标
在.net桌面程式中自定義滑鼠光标
1     public class CursorHelper
 2     {
 3         static class NativeMethods
 4         {
 5             public struct IconInfo
 6             {
 7                 public bool fIcon;
 8                 public int xHotspot;
 9                 public int yHotspot;
10                 public IntPtr hbmMask;
11                 public IntPtr hbmColor;
12             }
13 
14             [DllImport("user32.dll")]
15             public static extern SafeIconHandle CreateIconIndirect(ref IconInfo icon);
16 
17             [DllImport("user32.dll")]
18             public static extern bool DestroyIcon(IntPtr hIcon);
19 
20             [DllImport("user32.dll")]
21             [return: MarshalAs(UnmanagedType.Bool)]
22             public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
23         }
24 
25         [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
26         class SafeIconHandle : SafeHandleZeroOrMinusOneIsInvalid
27         {
28             public SafeIconHandle()
29                 : base(true)
30             {
31             }
32 
33             protected override bool ReleaseHandle()
34             {
35                 return NativeMethods.DestroyIcon(handle);
36             }
37         }
38 
39         static Cursor InternalCreateCursor(System.Drawing.Bitmap bitmap, int xHotSpot, int yHotSpot)
40         {
41             var iconInfo = new NativeMethods.IconInfo
42             {
43                 xHotspot = xHotSpot,
44                 yHotspot = yHotSpot,
45                 fIcon = false
46             };
47 
48             NativeMethods.GetIconInfo(bitmap.GetHicon(), ref iconInfo);
49 
50             var cursorHandle = NativeMethods.CreateIconIndirect(ref iconInfo);
51             return CursorInteropHelper.Create(cursorHandle);
52         }
53 
54         public static Cursor CreateCursor(UIElement element, int xHotSpot = 0, int yHotSpot = 0)
55         {
56             element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
57             element.Arrange(new Rect(new Point(), element.DesiredSize));
58 
59             var renderTargetBitmap = new RenderTargetBitmap(
60                 (int)element.DesiredSize.Width, (int)element.DesiredSize.Height,
61                 96, 96, PixelFormats.Pbgra32);
62 
63             renderTargetBitmap.Render(element);
64 
65             var encoder = new PngBitmapEncoder();
66             encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
67 
68             using (var memoryStream = new MemoryStream())
69             {
70                 encoder.Save(memoryStream);
71                 using (var bitmap = new System.Drawing.Bitmap(memoryStream))
72                 {
73                     return InternalCreateCursor(bitmap, xHotSpot, yHotSpot);
74                 }
75             }
76         }
77     }      

需要注意的是,由于使用的System.Drawing.BitMap,是需要引用System.Drawing.dll的

封裝之後,是可以直接傳入UIElement作為自繪制的光标的,得益于WPF的強大繪圖功能,是可以非常容易的繪制漂亮的光标的。測試代碼如下:

    this.Cursor= CursorHelper.CreateCursor(new UserControl1());