WPF鼠标样式

时间:2025-01-25 08:35:50
  • 简介
  • 调用方式
  • 源码

简介

  上本身自带20多种鼠标样式,通过Cursors类进行获取,例如:

  项目里边识别.ani和.cur文件,可直接通过相对路径或绝对路径来获取文件  例如:

//相对路径
StreamResourceInfo sri = (new Uri(@"Resources/", ));
Cursor customCursor = new Cursor();
 = customCursor;
//绝对路径
Cursor custoCursor = new Cursor(((), ""));
 = custoCursor;

   3.自定义图片来做鼠标样式,也适用于.ani和cur文件 调用方式如下:

//Environment(.Net提供的类库,客户区系统一些特殊文件夹)
string path= ((), "")
 = (path, 500, 500);

源码如下(直接无脑复制粘贴,改一下命名空间即可):

using System;
using ;
using ;
using Microsoft.;
using ;
using ;
using ;
using ;
using ;

namespace WPFDemo
{    
    public class MouseManager
    {
        /// <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 = BitmapSource2Bitmap(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 ((filePath) || (filePath) || !(filePath))
            {
                return ret;
            }

            //首先尝试通过默认方法创建
            if ((".ani") || (".cur"))
            {
                try
                {
                    ret = new Cursor(filePath);
                }
                catch (Exception)
                {
                    ret = null;
                }
            }

            //如果文件不是正确的.ani或.cur文件,则尝试通过BitMap创建
            if (ret == null)
            {

                Bitmap bmp = null;
                try
                {
                    bmp = (filePath) as Bitmap;
                    if (bmp != null)
                    {
                        ret = CreateCursor(bmp, xHotSpot, yHotSpot);
                    }
                }
                catch (Exception)
                {
                    ret = null;
                }
            }

            return ret;
        }


        //BitmapSource 转 Bitmap 方法

        /// <summary>
        /// BitmapSource 转 Bitmap
        /// </summary>
        /// <param name="bi"></param>
        /// <returns>错误则返回null</returns>
        public static Bitmap BitmapSource2Bitmap(BitmapSource bi)
        {

            Bitmap ret = null;
            MemoryStream stream = null;

            if (bi == null)
            {
                return ret;
            }

            try
            {
                stream = new MemoryStream();
                BmpBitmapEncoder enc = new BmpBitmapEncoder();
                ((bi));
                (stream);

                ret = new Bitmap(stream);
            }
            catch (Exception)
            {
                ret = null;
            }
            finally
            {
                ();
            }

            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 ();
            ((), ref iconInfo);

             = xHotSpot;//焦点x轴坐标
             = yHotSpot;//焦点y轴坐标
             = false;//设置鼠标

            SafeIconHandle cursorHandle = (ref iconInfo);
            return (cursorHandle);
        }


        protected static class NativeMethods
        {
            public struct IconInfo
            {
                public bool fIcon;
                public uint xHotspot;
                public uint yHotspot;
                public IntPtr hbmMask;
                public IntPtr hbmColor;
            }

            [DllImport("")]
            public static extern SafeIconHandle CreateIconIndirect(ref IconInfo icon);

            [DllImport("")]
            public static extern bool DestroyIcon(IntPtr hIcon);

            [DllImport("")]
            [return: MarshalAs()]
            public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
        }

   
        [SecurityPermission(, UnmanagedCode = true)]
        protected class SafeIconHandle : SafeHandleZeroOrMinusOneIsInvalid
        {
            public SafeIconHandle()
                : base(true)
            {
            }

            /// <summary>
            /// 释放资源(系统清理缓存时调用该方法)
            /// </summary>
            /// <returns></returns>
            protected override bool ReleaseHandle()
            {
                return (handle);
            }
        }
    }
}