/*------------------------------------------------------------------------- * 功能描述:CursorUtil * 作者:xulisong * 创建时间: 2019/4/17 9:14:22 * 版本号:v1.0 * -------------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media.Imaging; using FWindSoft.WindowsApi; namespace FWindSoft.Wpf.Utils { public class CursorUtil { public static Cursor CreateCursor(Stream stream) { //StreamResourceInfo sri = Application.GetResourceStream(new Uri(@"Resources/0036.cur", UriKind.Relative)); Cursor cursor = new Cursor(stream); return cursor; } /// /// 识别.ani和.cur文件 /// /// /// public static Cursor CreateCursor(string path) { Cursor cursor = new Cursor(path); return cursor; } #region 自定义方式创建 /// /// 根据 BitmapImage 创建鼠标图标 /// /// 鼠标图像 /// 焦点在图片中的 X轴 坐标(相对于左上角) /// 焦点在图片中的 Y轴 坐标(相对于左上角) /// 错误则返回null public static Cursor CreateCursor(BitmapSource bs, uint xHotSpot = 0, uint yHotSpot = 0) { Cursor ret = null; Bitmap bm = BitmapSourceToBitmap(bs); if (bm != null) { try { ret = InternalCreateCursor(bm, xHotSpot, yHotSpot); } catch (Exception) { ret = null; } } return ret; } /// /// 根据 Bitmap 创建自定义鼠标 /// /// 鼠标图像 /// 焦点在图片中的 X轴 坐标(相对于左上角) /// 焦点在图片中的 Y轴 坐标(相对于左上角) /// 错误则返回null public static Cursor CreateCursor(Bitmap bm, uint xHotSpot = 0, uint yHotSpot = 0) { Cursor ret = null; if (bm == null) { return ret; } try { ret = InternalCreateCursor(bm, xHotSpot, yHotSpot); } catch (Exception) { ret = null; } return ret; } /// /// 根据 本地文件路径 创建鼠标图标 /// /// 鼠标图像全路径 /// 焦点在图片中的 X轴 坐标(相对于左上角) /// 焦点在图片中的 Y轴 坐标(相对于左上角) /// 错误则返回null public static Cursor CreateCursor(String filePath, uint xHotSpot = 0, uint yHotSpot = 0) { Cursor ret = null; if (string.IsNullOrWhiteSpace(filePath) || Directory.Exists(filePath) || !File.Exists(filePath)) { return ret; } //首先尝试通过默认方法创建 if (filePath.EndsWith(".ani") || filePath.EndsWith(".cur")) { try { ret = new Cursor(filePath); } catch (Exception) { ret = null; } } if (ret == null) { Bitmap bmp = null; try { bmp = Bitmap.FromFile(filePath) as Bitmap; if (bmp != null) { ret = CreateCursor(bmp, xHotSpot, yHotSpot); } } catch (Exception) { ret = null; } } return ret; } /// /// BitmapSource 转 Bitmap /// /// /// 错误则返回null public static Bitmap BitmapSourceToBitmap(BitmapSource bi) { Bitmap ret = null; if (bi == null) { return ret; } using (MemoryStream stream = new MemoryStream()) { try { BmpBitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(bi)); enc.Save(stream); ret = new Bitmap(stream); } catch (Exception) { ret = null; } } return ret; } /// /// 创建鼠标(本方法不允许public,避免内存泄漏) /// /// /// /// /// protected static Cursor InternalCreateCursor(Bitmap bitmap, uint xHotSpot, uint yHotSpot) { var iconInfo = new IconInfo(); WinAPI.GetIconInfo(bitmap.GetHicon(), ref iconInfo); iconInfo.xHotspot = xHotSpot;//焦点x轴坐标 iconInfo.yHotspot = yHotSpot;//焦点y轴坐标 iconInfo.fIcon = false;//设置鼠标 TSafeHandle cursorHandle = WinAPI.CreateIconIndirect(ref iconInfo); cursorHandle.Release = (h) => WinAPI.DestroyIcon(h); return CursorInteropHelper.Create(cursorHandle); } #endregion } }