自学内容网 自学内容网

C# 鼠标光标(获取与设置)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sci
{
    public class CursorTool
    {
        // 设置显示的鼠标光标
        // this.pictureBox2.Cursor = ToCursor(pic);

        /// <summary>
        /// 将pic转化未Cursor
        /// </summary>
        public static Cursor ToCursor(Bitmap pic)
        {
            try
            {
                return new Cursor(pic.GetHicon());
            }
            catch (Exception)
            {
                return Cursors.Default;
            }
        }

        /// <summary>
        /// 将pic转化未Cursor
        /// </summary>
        public static Cursor ToCursor(Image pic, int width)
        {
            Bitmap image = new Bitmap(width, width);
            Graphics graphics = Graphics.FromImage(image);
            graphics.Clear(Color.FromArgb(0, 0, 0, 0));
            graphics.DrawImage(pic, 0, 0, image.Width, image.Height);
            try
            {
                return new Cursor(image.GetHicon());
            }
            catch (Exception)
            {
                return Cursors.Default;
            }
        }

        /// <summary>
        /// 延时保存当前鼠标图像
        /// </summary>
        public static void DelaySaveCursor(int second = 10)
        {
            long time = DateTime.Now.Ticks;
            while(DateTime.Now.Ticks < time + 10000000 * second) Application.DoEvents();

            Directory.CreateDirectory("Cursor");
            GetCurCursor().Save("Cursor\\" + DateTime.Now.Ticks + ".png");
        }


        [StructLayout(LayoutKind.Sequential)]
        struct CURSORINFO
        {
            public int cbSize;
            public int flags;
            public IntPtr hCursor;
            public Point ptScreenPos;
        }

        [DllImport("user32.dll")]
        static extern bool GetCursorInfo(out CURSORINFO pci);

        private const int CURSOR_SHOWING = 0x00000001;


        [DllImport("user32.dll")]
        static extern IntPtr LoadCursorFromFile(string fileName);


        [DllImport("user32.dll")]
        static extern bool SetCursor(IntPtr cursorHandle);

        /// <summary>
        /// 获取当前鼠标图像
        /// </summary>
        /// <returns></returns>
        public static Bitmap GetCurCursor()
        {
            CURSORINFO vCurosrInfo;
            vCurosrInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
            GetCursorInfo(out vCurosrInfo);
            if ((vCurosrInfo.flags & CURSOR_SHOWING) != CURSOR_SHOWING) return null;

            //Cursor vCursor = new Cursor(vCurosrInfo.hCursor);
            //Icon icon = Icon.FromHandle(vCursor.Handle);

            Icon icon = Icon.FromHandle(vCurosrInfo.hCursor);
            Bitmap cursorPic = icon.ToBitmap();
            return cursorPic;
        }


        / <summary>
        / 获取当前鼠标图像(仅可获取当前应用程序上的)
        / </summary>
        / <returns></returns>
        //public static Bitmap GetCurCursor0()
        //{
        //    Icon icon = Icon.FromHandle(Cursor.Current.Handle);
        //    Bitmap cursorPic = icon.ToBitmap();
        //    return cursorPic;
        //}

    }
}


原文地址:https://blog.csdn.net/scimence/article/details/140547115

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!