WPF与Winform自定义鼠标的方法(自选图片与大小)
Winform:
[DllImport("user32.dll")] public static extern IntPtr LoadImage(IntPtr hinst,string lpszName,uint uType ,int cxDesired,int cyDesired,uint fuLoad); private void OnLoad(object sender, EventArgs eventArgs) { Cursor myCursor = new Cursor(Cursor.Current.Handle); IntPtr p = LoadImage(IntPtr.Zero, "thinking.ani", 2, 1256, 1256, 0x00000010); myCursor.GetType().InvokeMember("handle", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField, null, myCursor, new object[] { p }); this.Cursor = myCursor; }
WPF 核心代码 将一个UIElement转化成Cursor
1 public Cursor ConvertToCursor(UIElement control, Point hotSpot) 2 { 3 // convert FrameworkElement to PNG stream 4 var pngStream = new MemoryStream(); 5 control.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 6 Rect rect = new Rect(0, 0, control.DesiredSize.Width, control.DesiredSize.Height); 7 RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.DesiredSize.Width, (int)control.DesiredSize.Height, 96, 96, PixelFormats.Pbgra32); 8 9 control.Arrange(rect); 10 rtb.Render(control); 11 12 PngBitmapEncoder png = new PngBitmapEncoder(); 13 png.Frames.Add(BitmapFrame.Create(rtb)); 14 png.Save(pngStream); 15 16 // write cursor header info 17 var cursorStream = new MemoryStream(); 18 cursorStream.Write(new byte[2] { 0x00, 0x00 }, 0, 2); // ICONDIR: Reserved. Must always be 0. 19 cursorStream.Write(new byte[2] { 0x02, 0x00 }, 0, 2); // ICONDIR: Specifies image type: 1 for icon (.ICO) image, 2 for cursor (.CUR) image. Other values are invalid 20 cursorStream.Write(new byte[2] { 0x01, 0x00 }, 0, 2); // ICONDIR: Specifies number of images in the file. 21 cursorStream.Write(new byte[1] { (byte)control.DesiredSize.Width }, 0, 1); // ICONDIRENTRY: Specifies image width in pixels. Can be any number between 0 and 255. Value 0 means image width is 256 pixels. 22 cursorStream.Write(new byte[1] { (byte)control.DesiredSize.Height }, 0, 1); // ICONDIRENTRY: Specifies image height in pixels. Can be any number between 0 and 255. Value 0 means image height is 256 pixels. 23 cursorStream.Write(new byte[1] { 0x00 }, 0, 1); // ICONDIRENTRY: Specifies number of colors in the color palette. Should be 0 if the image does not use a color palette. 24 cursorStream.Write(new byte[1] { 0x00 }, 0, 1); // ICONDIRENTRY: Reserved. Should be 0. 25 cursorStream.Write(new byte[2] { (byte)hotSpot.X, 0x00 }, 0, 2); // ICONDIRENTRY: Specifies the horizontal coordinates of the hotspot in number of pixels from the left. 26 cursorStream.Write(new byte[2] { (byte)hotSpot.Y, 0x00 }, 0, 2); // ICONDIRENTRY: Specifies the vertical coordinates of the hotspot in number of pixels from the top. 27 cursorStream.Write(new byte[4] { // ICONDIRENTRY: Specifies the size of the image's data in bytes 28 (byte)((pngStream.Length & 0x000000FF)), 29 (byte)((pngStream.Length & 0x0000FF00) >> 0x08), 30 (byte)((pngStream.Length & 0x00FF0000) >> 0x10), 31 (byte)((pngStream.Length & 0xFF000000) >> 0x18) 32 }, 0, 4); 33 cursorStream.Write(new byte[4] { // ICONDIRENTRY: Specifies the offset of BMP or PNG data from the beginning of the ICO/CUR file 34 (byte)0x16, 35 (byte)0x00, 36 (byte)0x00, 37 (byte)0x00, 38 }, 0, 4); 39 40 // copy PNG stream to cursor stream 41 pngStream.Seek(0, SeekOrigin.Begin); 42 pngStream.CopyTo(cursorStream); 43 44 // return cursor stream 45 cursorStream.Seek(0, SeekOrigin.Begin); 46 return new Cursor(cursorStream); 47 }