C# 设置窗体和系统的光标形状

开发过程中发现需要用到改变鼠标样式(光标图标),我们可能设置单个窗体中光标形状或者光标在系统中的形状,我们可以通过制作系统光标文件(CUR文件)来替换光标形状,也可通过图片来替换光标形状。

实例链接:c#设置窗体和系统的光标形状(使用cur或png)-桌面系统文档类资源-CSDN下载

一、通过CUR文件设置窗体光标形状

        /// 
        /// 设置窗体光标
        /// 
        /// 
        /// 
        public void SetCursor(Bitmap cursor, Point hotPoint)
        {
            int hotX = hotPoint.X;
            int hotY = hotPoint.Y;
            Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g = Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,
            cursor.Height);
            this.Cursor = new Cursor(myNewCursor.GetHicon());
            g.Dispose();
            myNewCursor.Dispose();
        }

        private void btnSetFormCursor_Click(object sender, EventArgs e)
        {
            this.Cursor = new Cursor("my.cur");
        }

        private void btnRestoreFormCursor_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }

二、通过图片文件设置窗体光标形状

        /// 
        /// 设置窗体光标
        /// 
        /// 
        /// 
        public void SetCursor(Bitmap cursor, Point hotPoint)
        {
            int hotX = hotPoint.X;
            int hotY = hotPoint.Y;
            Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g = Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,
            cursor.Height);
            this.Cursor = new Cursor(myNewCursor.GetHicon());
            g.Dispose();
            myNewCursor.Dispose();
        }

        private void btnSetCursorByPng_Click(object sender, EventArgs e)
        {
            Bitmap a = (Bitmap)Bitmap.FromFile("my.png");
            SetCursor(a, new Point(0, 0));
        }

        private void btnStoreCursorByPng_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }

三、设置和取消系统光标形状

        /// 
        /// 光标资源加载函数
        /// 
        /// 加载路径下的.cur文件
        /// 
        [DllImport("User32.DLL")]
        public static extern IntPtr LoadCursorFromFile(string fileName);
        /// 
        /// 设置系统指针函数(用hcur替换id定义的光标)
        /// 
        /// 
        /// 
        /// 
        [DllImport("User32.DLL")]
        public static extern bool SetSystemCursor(IntPtr hcur, uint id);
        public const uint OCR_NORMAL = 32512;
        public const uint OCR_IBEAM = 32513;
        /// 
        /// 查询或设置的系统级参数函数
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("User32.DLL")]
        public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
        public const uint SPI_SETCURSORS = 87;
        public const uint SPIF_SENDWININICHANGE = 2;

        /// 
        /// 设置系统光标
        /// 
        private void SetCursor()
        {
            IntPtr hcur_click = LoadCursorFromFile("my.cur");
            SetSystemCursor(hcur_click, OCR_NORMAL);
            SetSystemCursor(hcur_click, OCR_IBEAM);
            设置移动
            //cur = LoadCursorFromFile("my.cur");
            //SetSystemCursor(cur, OCR_SIZEALL);
            设置不可用
            //cur = LoadCursorFromFile("my.cur");
            //SetSystemCursor(cur, OCR_NO);
            设置超链接
            //cur = LoadCursorFromFile("my.cur");
            //SetSystemCursor(cur, OCR_HAND);
        }

       //恢复系统光标
       SystemParametersInfo(SPI_SETCURSORS, SPIF_SENDWININICHANGE, IntPtr.Zero, SPIF_SENDWININICHANGE);

参考:

CUR文件制作:https://jingyan.baidu.com/article/afd8f4de444fad75e286e9f4.html

你可能感兴趣的:(C#,c#,开发语言)