当前位置:网站首页>C # set the cursor shape of forms and systems

C # set the cursor shape of forms and systems

2022-06-11 13:22:00 Non entropy~

During the development process, it is found that it is necessary to change the mouse style ( Cursor Icon ), We may set the cursor shape in a single form or the cursor shape in the system , We can create system cursor files (CUR file ) To replace the cursor shape , You can also replace the cursor shape with a picture .

Instance Links :c# Set the cursor shape of forms and systems ( Use cur or png)- Desktop system document resources -CSDN download

One 、 adopt CUR File set form cursor shape

        /// <summary>
        ///  Set the form cursor 
        /// </summary>
        /// <param name="cursor"></param>
        /// <param name="hotPoint"></param>
        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;
        }

Two 、 Set the form cursor shape through the picture file

        /// <summary>
        ///  Set the form cursor 
        /// </summary>
        /// <param name="cursor"></param>
        /// <param name="hotPoint"></param>
        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;
        }

3、 ... and 、 Set and cancel the system cursor shape

        /// <summary>
        ///  Cursor resource loading function 
        /// </summary>
        /// <param name="fileName"> Under load path .cur file </param>
        /// <returns></returns>
        [DllImport("User32.DLL")]
        public static extern IntPtr LoadCursorFromFile(string fileName);
        /// <summary>
        ///  Set system pointer function ( use hcur Replace id Defined cursor )
        /// </summary>
        /// <param name="hcur"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [DllImport("User32.DLL")]
        public static extern bool SetSystemCursor(IntPtr hcur, uint id);
        public const uint OCR_NORMAL = 32512;
        public const uint OCR_IBEAM = 32513;
        /// <summary>
        ///  System level parameter functions queried or set 
        /// </summary>
        /// <param name="uiAction"></param>
        /// <param name="uiParam"></param>
        /// <param name="pvParam"></param>
        /// <param name="fWinIni"></param>
        /// <returns></returns>
        [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;

        /// <summary>
        ///  Set the system cursor 
        /// </summary>
        private void SetCursor()
        {
            IntPtr hcur_click = LoadCursorFromFile("my.cur");
            SetSystemCursor(hcur_click, OCR_NORMAL);
            SetSystemCursor(hcur_click, OCR_IBEAM);
             Set up mobile 
            //cur = LoadCursorFromFile("my.cur");
            //SetSystemCursor(cur, OCR_SIZEALL);
             Settings not available 
            //cur = LoadCursorFromFile("my.cur");
            //SetSystemCursor(cur, OCR_NO);
             Set hyperlink 
            //cur = LoadCursorFromFile("my.cur");
            //SetSystemCursor(cur, OCR_HAND);
        }

       // Restore the system cursor 
       SystemParametersInfo(SPI_SETCURSORS, SPIF_SENDWININICHANGE, IntPtr.Zero, SPIF_SENDWININICHANGE);

Reference resources :

CUR Documentation :https://jingyan.baidu.com/article/afd8f4de444fad75e286e9f4.html

原网站

版权声明
本文为[Non entropy~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111318130961.html