在.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 publicView Codeint 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 }
測試程式碼為:
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());