13

Guys, I have a C# Winforms application with a panel inside the form. What I want to do is, whenever the mouse pointer enters this panel, I want to slow the movement speed of the mouse by 50%. Once the pointer moves outside this panel, I want to speed of the mouse to resume normal 100% speed. How can I accomplish this in C#?

Icemanind
  • 47,519
  • 50
  • 171
  • 296

3 Answers3

6

This article might help

Here's the code from the article:

using System;
using System.Runtime.InteropServices;

namespace MouseSpeedSwitcher
{
    class Program
    {
        public const UInt32 SPI_SETMOUSESPEED = 0x0071;

        [DllImport("User32.dll")]
        static extern Boolean SystemParametersInfo(
            UInt32 uiAction, 
            UInt32 uiParam, 
            UInt32 pvParam,
            UInt32 fWinIni);

        static void Main(string[] args)
        {
            SystemParametersInfo(
                SPI_SETMOUSESPEED, 
                0, 
                uint.Parse(args[0]), 
                0);
        }
    }
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112
  • 1
    +1 for the article. For icemanind: just call the function specified in this article on the "Enter" and "Leave" events, slower when the control is entered and faster when leaving the control, and it should work. – Mike Webb May 28 '10 at 17:33
2
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
    class Program
    {
        public const UInt32 SPI_GETMOUSESPEED = 0x0070;


        const UInt32 SPIF_UPDATEINIFILE = 0x01;
        const UInt32 SPIF_SENDWININICHANGE = 0x02;

        [DllImport("User32.dll")]
        static extern Boolean SystemParametersInfo(
            UInt32 uiAction,
            UInt32 uiParam,
            IntPtr pvParam,
            UInt32 fWinIni);

        static unsafe void Main(string[] args)
        {
           MouseOptions m = new MouseOptions();

            MouseOptions.GetDefaults();
            int speed;
            SystemParametersInfo(SPI_GETMOUSESPEED, 0, new IntPtr(&speed), 0);
            Console.WriteLine(speed);

            MouseOptions.SetDefaults();


            SystemParametersInfo(SPI_GETMOUSESPEED, 0, new IntPtr(&speed), 0);
            Console.WriteLine(speed);


            Console.ReadLine();
        }



        public class MouseOptions
        {
            [DllImport("user32.dll")]
            public static extern int SystemParametersInfo( int uAction, int uParam, IntPtr lpvParam, int fuWinIni );

            [DllImport("kernel32.dll")]
            public static extern int GetLastError();

            public const int SPI_GETMOUSESPEED = 112;
            public const int SPI_SETMOUSESPEED = 113;


            private static int intDefaulSpeed = 10;
            private static int intCurrentSpeed;
            private static int intNewSpeed;

            public static void GetDefaults()
            {
                intCurrentSpeed = GetMouseSpeed();
            }
            public static void SetDefaults()
            {
                if ( intCurrentSpeed == 20 )
                {
                    SetMouseSpeed(intDefaulSpeed); 
                }
                else if ( intCurrentSpeed < 10 )
                {
                    SetMouseSpeed(intDefaulSpeed);   
                }
            }

            public static int GetMouseSpeed()
            {
                int intSpeed = 0;
                IntPtr ptr;
                ptr = Marshal.AllocCoTaskMem(4);
                SystemParametersInfo(SPI_GETMOUSESPEED, 0, ptr, 0);
                intSpeed = Marshal.ReadInt32(ptr);
                Marshal.FreeCoTaskMem(ptr);

                return intSpeed;
            }

            public static void SetMouseSpeed( int intSpeed )
            {
                IntPtr ptr = new IntPtr(intSpeed);

                int b = SystemParametersInfo(SPI_SETMOUSESPEED, 0, ptr, 0);

                if (b == 0)
                {
                    Console.WriteLine("Not able to set speed");
                }
                else if ( b == 1 )
                {
                    Console.WriteLine("Successfully done");
                }

            }

        }


    }

}
  • Hi Meghraj, I have the same requirement but I want to move the mouse cursor on a desktop slowly (I want to change speed). I am using SetCursorPos Win32 API to set move cursor to the required point. Can you explain more to achieve it ? – Rajendar Manda Aug 09 '18 at 13:49
  • public static extern int SystemParametersInfo( int uAction, int uParam, IntPtr lpvParam, int fuWinIni ) Above system function Retrieves or sets the value of one of the system-wide parameters. This function can also update the user profile while setting a parameter. To access this function/method we need to import DllImport("user32.dll") uAction:- Here we pass constant of control's operation in my case I am passing below constant "SPI_SETMOUSESPEED" for setting/changing mouse cursor speed "SPI_GETMOUSESPEED" for to get system's mouse cursor speed – user8851697 Aug 16 '18 at 09:49
  • uParam:- A parameter whose usage and format depends on the system parameter being queried or set. For more information about system-wide parameters, see the uiAction parameter. If not otherwise indicated, you must specify zero for this parameter. lpvParam-: A parameter whose usage and format depends on the system parameter being queried or set. For more information about system-wide parameters, see the uiAction parameter. If not otherwise indicated, you must specify NULL for this parameter. For information on the PVOID datatype, see Windows Data Types – user8851697 Aug 16 '18 at 09:50
  • GetDefaults() -This method i used for getting system's current mouse cursor speed & storing in variable. If i want to restore old value i can use stored mouse cursor speed SetMouseSpeed( int intSpeed ) -This method i used for setting system's mouse cursor speed. SetDefaults() - This method I used to restore system's mouse cursor speed. – user8851697 Aug 16 '18 at 09:51
  • @user1915370 , please read all details i had commented above.Still there is any doubt you have please free to ask – user8851697 Aug 16 '18 at 09:53
0

As it was not very clear how to use code from the answers, I found more concise solution for changing Mouse speed. Add this code to class where you want to change the speed:

[DllImport("user32.dll", CharSet = CharSet.Auto),]
public static extern int SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);

And then call SystemParametersInfo with required Mouse speed:

//SPEED is an integer value between 0 and 20. 10 is the default.
SystemParametersInfo(113,0,SPEED,0);

Not forget to add using System.Runtime.InteropServices; Credits.

Creek Drop
  • 464
  • 3
  • 13