• C# .Net中鼠标Cursor的公用辅助类


    有时,我们需要将背景透明的png或gif格式图片生成的Cursor,甚至将其旋转后生成旋转效果的Cursor(可指定热点)。

    直接上源码:

    using System;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;
    using System.Drawing;

    namespace CommonUtils.Common
    {
        ///


        /// Cursor的公用辅助类
        ///

        public class CursorUtil
        {
            ///
            /// 从资源文件中调用指定名称的Cursor图标
            ///

            /// Cursor图标的名称
            /// Cursor图标
            public static Cursor GetCursorByResourceName(string cursorName)
            {
                using (Stream resStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(cursorName))
                {
                    return new Cursor(resStream);
                }
            }

            ///


            ///用背景透明的png或gif格式图片生成的Cursor
            /// 用法:
            /// Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
            /// GetCursor(a, new Point(0, 0));
            ///

            /// 背景透明的png或gif格式图片
            /// 热点
            /// Cursor图标
            public static Cursor GetCursor(Bitmap cursorImg, Point hotPoint)
            {
                return GetRotatedCursor(cursorImg, hotPoint, 0);
            }

            ///


            /// 用背景透明的png或gif格式图片生成,并指定旋转角度的Cursor
            /// 用法:
            /// Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
            /// GetRotatedCursor(a, new Point(0, 0),30f);
            ///

            /// 背景透明的png或gif格式图片
            /// 热点 
            /// 指定Cursor图片的旋转角度
            /// Cursor图标
            public static Cursor GetRotatedCursor(Bitmap cursorImg, Point hotPoint, float rotationAngle)
            {
                int hotX = hotPoint.X;
                int hotY = hotPoint.Y;
                Bitmap cursorBmp = new Bitmap(cursorImg.Width * 2 - hotX, cursorImg.Height * 2 - hotY);
                Graphics g = Graphics.FromImage(cursorBmp);
                g.Clear(Color.FromArgb(0, 0, 0, 0));
                //旋转指定角度
                if(rotationAngle!=0) g.RotateTransform(rotationAngle);
                g.DrawImage(cursorImg, cursorImg.Width - hotX, cursorImg.Height - hotY, cursorImg.Width, cursorImg.Height);
                Cursor result = new Cursor(cursorBmp.GetHicon());
                g.Dispose();
                cursorBmp.Dispose();

                return result;
            }

            ///


            /// 用背景透明的png或gif格式图片生成,并指定旋转角度的Cursor
            /// 用法:
            /// Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
            /// GetRotatedCursor(a, new Point(0, 0),30f);
            ///

            /// 背景透明的png或gif格式图片
            /// 热点 
            /// 指定Cursor图片的旋转角度
            /// Cursor图标
            public Cursor GetRotatedCursor(byte[] curFileBytes, Point hotPoint, float rotationAngle)
            {
                var origStream = new MemoryStream(curFileBytes);
                var cursorImg = new System.Drawing.Icon(origStream).ToBitmap();
                return GetRotatedCursor(cursorImg,  hotPoint,  rotationAngle);
            }
        }
    }

  • 相关阅读:
    [要素察觉]C语言六讲:函数
    [React]为什么写React组件的时候,需要先引入React?
    【代码精读】进入optee时中断都是开启的还是关闭的?
    【QT】常用控件——按钮组
    DORIS 文件读取(研究中)
    LOOK!菊风可视化回溯解决方案,快速上线全面保障业务合规
    查词翻译类应用使用数据接口api总结
    一篇读懂 Linux 用户管理
    Linux中如何禁止普通用户使用su命令
    ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)
  • 原文地址:https://blog.csdn.net/johnsuna/article/details/126704245