动态改变鼠标速度

发布于 2024-09-03 05:05:24 字数 127 浏览 4 评论 0原文

伙计们,我有一个 C# Winforms 应用程序,表单内有一个面板。我想做的是,每当鼠标指针进入这个面板时,我想将鼠标的移动速度减慢50%。一旦指针移出该面板,我希望鼠标速度恢复正常的 100% 速度。我怎样才能在 C# 中完成这个任务?

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#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风几经秋 2024-09-10 05:05:24

这篇文章可能会有所帮助

以下是文章中的代码:

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);
        }
    }
}

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);
        }
    }
}
疯了 2024-09-10 05:05:24
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");
                }

            }

        }


    }

}
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");
                }

            }

        }


    }

}
牵强ㄟ 2024-09-10 05:05:24

由于不太清楚如何使用答案中的代码,我找到了更简洁的解决方案来更改鼠标速度。将此代码添加到要更改速度的类中:

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

然后使用所需的鼠标速度调用 SystemParametersInfo:

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

不要忘记添加 using System.Runtime.InteropServices; 致谢。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文