MouseManager.cs
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TSA_V.Common;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace TSA_V
{
public class MouseManager
{
public static void Start()
{
Task.Run(() =>
{
Run();
});
}
public static void Stop()
{
mstart = false;
}
static bool mstart = true;
public static void Run()
{
mstart = true;
while (mstart)
{
try
{
Thread.Sleep(50);
if (!Setting_NInit.Set_CursorProcess)
{
}
else
{
MouseProcess();
}
}
catch (Exception ex)
{
LogUtil.error("光标处理出错:" + ex.ToString());
}
}
LogUtil.info("光标位置处理线程已退出.");
}
private static Screen GetMainScreen(int index = -1)
{
Screen[] sc = Screen.AllScreens;
if (sc.Count() > index)
{
if (index < 0 || index >= sc.Length)
{
int i = 0;
foreach (Screen s in sc)
{
if (s.Primary)
{
return s;
}
}
}
}
return null;
}
[System.Runtime.InteropServices.DllImport("user32.dll")] //导入user32.dll函数库
public static extern bool GetCursorPos(out System.Drawing.Point lpPoint);//获取鼠标坐标
[DllImport("user32.dll")]
private static extern int SetCursorPos(int x, int y);
private static void MouseProcess()
{
GetCursorPos(out Point mp);
int mousex = mp.X; //鼠标当前X坐标
int mousey = mp.Y; //鼠标当前Y坐标
int newX = mousex;
int newY = mousey;
Screen mainScreen = GetMainScreen();
if (mousex < mainScreen.Bounds.Left)
{
newX = mainScreen.Bounds.Left;
SetCursorPos(newX, newY);
}
else if (mousex > mainScreen.Bounds.Right)
{
newX = mainScreen.Bounds.Right;
SetCursorPos(newX, newY);
}
if (mousey < mainScreen.Bounds.Top)
{
newY = mainScreen.Bounds.Top;
SetCursorPos(newX, newY);
}
else if (mousey > mainScreen.Bounds.Bottom)
{
newX = mainScreen.Bounds.Bottom;
SetCursorPos(newX, newY);
}
}
}
}