[go: up one dir, main page]

Menu

[0dea27]: / main.cs  Maximize  Restore  History

Download this file

192 lines (186 with data), 7.5 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
[assembly: AssemblyTitle("MouseClickTool")]
[assembly: AssemblyProduct("MouseClickTool")]
[assembly: AssemblyCopyright("Copyright © 2024 lalaki.cn")]
[assembly: AssemblyVersion("1.5.0.0")]
[assembly: AssemblyFileVersion("1.5.0.0")]
namespace MouseClickTool
{/// <summary>
/// 怎么简单怎么来了
/// </summary>
///
public partial class main : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new main());
}
//新方法:https://stackoverflow.com/questions/5094398/how-to-programmatically-mouse-move-click-right-click-and-keypress-etc-in-winfo
class MouseSimulator
{
[DllImport("user32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public SendInputEventType type;
public MouseKeybdhardwareInputUnion mkhi;
}
[StructLayout(LayoutKind.Explicit)]
struct MouseKeybdhardwareInputUnion
{
[FieldOffset(0)]
public MouseInputData mi;
}
[Flags]
enum MouseEventFlags : uint
{
MOUSEEVENTF_LEFTDOWN = 0x0002,
MOUSEEVENTF_LEFTUP = 0x0004,
MOUSEEVENTF_RIGHTDOWN = 0x0008,
MOUSEEVENTF_RIGHTUP = 0x0010,
}
[StructLayout(LayoutKind.Sequential)]
struct MouseInputData
{
public int dx;
public int dy;
public uint mouseData;
public MouseEventFlags dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
enum SendInputEventType : int
{
InputMouse
}
public static void ClickLeftMouseButton()
{
INPUT mouseDownInput = new INPUT();
mouseDownInput.type = SendInputEventType.InputMouse;
mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));
INPUT mouseUpInput = new INPUT();
mouseUpInput.type = SendInputEventType.InputMouse;
mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
}
public static void ClickRightMouseButton()
{
INPUT mouseDownInput = new INPUT();
mouseDownInput.type = SendInputEventType.InputMouse;
mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTDOWN;
SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));
INPUT mouseUpInput = new INPUT();
mouseUpInput.type = SendInputEventType.InputMouse;
mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTUP;
SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
}
}
const int waitSeconds = 4;
bool isRunning = false;
void SystemExit()
{
Hide();
isRunning = false;
Environment.Exit(0);
}
delegate void testClick();
public main()
{
InitializeComponent();
this.Icon = SystemIcons.Application;
this.comboBox1.SelectedIndex = 0;
url.Click += (s, e) => Process.Start(url.Text);
btn_close.Click += (s, e) => SystemExit();
btn_min.Click += (s, e) => WindowState = FormWindowState.Minimized;
btn_close.MouseHover += (s, e) => btn_close.ForeColor = Color.IndianRed;
btn_close.MouseLeave += (s, e) => btn_close.ForeColor = Control.DefaultForeColor;
btn_min.MouseHover += (s, e) => btn_min.ForeColor = Color.DodgerBlue;
btn_min.MouseLeave += (s, e) => btn_min.ForeColor = Control.DefaultForeColor;
Resize += (s, e) =>
{
if (WindowState == FormWindowState.Maximized) WindowState = FormWindowState.Normal;
};
MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
Tag = e.Location;
}
};
MouseMove += (s, e) =>
{
if (e.Button == MouseButtons.Left && Tag is Point offset)
{
Cursor = Cursors.SizeAll;
Point newLocation = this.PointToScreen(e.Location);
newLocation.Offset(-offset.X, -offset.Y);
this.Location = newLocation;
}
};
MouseUp += (s, e) => Cursor = Cursors.Default;
Paint += (s, e) =>
{
var g = e.Graphics;
using (var pen = new Pen(Color.HotPink, 7f))
g.DrawLine(pen, Width, 0, 0, 0);
using (var border = new Pen(Color.FromArgb(55, 0, 0, 0)))
g.DrawRectangle(border, 0, 0, Width - 1, Height - 1);
};
btn_start.Click += (s, e) =>
{
if (isRunning)
{
SystemExit();
return;
}
if (int.TryParse(is_ms.Text, out int result) && result > 0)
{
isRunning = true;
is_ms.ReadOnly = true;
Task.Factory.StartNew(async () =>
{
await Task.Run(async () =>
{
var startText = btn_start.Text;
for (int i = 1; i < waitSeconds; i++)
{
btn_start.Text = string.Format("{0}({1})", startText, waitSeconds - i);
await Task.Delay(1000);
}
});
btn_start.Text = "停止";
testClick mouseClick = new testClick(MouseSimulator.ClickRightMouseButton);
if (this.comboBox1.SelectedIndex == 0)
{
mouseClick = new testClick(MouseSimulator.ClickLeftMouseButton);
}
while (isRunning)
{
await Task.Run(async () =>
{
mouseClick.Invoke();
await Task.Delay(result);
});
}
});
}
else
{
MessageBox.Show("输入的数字不正确,必须是正整数", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
};
}
}
}