-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathMainForm.cs
614 lines (508 loc) · 19.3 KB
/
MainForm.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
using AntdUI;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using MobileControlGuru.Base;
using MobileControlGuru.Model;
using MobileControlGuru.Properties;
using MobileControlGuru.Src;
using MobileControlGuru.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MobileControlGuru
{
public partial class MainForm : BaseForm
{
WebAPI.WebHelper webservice = new WebAPI.WebHelper();
private MyHotKey hotkey;
public MainForm()
{
InitializeComponent();
resources = new ComponentResourceManager(typeof(MainForm));
this.Resize += new EventHandler(MainForm_Resize);
//DeviceManager.Instance.updateDelegates += UpdateDeviceInfo;
DeviceManager.Instance.updateDelegates += UpdateDeviceList;
this.api_close.Visible = false;
}
private void Mian_Load(object sender, EventArgs e)
{
InitScrcpy();
//InitTable();
hotkey = new MyHotKey(this);
webservice = new WebAPI.WebHelper();
DeviceManager.Instance.UpdateDevices();
this.x_input.Text= Properties.Settings.Default.ClickPoint.X.ToString();
this.y_input.Text = Properties.Settings.Default.ClickPoint.Y.ToString();
var lang= Tools.ConfigHelp.GetConfig("Lang");
if (!string.IsNullOrEmpty(lang))
{
ChangeLang(lang);
ApplyResource();
}
Init();
}
private void InitScrcpy()
{
if (!Scrcpy.ChcekScrcpyPATH())
{
Scrcpy.ExtractScrcpyZip(Directory.GetCurrentDirectory());
}
}
public void UpdateDeviceList()
{
if (this.flowLayoutPanel1.InvokeRequired)
{
this.flowLayoutPanel1.Invoke((MethodInvoker)delegate
{
this.flowLayoutPanel1.Controls.Clear();
foreach (var d in DeviceManager.Instance.devices)
{
var s = new MoblieControl(d);
this.flowLayoutPanel1.Controls.Add(s);
}
});
}
else
{
this.flowLayoutPanel1.Controls.Clear();
foreach (var d in DeviceManager.Instance.devices)
{
var s = new MoblieControl(d);
this.flowLayoutPanel1.Controls.Add(s);
}
}
}
#region table
private void InitTable()
{
AntdUI.Column[] cols = new Column[] {
new AntdUI.ColumnCheck("IsSelected"),
new AntdUI.Column("Name", resources.GetString("tableNameText")),
new AntdUI.Column("status", resources.GetString("tableStatusText")),
new AntdUI.Column("btns", resources.GetString("tablebtnsText")),
// new AntdUI.Column("Name", "设备"),
};
this.table1.Columns = cols;
this.table1.EmptyText = resources.GetString("tableNoDataText");// "暂无设备,请连接设备后刷新";
}
private void Init()
{
this.dropdown1.Items.Clear();
this.dropdown1.Items.AddRange(new object[] {
resources.GetString("AboutText"),
resources.GetString("HotSetText")
});
this.Text = this.Text +"V"+System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
public AntdUI.AntList<DeviceItem> deviceItems = new AntList<DeviceItem>();
public void UpdateDeviceInfo()
{
deviceItems = new AntList<DeviceItem>();
foreach (var item in DeviceManager.Instance.devices)
{
deviceItems.Add(new DeviceItem(item));
}
this.table1.Binding(deviceItems);
}
/// <summary>
/// 列表 按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="btn"></param>
/// <param name="args"></param>
/// <param name="record"></param>
/// <param name="rowIndex"></param>
/// <param name="columnIndex"></param>
private void table1_CellButtonClick(object sender, CellLink btn, MouseEventArgs args, object record, int rowIndex, int columnIndex)
{
Model.DeviceItem deviceItem = (Model.DeviceItem)record;
var d = DeviceManager.Instance.GetDevice(deviceItem.Name);
Process process = null;
switch (btn.Id)
{
case "usb_put":
process = Scrcpy.UsbPut(deviceItem.Name);
DeviceManager.Instance.BindDevicesProcees(deviceItem.Name, process);
break;
case "ip_put":
process = Scrcpy.IPPut(deviceItem.Name);
DeviceManager.Instance.BindDevicesProcees(deviceItem.Name, process);
//d.monitor = new ScrcpyMonitor(d.Name);
//d.monitor.main = this;
//d.monitor.Start();
break;
case "close_put":
if (d.ScrcpyProcess != null)
{
if (!d.ScrcpyProcess.HasExited)
{
d.ScrcpyProcess.Kill(); // 强制结束进程
}
d.ScrcpyProcess = null;
}
break;
case "disconnected":
if (d.ScrcpyProcess != null)
{
if (!d.ScrcpyProcess.HasExited)
{
d.ScrcpyProcess.Kill(); // 强制结束进程
}
d.ScrcpyProcess = null;
}
ADB.Exec("disconnect " + d.Name);
DeviceManager.Instance.UpdateDevices();
break;
}
DeviceManager.Instance.UpdateDevices();
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
DeviceManager.Instance.UpdateDevices();
}
private void dropdown1_SelectedValueChanged(object sender, object value)
{
if (value.ToString()== resources.GetString("AboutText")) {
var about = new About();
about.StartPosition = FormStartPosition.CenterScreen;
about.Show();
}else if (value.ToString() == resources.GetString("HotSetText"))
{
var hot = new HotKeySetting(hotkey);
hot.StartPosition = FormStartPosition.CenterScreen;
hot.Show();
}
}
private void exit_tsmi_Click(object sender, EventArgs e)
{
DeviceManager.Instance.CloseAll();
hotkey.UnRegister();
System.Environment.Exit(System.Environment.ExitCode);
this.Dispose();
this.Close();
}
private void ipconnect_btn_Click(object sender, EventArgs e)
{
IPConnect iPConnect = new IPConnect(this);
iPConnect.StartPosition = FormStartPosition.CenterParent;
iPConnect.ShowDialog(this);
}
#region I18n
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (lang_select.SelectedIndex == 0)
{
Tools.ConfigHelp.SetSetting("Lang", "zh-CN");
ChangeLang("zh-CN");
ApplyResource();
//InitTable();
Init();
DeviceManager.Instance.UpdateDevices();
}
else
{
Tools.ConfigHelp.SetSetting("Lang", "en");
ChangeLang("en");
ApplyResource();
//InitTable();
Init();
DeviceManager.Instance.UpdateDevices();
}
}
//private void ChangeLang(string lang)
//{
// Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
// ApplyResource();
//}
// 遍历控件,并根据资源文件替换相应属性
//private void ApplyResource()
//{
// foreach (Control ctl in this.Controls)
// {
// resources.ApplyResources(ctl, ctl.Name);
// }
// this.ResumeLayout(false);
// this.PerformLayout();
// resources.ApplyResources(this, "$this");
// InitTable();
//}
#endregion
#region 全局快捷键
protected override void WndProc(ref System.Windows.Forms.Message m)
{
const int WM_HOTKEY = 0x0312;
switch (m.Msg)
{
//监听快捷键 消息
case WM_HOTKEY:
hotkey.Deal(this,m.WParam.ToInt32());
break;
}
base.WndProc(ref m);
}
#endregion
// 窗口已经被最小化
private void MainForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
}
}
private void Mian_FormClosed(object sender, FormClosedEventArgs e)
{
// 检查关闭的原因是否为用户操作(CloseReason.UserClosing)
DeviceManager.Instance.CloseAll();
hotkey.UnRegister();
}
private void Mian_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
// 隐藏主窗体而不是退出
this.Hide();
notifyIcon1.Visible = true;
}
}
private void main_tsmi_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.BringToFront();
this.Activate();
}
private void button3_Click(object sender, EventArgs e)
{
using (CommonOpenFileDialog dialog = new CommonOpenFileDialog())
{
dialog.IsFolderPicker = true; // 设置为文件夹选择器
dialog.Multiselect = false; // 允许选择多个文件夹
dialog.Title = "请选择文件夹"; // 设置对话框标题
dialog.DefaultFileName = "Scrcpy";
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
var s= dialog.FileName;
Scrcpy.ExtractScrcpyZip(s);
}
}
}
private void api_run_Click(object sender, EventArgs e)
{
try
{
if (CheckAdminPrivileges())
{
if (webservice.Runnig) {
System.Diagnostics.Process.Start("explorer.exe", webservice.Url + "/swagger");
}
else
{
webservice.Run();
webservice.Runnig = true;
//System.Diagnostics.Process.Start("explorer.exe", webservice.Url + "/swagger");
this.api_close.Visible = true;
}
((AntdUI.Button)sender).Text = "查看文档";
}
else
{
MessageBox.Show("API服务需要以管理员方式打开,如果需要请以管理方式重启应用");
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void api_close_Click(object sender, EventArgs e)
{
DialogResult AF = MessageBox.Show("确定关闭API服务吗?", "确认框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (AF == DialogResult.OK)
{
webservice?.Dispose();
this.api_run.Text = "开启API";
((AntdUI.Button)sender).Visible = false;
}
else
{
//用户点击取消或者关闭对话框后执行的代码
}
}
private bool CheckAdminPrivileges()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
bool isAdministrator = principal.IsInRole(WindowsBuiltInRole.Administrator);
return isAdministrator;
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.BringToFront();
this.Activate();
}
private void button4_Click(object sender, EventArgs e)
{
ScrcpySetting scrcpysetting = new ScrcpySetting();
scrcpysetting.StartPosition = FormStartPosition.CenterParent;
scrcpysetting.ShowDialog(this);
}
private void button5_Click(object sender, EventArgs e)
{
ScrcpyConrtrolPanel scrcpysetting = new ScrcpyConrtrolPanel();
//scrcpysetting.StartPosition = FormStartPosition.CenterParent;
scrcpysetting.Show();
}
#region scrcpy 控制器
ScrcpyConrtrolPanel conrtrolPanel = new ScrcpyConrtrolPanel();
public void ShowControl(Device device)
{
//conrtrolPanel.StartPosition = FormStartPosition.CenterParent;
conrtrolPanel.Device = device;
conrtrolPanel.ShowInTaskbar = false;
conrtrolPanel.Activate();
conrtrolPanel.Show();
}
public void HideControl()
{
conrtrolPanel.Show();
}
public void SetControl(ProcessWindowController.RECT fx)
{
int x = fx.Right;
int y = fx.Top;
int width = fx.Right - fx.Left; //窗口的宽度
//int height = fx.Bottom - fx.Top;
conrtrolPanel.Location=new Point(x, y);
//conrtrolPanel.Width = width;
//conrtrolPanel.Height = fx.Top - fx.Bottom;
}
#endregion
private void setting_tmsi_Click(object sender, EventArgs e)
{
ScrcpySetting scrcpysetting = new ScrcpySetting();
scrcpysetting.StartPosition = FormStartPosition.CenterParent;
scrcpysetting.ShowDialog(this);
}
public Point point=new Point();
//private void button5_Click_1(object sender, EventArgs e)
//{
// point= new Point(Convert.ToInt32(x_input.Text), Convert.ToInt32(y_input.Text));
// Properties.Settings.Default.ClickPoint=point;
// Properties.Settings.Default.Save();
// var dlist= this.deviceItems.Where(n => n.IsSelected).ToList();
// foreach( var ditem in dlist) {
// new DeviceADB(ditem.Name).TapScreen(point);
// }
//}
#region 批量操作按钮
private void button_Click(object sender, EventArgs e)
{
var selecteds = DeviceManager.Instance.devices.Where(n => n.Selected).ToList();
point = new Point(Convert.ToInt32(x_input.Text), Convert.ToInt32(y_input.Text));
Properties.Settings.Default.ClickPoint = point;
Properties.Settings.Default.Save();
if (selecteds.Count() == 0)
{
AntdUI.Message.error(this, "请选择要操作的设备");
return;
}
var btn= ((AntdUI.Button)sender);
string btn_name = btn.Name;
if (btn_name.Contains("keycode"))
{
foreach (var ditem in selecteds)
{
new DeviceADB(ditem.Name).SendKeyEvent(btn.Tag.ToString());
}
}
if (btn_name.Contains("swipe"))
{
if (btn.Tag.ToString() == "up")
{
foreach (var ditem in selecteds)
{
new DeviceADB(ditem.Name).SendSwipeUp();
}
}
else
{
foreach (var ditem in selecteds)
{
new DeviceADB(ditem.Name).SendSwipeDown();
}
}
}
if (btn_name.Contains("tap"))
{
point = new Point(Convert.ToInt32(x_input.Text), Convert.ToInt32(y_input.Text));
foreach (var ditem in selecteds)
{
new DeviceADB(ditem.Name).TapScreen(point);
}
}
}
private void button1_Click_1(object sender, EventArgs e)
{
var selecteds = DeviceManager.Instance.devices.Where(n => n.Selected).ToList();
if (selecteds.Count() == 0)
{
AntdUI.Message.error(this, "请选择要操作的设备");
return;
}
if (string.IsNullOrEmpty(this.keycode_input.Text))
{
return;
}
foreach (var ditem in selecteds)
{
new DeviceADB(ditem.Name).SendKeyEvent(this.keycode_input.Text);
}
}
#endregion
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
}
System.Windows.Forms.Timer timer =new System.Windows.Forms.Timer();
private void button2_Click(object sender, EventArgs e)
{
if (timer.Enabled) {
timer.Enabled = false;
timer.Stop();
}
else
{
}
}
private void button3_Click_1(object sender, EventArgs e)
{
IPPair ippair = new IPPair(this);
ippair.StartPosition = FormStartPosition.CenterParent;
ippair.ShowDialog(this);
}
private void button5_Click_1(object sender, EventArgs e)
{
this.TopMost = !this.TopMost;
((AntdUI.Button)sender).BackgroundImage= this.TopMost? Resources.ontop: Resources.ontop_blur;
}
private void button6_Click(object sender, EventArgs e)
{
AutoTask.TaskList tasklist = new AutoTask.TaskList();
tasklist.Show(this);
}
}
}