Spring – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Runtime.InteropServices;
3  
4 namespace Spring.Utilities
5 {
6 /// <summary>
7 /// Setting and unsetting of mouse parameters such as the mouse acceleration and the mouse enhanced precision.
8 /// </summary>
9 /// <remarks>
10 /// https://stackoverflow.com/questions/24737775/toggle-enhance-pointer-precision and
11 /// https://stackoverflow.com/questions/2931122/dynamically-changing-mouse-speed
12 /// </remarks>
13 public class MouseOptions
14 {
15 #region Static Fields and Constants
16  
17 public const int SPI_GET_MOUSE_SPEED = 112;
18  
19 public const int SPI_SET_MOUSE_SPEED = 113;
20  
21 public const int SPI_GET_MOUSE_ENHANCED_PRECISION = 0x0003;
22  
23 public const int SPI_SET_MOUSE_ENHANCED_PRECISION = 0x0004;
24  
25 private const int DEFAULT_MOUSE_SPEED = 10;
26  
27 #endregion
28  
29 #region Public Methods
30  
31 [DllImport("user32.dll")]
32 public static extern int SystemParametersInfo(int uAction, int uParam, IntPtr lpvParam, int fuWinIni);
33  
34 public static void SetDefaultMouseSpeed()
35 {
36 SetMouseSpeed(DEFAULT_MOUSE_SPEED);
37 }
38  
39 public static void SetMouseEnhancedPrecision(bool set)
40 {
41 var mouseParams = new int[3];
42  
43 SystemParametersInfo(SPI_GET_MOUSE_ENHANCED_PRECISION,
44 0,
45 GCHandle.Alloc(mouseParams, GCHandleType.Pinned)
46 .AddrOfPinnedObject(),
47 0);
48  
49 mouseParams[2] = set ? 1 : 0;
50  
51 SystemParametersInfo(SPI_SET_MOUSE_ENHANCED_PRECISION,
52 0,
53 GCHandle.Alloc(mouseParams, GCHandleType.Pinned)
54 .AddrOfPinnedObject(),
55 0);
56 }
57  
58 public static bool GetMouseEnhancedPrecision()
59 {
60 var mouseParams = new int[3];
61  
62 SystemParametersInfo(SPI_GET_MOUSE_ENHANCED_PRECISION,
63 0,
64 GCHandle.Alloc(mouseParams, GCHandleType.Pinned)
65 .AddrOfPinnedObject(),
66 0);
67  
68 return mouseParams[2] == 1;
69 }
70  
71 public static int GetMouseSpeed()
72 {
73 var ptr = Marshal.AllocCoTaskMem(4);
74 SystemParametersInfo(SPI_GET_MOUSE_SPEED, 0, ptr, 0);
75 var intSpeed = Marshal.ReadInt32(ptr);
76 Marshal.FreeCoTaskMem(ptr);
77  
78 return intSpeed;
79 }
80  
81 public static bool SetMouseSpeed(int intSpeed)
82 {
83 var ptr = new IntPtr(intSpeed);
84  
85 var b = SystemParametersInfo(SPI_SET_MOUSE_SPEED, 0, ptr, 0);
86  
87 return b == 1;
88 }
89  
90 #endregion
91 }
92 }