1. 程式人生 > >在.net桌面程序中自定義鼠標光標

在.net桌面程序中自定義鼠標光標

.com info mco 分享圖片 rap orm mask ali asp

原文:在.net桌面程序中自定義鼠標光標

有的時候,一個自定義的鼠標光標能給你的程序增色不少。本文這裏介紹一下如何在.net桌面程序中自定義鼠標光標。由於.net的桌面程序分為WinForm和WPF兩種,這裏分別介紹一下。

WinForm程序

對於WinForm程序,可以通過修改Control.Cursor屬性來實現光標的修改,如果我們有光標文件的話,可以直接通過如下代碼實現自定義光標:

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

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

創建一個自定義光標,首先需要定義需要一個光標結構 ICONINFO ,它的.net版本如下:

public struct IconInfo
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}

然後通過GetIconInfo and CreateIconIndirect兩個函數來合成光標。完整代碼如下:

技術分享圖片
 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屬性來自定義光標文件。

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

技術分享圖片
 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     }
View Code

需要註意的是,由於使用的System.Drawing.BitMap,是需要引用System.Drawing.dll的

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

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

在.net桌面程序中自定義鼠標光標