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
{
    /// <summary>
    /// Cursor的公用辅助类
    /// </summary>
    public class CursorUtil
    {
        /// <summary>
        /// 从资源文件中调用指定名称的Cursor图标
        /// </summary>
        /// <param name="cursorName">Cursor图标的名称</param>
        /// <returns>Cursor图标</returns>
        public static Cursor GetCursorByResourceName(string cursorName)
        {
            using (Stream resStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(cursorName))
            {
                return new Cursor(resStream);
            }
        }

        ///<summary>
        ///用背景透明的png或gif格式图片生成的Cursor
        /// 用法:
        /// Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
        /// GetCursor(a, new Point(0, 0));
        /// </summary>
        /// <param name="cursorImg">背景透明的png或gif格式图片</param>
        /// <param name="hotPoint">热点</param>
        /// <returns>Cursor图标</returns>
        public static Cursor GetCursor(Bitmap cursorImg, Point hotPoint)
        {
            return GetRotatedCursor(cursorImg, hotPoint, 0);
        }

        /// <summary>
        /// 用背景透明的png或gif格式图片生成,并指定旋转角度的Cursor
        /// 用法:
        /// Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
        /// GetRotatedCursor(a, new Point(0, 0),30f);
        /// </summary>
        /// <param name="cursorImg">背景透明的png或gif格式图片</param>
        /// <param name="hotPoint">热点</param> 
        /// <param name="rotationAngle">指定Cursor图片的旋转角度</param>
        /// <returns>Cursor图标</returns>
        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;
        }

        /// <summary>
        /// 用背景透明的png或gif格式图片生成,并指定旋转角度的Cursor
        /// 用法:
        /// Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
        /// GetRotatedCursor(a, new Point(0, 0),30f);
        /// </summary>
        /// <param name="cursorImg">背景透明的png或gif格式图片</param>
        /// <param name="hotPoint">热点</param> 
        /// <param name="rotationAngle">指定Cursor图片的旋转角度</param>
        /// <returns>Cursor图标</returns>
        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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值