-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
555 lines (523 loc) · 22.5 KB
/
Main.cs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
using System.Diagnostics;
using WindowsAPI;
using static WindowsAPI.GetWindows;
using System.Xml;
using System.Windows.Forms.VisualStyles;
using System.Drawing;
using System.Windows.Forms;
using System;
namespace KeyInputMacro
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
/// <summary>
/// 重写
/// </summary>
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0312://WM_HOTKEY
switch (m.WParam.ToInt32())
{
case 0x001:
if (Main_ToolStrip_Script_Run.Enabled)
Main_ToolStrip_Script_Run_Click(null!, null!);
else if (Main_ToolStrip_Script_Stop.Enabled)
Main_ToolStrip_Script_Stop_Click(null!, null!);
break;
}
break;
}
base.WndProc(ref m);
}
private void Main_Load(object sender, EventArgs e)
{
Logger.LogAdd("程序启动");
if (Program.programArgs!.Length > 0)
if (File.Exists(Program.programArgs[0]))
OpenFileToScriptEditBox(Program.programArgs[0]);
if (!HotKey.RegisterHotKey(Handle, 0x001, HotKey.FsModifiers.MOD_CONTROL, Keys.OemPipe))
MessageBox.Show("错误: 快捷键存在冲突", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void Main_FormClosed(object sender, FormClosedEventArgs e)
{
HotKey.UnregisterHotKey(Handle, 0x001);
}
private void Main_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.O)
{
Main_ToolStrip_Script_Open_Click(null!, null!);
}
else if(e.Control && e.KeyCode == Keys.S)
{
Main_ToolStrip_Script_SaveAs_Click(null!, null!);
}
else if (e.Control && e.KeyCode == Keys.N)
{
Main_ToolStrip_Script_New_Click(null!, null!);
}
else if(e.Control && e.KeyCode == Keys.F)
{
Main_ToolStrip_Edit_Format_Click(null!, null!);
}
}
IReadOnlyList<WindowInfo>? windowsList;
IReadOnlyList<WindowInfo>? subWindowsList;
private void FlushWindowsList_Click(object sender, EventArgs e)
{
parentObjList.Items.Clear();
subObjList.Items.Clear();
subWindowsList = null;
windowsList = GetWindows.FindAllWindows();
foreach (var wind in windowsList)
{
parentObjList.Items.Add($"{wind.Title}; {wind.ClassName}");
}
}
private void ParentObjList_SelectedIndexChanged(object sender, EventArgs e)
{
subObjList.Items.Clear();
subWindowsList = GetWindows.FindAllChildWindows(windowsList![parentObjList.SelectedIndex].Hwnd, x => true);
foreach (var wind in subWindowsList)
{
subObjList.Items.Add($"{wind.Title}; {wind.ClassName}");
}
}
private void Main_ToolStrip_OpenTestWindow_Click(object sender, EventArgs e)
{
TestWindow tw = new();
tw.Show();
}
private void Main_ToolStrip_OpenLogWindow_Click(object sender, EventArgs e)
{
LogWindow lw = new();
lw.Show();
}
/// <summary>
/// 当对脚本编辑框的内容进行操作时,检查编辑框内是否包含内容,并做出相应反馈。
/// <br/>如果编辑框内包含内容,则询问用户是否继续。用户点击是,则返回true表示通过,否则为false
/// <br/>如果编辑框内没用任何内容,则返回true表示通过。
/// </summary>
/// <returns>返回true表示通过;返回false表示不通过</returns>
bool EditBoxTextCheck()
{
if (scriptEditBox.Text != "")
{
DialogResult input = MessageBox.Show("当前编辑框内包含内容,是否继续?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (input == DialogResult.Yes)
return true;
else
return false;
}
return true;
}
private void Main_ToolStrip_Script_New_Click(object sender, EventArgs e)
{
if (!EditBoxTextCheck())
return;
scriptEditBox.Text =
@"<KeyInputMacro>
<TargetObject/>
<Script>
</Script>
</KeyInputMacro>"
;
}
private void Main_ToolStrip_Script_Open_Click(object sender, EventArgs e)
{
if (!EditBoxTextCheck())
return;
if (OpenScriptFile.ShowDialog() == DialogResult.OK)
{
OpenFileToScriptEditBox(OpenScriptFile.FileName);
}
}
/// <summary>
/// 打开目标文件至文本编辑框内
/// </summary>
/// <param name="filePath">目标文件位置</param>
void OpenFileToScriptEditBox(string filePath)
{
try
{
StreamReader sr = new(filePath);
scriptEditBox.Text = sr.ReadToEnd();
sr.Close();
}
catch
{
MessageBox.Show("文件打开失败!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Main_ToolStrip_Script_SaveAs_Click(object sender, EventArgs e)
{
if (SaveAsScriptFile.ShowDialog() == DialogResult.OK)
{
try
{
XmlDocument xmlDoc = new();
xmlDoc.LoadXml(scriptEditBox.Text);
xmlDoc.Save(SaveAsScriptFile.FileName);
}
catch
{
MessageBox.Show("文件保存失败!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void Main_ToolStrip_Script_Run_Click(object sender, EventArgs e)
{
Main_ToolStrip_Script_Run.Enabled = false;
ScriptThreadStop = false;//重置脚本线程终止装置的状态
Main_ToolStrip_Script_Stop.Enabled = true;
scriptEditBox.Enabled = false;
string script = scriptEditBox.Text;
Logger.LogAdd("正在准备运行脚本...");
Thread thread = new(() =>
{
void ErrorMessage(string cause = "不明。")
{
string errstr = $"执行脚本时发生错误!原因: {cause}";
Logger.LogAdd(errstr);
MessageBox.Show(errstr, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
Logger.LogAdd("脚本执行线程因发生错误而终止");
this.Invoke(new Action(() =>
{
Main_ToolStrip_Script_Stop_Click(null!, null!);
}));
}
try
{
while (script.IndexOf("<!--") != -1) //对脚本进行处理
{
int[] index = new int[2];
index[0] = script.IndexOf("<!--");//去除所有注释
index[1] = script.IndexOf("-->", index[0]);
script = script.Remove(index[0], index[1] + 3 - index[0]);
}
IntPtr parentWindow = 0, subWindow = 0;//父窗口句柄与子窗口/控件句柄
IntPtr mainTarget;//主要目标句柄
XmlDocument xmlDoc = new();
xmlDoc.LoadXml(script);
script = null!;//释放该变量
XmlNode xmlRoot = xmlDoc.SelectSingleNode("KeyInputMacro")!;
XmlNodeList xmlNL = xmlRoot.ChildNodes;
XmlElement xmlE;
foreach (XmlNode xn in xmlNL)
{
xmlE = (XmlElement)xn;
if (xmlE.Name == "TargetObject")
{
parentWindow = GetWindows.FindWindow(xmlE.GetAttribute("windowClass"), xmlE.GetAttribute("windowName"));//读取xml文件中的目标信息
subWindow = GetWindows.FindWindowEx(parentWindow, 0, xmlE.GetAttribute("subClass"), xmlE.GetAttribute("subName"));
Logger.LogAdd($"已读取脚本中的目标信息: ({xmlE.GetAttribute("windowName")}; {xmlE.GetAttribute("windowClass")}; {xmlE.GetAttribute("subName")}; {xmlE.GetAttribute("subClass")})");
}
}
if (subWindow != 0)//判断子对象是否为空,不为空则赋值至主要变量
mainTarget = subWindow;
else if (parentWindow != 0)//同理
mainTarget = parentWindow;
else//如果都不满足,则错误
{ ErrorMessage("未找到或未指定执行目标。"); goto over; }
xmlRoot = xmlDoc.SelectSingleNode("KeyInputMacro")!.SelectSingleNode("Script")!;
xmlNL = xmlRoot.ChildNodes;
ScriptRunFramework srf = new()
{
mainTarget = mainTarget
};
Logger.LogAdd("准备完毕,开始执行脚本");
srf.Run(xmlNL);//开始执行脚本内容
Logger.LogAdd("脚本执行线程终止");
this.Invoke(new Action(() =>
{
Main_ToolStrip_Script_Stop_Click(null!, null!);//执行完毕后终止
}));
}
catch
#if DEBUG
(Exception ex)
#endif
{
#if DEBUG
Debug.WriteLine(ex.ToString());
#endif
ErrorMessage(); goto over;
}
over:;
}); thread.Start();
}
private void Main_ToolStrip_Script_Stop_Click(object sender, EventArgs e)
{
Main_ToolStrip_Script_Stop.Enabled = false;
ScriptThreadStop = true;//终止脚本执行线程
Main_ToolStrip_Script_Run.Enabled = true;
scriptEditBox.Enabled = true;
Logger.LogAdd("停止脚本执行");
}
/// <summary>
/// 用于终止脚本运行线程,设置为true后线程将控制终止
/// </summary>
static bool ScriptThreadStop = false;
struct ScriptRunFramework
{
/// <summary>
/// 主要目标句柄
/// </summary>
public IntPtr mainTarget;
/// <summary>
/// 执行按键操作
/// </summary>
/// <param name="actionType">按键操作类型</param>
/// <param name="keyStr">按键ID</param>
/// <param name="keyType">按键类型</param>
/// <param name="intervalStr">press类型的等待时间</param>
/// <param name="mousePos">鼠标进行操作的位置坐标</param>
private readonly void KeyActionRun(string actionType, string keyStr, string keyType, string intervalStr = "", string mousePos = "")
{
Keys keyCode = (Keys)Enum.Parse(typeof(Keys), keyStr.ToLower(), true);
int interval = 100;//按下和抬起的间隔时间
{
string GetIntervalTime = intervalStr;
if (GetIntervalTime != "")
interval = int.Parse(GetIntervalTime);
}
if (keyType.ToLower() == "key" || keyType == "") //(keyCode.ToString().ToLower().IndexOf("button") == -1)
{
if (actionType == "up") goto keyUp;
KeyInput.SendAction(keyCode, KeyInput.KeyAction.KeyDown, mainTarget);
if (actionType == "down") goto keyActionOver;
Thread.Sleep(interval);
keyUp:;
KeyInput.SendAction(keyCode, KeyInput.KeyAction.KeyUp, mainTarget);
keyActionOver:;
}
else if (keyType.ToLower() == "mouse")//判断是否为鼠标按键,如果是鼠标按键则另外执行
{
Point mousePosPoint;
if (mousePos == "") mousePosPoint = new(0, 0);
else mousePosPoint = new(int.Parse(mousePos.Split(',')[0]), int.Parse(mousePos.Split(',')[1]));
if (actionType == "up") goto mouseUp;
KeyInput.SendAction((KeyInput.MouseButton)keyCode, KeyInput.MouseAction.MouseDown, mainTarget, mousePosPoint);
if (actionType == "down") goto mouseActionOver;
Thread.Sleep(interval);
mouseUp:;
KeyInput.SendAction((KeyInput.MouseButton)keyCode, KeyInput.MouseAction.MouseUp, mainTarget, mousePosPoint);
mouseActionOver:;
}
Thread logT = new(() =>
{
string logStr = "";
logStr += "执行按键";
switch (actionType)
{
case "press":
logStr += "按压"; break;
case "down":
logStr += "按下"; break;
case "up":
logStr += "抬起"; break;
}
logStr += $"。按键: {keyCode}";
if (actionType == "press")
logStr += $"; 间隔时间: {interval}ms";
if (keyType == "mouse" && mousePos != "")
logStr += $"; 位置: [x: {mousePos.Split(',')[0]}; y: {int.Parse(mousePos.Split(',')[1])}]";
Logger.LogAdd(logStr);
}); logT.Start();
}
public void Run(XmlNodeList xmlNL)
{
foreach (XmlNode xn in xmlNL)
{
XmlElement xmlE;
xmlE = (XmlElement)xn;
switch (xmlE.Name)
{
case "press":
KeyActionRun(xmlE.Name, xmlE.GetAttribute("key"), xmlE.GetAttribute("type"), xmlE.GetAttribute("ms"), xmlE.GetAttribute("mouse_pos"));
break;
case "down":
case "up":
KeyActionRun(xmlE.Name, xmlE.GetAttribute("key"), xmlE.GetAttribute("type"), mousePos: xmlE.GetAttribute("mouse_pos"));
break;
case "mouse_move":
{
string posStr = xmlE.GetAttribute("pos");
/*string keyStr = xmlE.GetAttribute("key").ToLower();*/
Point pos = new(int.Parse(posStr.Split(',')[0]), int.Parse(posStr.Split(',')[1]));
KeyInput.MouseButton inputMouseButton = KeyInput.MouseButton.none;
/*if (keyStr != "")
inputMouseButton = (KeyInput.MouseButton)Enum.Parse(typeof(Keys),keyStr, true); */
KeyInput.SendAction(inputMouseButton, KeyInput.MouseAction.MouseMove, mainTarget, pos);
Logger.LogAdd($"执行鼠标移动。位置: [x: {pos.X}; y: {pos.Y}]");
}
break;
case "mouse_wheel":
{
string wheelValue = xmlE.GetAttribute("wheel");
string posStr = xmlE.GetAttribute("pos");
Point pos;
if (posStr == "")
pos = new(0, 0);
else
pos = new(int.Parse(posStr.Split(',')[0]), int.Parse(posStr.Split(',')[1]));
KeyInput.SendAction(KeyInput.MouseButton.none, KeyInput.MouseAction.MouseWheel, mainTarget, pos, int.Parse(wheelValue));
Logger.LogAdd($"执行鼠标滚轮滚动。距离: {wheelValue}; 位置: [x: {pos.X}; y: {pos.Y}]");
}
break;
case "text":
{
string targetText = xmlE.GetAttribute("txt");
KeyInput.SendAction(targetText, mainTarget);
Logger.LogAdd($"执行输入文本。文本内容: {targetText}");
}
break;
case "loop":
{
Logger.LogAdd($"进入循环执行");
ScriptRunFramework srf = new()
{
mainTarget = mainTarget
};
while (!ScriptThreadStop)
srf.Run(xn.ChildNodes);
Logger.LogAdd($"退出循环执行");
}
break;
case "wait":
Logger.LogAdd($"执行等待。等待时间: {xmlE.GetAttribute("ms")}ms");
Thread.Sleep(int.Parse(xmlE.GetAttribute("ms")));
break;
}
if (ScriptThreadStop)
break;
}
}
/*/// <summary>
/// 根据xml中的字符串获取key类型
/// </summary>
/// <param name="input">输入的字符串</param>
/// <returns></returns>
Keys GetKey(string input)
{
switch (input)
{
}
}*/
/*/// <summary>
/// 按键列表枚举
/// </summary>
enum KeysList
{
a=Keys.A, b=Keys.B,c=Keys.C,d=Keys.D,e=Keys.E,f=Keys.F,g=Keys.G,h=Keys.H,i=Keys.I,j=Keys.J,k=Keys.K,l=Keys.L,m=Keys.M,n=Keys.N,o=Keys.O,p=Keys.P,q=Keys.Q,r=Keys.R,s=Keys.S,t=Keys.T,u=Keys.U,v=Keys.V,w=Keys.W,x=Keys.X,y=Keys.Y,z=Keys.Z,
lwin=Keys.LWin, rwin=Keys.RWin,menu=Keys.Apps,
numpad0=Keys.NumPad0,numpad1=Keys.NumPad1,numpad2=Keys.NumPad2,numpad3=Keys.NumPad3,numpad4=Keys.NumPad4,numpad5=Keys.NumPad5,numpad6=Keys.NumPad6,numpad7=Keys.NumPad7,numpad8=Keys.NumPad8,numpad9=Keys.NumPad9,
}*/
}
private void WriteObjToScriptButton_Click(object sender, EventArgs e)
{
try
{
string[] writeText = new string[4];
if (windowsList != null && windowsList.Count > 0 && parentObjList.SelectedIndex != -1)
{
writeText[0] = windowsList[parentObjList.SelectedIndex].Title;
writeText[1] = windowsList[parentObjList.SelectedIndex].ClassName;
}
if (subWindowsList != null && subWindowsList.Count > 0 && subObjList.SelectedIndex != -1)
{
writeText[2] = subWindowsList[subObjList.SelectedIndex].Title;
writeText[3] = subWindowsList[subObjList.SelectedIndex].ClassName;
}
XmlDocument xmlDoc = new();
XmlNodeList xmlNL;
XmlElement xmlEle;
xmlDoc.LoadXml(scriptEditBox.Text);
xmlNL = xmlDoc.SelectSingleNode("KeyInputMacro")!.ChildNodes;
foreach (XmlNode xn in xmlNL)
{
xmlEle = (XmlElement)xn;
if (xmlEle.Name == "TargetObject")
{
xmlEle.SetAttribute("windowName", writeText[0]);
xmlEle.SetAttribute("windowClass", writeText[1]);
xmlEle.SetAttribute("subName", writeText[2]);
xmlEle.SetAttribute("subClass", writeText[3]);
break;
}
}
scriptEditBox.Text = FormatXML(xmlDoc);
}
catch { MessageBox.Show("写入失败!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
/// <summary>
/// 格式化XmlDocument类型中的xml内容
/// </summary>
/// <param name="xmldoc">目标XmlDocument类型</param>
/// <returns>返回格式化后的xml文本</returns>
///
static string FormatXML(XmlDocument xmldoc)
{
System.IO.StringWriter sw = new();
System.Xml.XmlTextWriter xtw = new(sw)
{
Indentation = 4,//缩进长度
Formatting = System.Xml.Formatting.Indented
};
xmldoc.WriteContentTo(xtw);
xtw.Close();
return sw.ToString();
}
private void Main_ToolStrip_Edit_Format_Click(object sender, EventArgs e)
{
try
{
XmlDocument xmlDoc = new();
xmlDoc.LoadXml(scriptEditBox.Text);
scriptEditBox.Text = FormatXML(xmlDoc);
}
catch { MessageBox.Show("格式化失败!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
private void ScriptEditBox_DragDrop(object sender, DragEventArgs e)
{
Array inputData = (Array)e.Data!.GetData(DataFormats.FileDrop)!;
string[] filePathData = new string[inputData.Length];
for (int i = 0; i < inputData.Length; i++)
{
filePathData[i] = inputData.GetValue(i)!.ToString()!;
}
OpenFileToScriptEditBox(filePathData[0]);
}
private void ScriptEditBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data!.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Main_ToolStrip_help_about_Click(object sender, EventArgs e)
{
MessageBox.Show(
$@"程序名: KeyInputMacro
别名: 按键输入宏
版本: V{Program.version}
Copyright (C) 2024 Hgnim, All rights reserved.
Github: https://github.com/Hgnim/KeyInputMacro",
"关于");
}
private void Main_ToolStrip_help_helpDoc_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", "https://github.com/Hgnim/KeyInputMacro/wiki/%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3");
}
}
}