123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /*-------------------------------------------------------------------------
- * 功能描述: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;
- }
- /// <summary>
- /// 识别.ani和.cur文件
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static Cursor CreateCursor(string path)
- {
- Cursor cursor = new Cursor(path);
- return cursor;
- }
-
- #region 自定义方式创建
- /// <summary>
- /// 根据 BitmapImage 创建鼠标图标
- /// </summary>
- /// <param name="bs">鼠标图像</param>
- /// <param name="xHotSpot">焦点在图片中的 X轴 坐标(相对于左上角)</param>
- /// <param name="yHotSpot">焦点在图片中的 Y轴 坐标(相对于左上角)</param>
- /// <returns>错误则返回null</returns>
- 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;
- }
- /// <summary>
- /// 根据 Bitmap 创建自定义鼠标
- /// </summary>
- /// <param name="bm">鼠标图像</param>
- /// <param name="xHotSpot">焦点在图片中的 X轴 坐标(相对于左上角)</param>
- /// <param name="yHotSpot">焦点在图片中的 Y轴 坐标(相对于左上角)</param>
- /// <returns>错误则返回null</returns>
- 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;
- }
- /// <summary>
- /// 根据 本地文件路径 创建鼠标图标
- /// </summary>
- /// <param name="filePath">鼠标图像全路径</param>
- /// <param name="xHotSpot">焦点在图片中的 X轴 坐标(相对于左上角)</param>
- /// <param name="yHotSpot">焦点在图片中的 Y轴 坐标(相对于左上角)</param>
- /// <returns>错误则返回null</returns>
- 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;
- }
- /// <summary>
- /// BitmapSource 转 Bitmap
- /// </summary>
- /// <param name="bi"></param>
- /// <returns>错误则返回null</returns>
- 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;
- }
- /// <summary>
- /// 创建鼠标(本方法不允许public,避免内存泄漏)
- /// </summary>
- /// <param name="bitmap"></param>
- /// <param name="xHotSpot"></param>
- /// <param name="yHotSpot"></param>
- /// <returns></returns>
- 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
- }
- }
|