2010-05-28 79 views
4

夥計們,我有一個C#Winforms應用程序,其中包含一個面板。我想要做的是,每當鼠標指針進入該面板時,我想將鼠標的移動速度減慢50%。一旦指針移動到該面板之外,我想讓鼠標恢復正常的100%速度。我怎樣才能在C#中完成這項工作?動態更改鼠標速度

回答

3

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

+1的文章。對於icemanind:只需在「Enter」和「Leave」事件中調用本文中指定的函數,在輸入控件時速度較慢,離開控件時速度較快,它應該可以工作。 – 2010-05-28 17:33:39

+0

完美。謝謝您的幫助! – Icemanind 2010-05-28 17:52:03

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

      } 

     } 


    } 

}