c#设置系统参数,鼠标速度,滚轮速度为例

因项目需要在程序设置系统的鼠标速度及滚轮速度,在百度寻求解决方案,竟然没有找到,开始以为微软没有公开函数,后来没有办法,只能科学上网向谷歌寻求帮助,还真的找到了,而且资料还不少,还是外国牛人多~~~
直接贴代码
static class MouseConfig
    {
        /// 
        /// 鼠标速度
        /// 
        public const UInt32 SPI_SETMOUSESPEED = 0x0071;
        /// 
        /// 滚轮速度
        /// 
        public const UInt32 SPI_SETWHEELSCROLLLINES = 0x0069;


        //最大值为20,最小值为1
        [DllImport("User32.dll")]
        static extern Boolean SystemParametersInfo(
            UInt32 uiAction,
            UInt32 uiParam,
            UInt32 pvParam,
            UInt32 fWinIni);

        /// 
        /// 设置鼠标速度
        /// 
        /// 鼠标速度,最大值20,最小值1
        public static void SetMouseSpeed(uint speed)
        {
            uint mouseSpeed = speed;
            if (mouseSpeed < 1)
            {
                mouseSpeed = 1;
            }
            else if (mouseSpeed > 20)
            {
                mouseSpeed = 20;
            }
            SystemParametersInfo(SPI_SETMOUSESPEED, 0, mouseSpeed, 0);
        }

        /// 
        /// 获取当前鼠标速度
        /// 
        /// 鼠标速度
        public static int GetMouseSpeed()
        {
            return System.Windows.Forms.SystemInformation.MouseSpeed;
        }

        /// 
        /// 设置鼠标滚轮滚动行数
        /// 
        /// 滚轮每次滚动行数,最大值20,最小值1
        public static void SetMouseWheel(uint speed)
        {
            uint wheelSpeed = speed;
            if (wheelSpeed < 1)
            {
                wheelSpeed = 1;
            }
            else if (wheelSpeed > 20)
            {
                wheelSpeed = 20;
            }
            SystemParametersInfo(SPI_SETWHEELSCROLLLINES, wheelSpeed, 0, 0);
        }
        /// 
        /// 获取鼠标滚轮滚动行数
        /// 
        /// 滚轮行数
        public static int GetMouseWheel()
        {
            return System.Windows.Forms.SystemInformation.MouseWheelScrollLines;
        }
    }

SystemParametersInfo应该是属于接近系统的函数,.net并无相应的实现或封装(设置部分)。读取可以通过System.Windows.Forms.SystemInformation及System.Windows.SystemParameters读取出其中的大部分设置,仅限于读取。
uiAction 为读取或设置的目标参数。
uiParam 和pvParam用来读取或设置参数值,实际使用过程参照MSDN。
fWinIni 指示是否更新配置文件。

MSDN 链接: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724947(v=vs.85).aspx

你可能感兴趣的:(C#)