【问题标题】:Dynamically changing Mouse speed动态改变鼠标速度
【发布时间】:2011-02-25 05:52:10
【问题描述】:

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

【问题讨论】:

    标签: c# winforms panel


    【解决方案1】:

    This 文章可能会有所帮助

    这是文章中的代码:

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

    【讨论】:

    • 文章+1。对于icemanind:只需在“Enter”和“Leave”事件上调用本文指定的函数,进入控件时速度较慢,离开控件时速度较快,应该可以工作。
    • 完美。感谢您的帮助!
    【解决方案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");
                    }
    
                }
    
            }
    
    
        }
    
    }
    

    【讨论】:

    • 嗨 Meghraj,我有同样的要求,但我想在桌面上缓慢移动鼠标光标(我想改变速度)。我正在使用 SetCursorPos Win32 API 将光标移动到所需的点。你能解释更多来实现它吗?
    • public static extern int SystemParametersInfo( int uAction, int uParam, IntPtr lpvParam, int fuWinIni ) above system function 检索或设置系统范围参数之一的值。此功能还可以在设置参数时更新用户配置文件。要访问此函数/方法,我们需要导入 DllImport("user32.dll") uAction:- 在这里,我们传递控件操作的常量,在我的情况下,我在常量“SPI_SETMOUSESPEED”下方传递,用于设置/更改鼠标光标速度“SPI_GETMOUSESPEED”获取系统鼠标光标速度
    • uParam:- 一个参数,其用法和格式取决于被查询或设置的系统参数。有关系统范围参数的更多信息,请参阅 uiAction 参数。如果没有另外说明,您必须为此参数指定零。 lpvParam-:一个参数,其用法和格式取决于被查询或设置的系统参数。有关系统范围参数的更多信息,请参阅 uiAction 参数。如果没有另外说明,您必须为此参数指定 NULL。有关 PVOID 数据类型的信息,请参阅 Windows 数据类型
    • GetDefaults() - 这个方法用于获取系统当前的鼠标光标速度并存储在变量中。如果我想恢复旧值,我可以使用存储的鼠标光标速度 SetMouseSpeed(int intSpeed) - 这个方法我用来设置系统的鼠标光标速度。 SetDefaults() - 这个方法我用来恢复系统的鼠标光标速度。
    • @user1915370 ,请阅读我上面评论的所有细节。仍有任何疑问,请随时询问
    【解决方案3】:

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

    [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;Credits.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-16
      • 2023-01-21
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多