MainForm.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using DirectShowLib;
  2. using Microsoft.Extensions.Logging.Console;
  3. using MySql.Data.MySqlClient;
  4. using SinMaiLauncher.ChildProcessHolder;
  5. using SinMaiLauncher.Interops;
  6. using SinMaiLauncher.Models;
  7. namespace SinMaiLauncher;
  8. public partial class MainForm : Form
  9. {
  10. private ILogger logger;
  11. private SettingReader setting;
  12. private Dictionary<ChildProcessKind, ChildProcessControlGroup> childProcessControlGroups;
  13. private readonly IEnumerator<string> txtAni = TextAnimation.LoadingTextSeq(8).GetEnumerator();
  14. private string mainControlStatusText = "摸鱼中";
  15. private bool mainControlStatusTextAnimation = false;
  16. private DateTime lastCheckSinMaiMainWinPos = DateTime.Now;
  17. private DateTime lastCheckSysVolume = DateTime.Now;
  18. private List<string> holdingExplorerWindowPaths = new();
  19. private CameraInterops.CameraCaptureWrap? cameraCapture;
  20. public MainForm()
  21. {
  22. InitializeComponent();
  23. }
  24. private WindowInterops.MoveWindowResult MoveSinMaiMainWindow()
  25. {
  26. var pb = (ChildProcessStateBagForWin)childProcessControlGroups[ChildProcessKind.SinMai].StateBag;
  27. //WS_CLIPSIBLINGS WS_POPUPWINDOW WS_VISIBLE
  28. var rc = setting.Settings.AutoRect;
  29. return WindowInterops.MoveWindow(pb.HWndMainWindow.Value, rc.Left, rc.Top, rc.Width, rc.Height);
  30. }
  31. private void ExplorerSaveAndKill()
  32. {
  33. holdingExplorerWindowPaths.Clear();
  34. var lst = ExplorerInterops.GetOpenWindows();
  35. logger.LogInformation($"explorer saved paths:{lst.Length}");
  36. holdingExplorerWindowPaths.AddRange(lst);
  37. ExplorerInterops.KillExplorer();
  38. }
  39. private async Task ExplorerRestore()
  40. {
  41. ExplorerInterops.RestoreExplorer();
  42. await Task.Delay(1000);
  43. ExplorerInterops.OpenWindow(holdingExplorerWindowPaths);
  44. }
  45. private async void MainForm_Shown(object sender, EventArgs e)
  46. {
  47. TopMost = false;
  48. TopMost = true;
  49. tcMain.SelectedTab = tpConsoles;
  50. ConsoleInterops.CurrentProcess.ConsoleAllocate();
  51. ConsoleInterops.CurrentProcess.ConsoleDisableQuickEdit();
  52. ConsoleInterops.CurrentProcess.ConsoleWindowSetParent(tpConsoleLauncher);
  53. ConsoleInterops.CurrentProcess.ConsoleWindowFill(tpConsoleLauncher);
  54. tpConsoleLauncher.Resize += delegate { ConsoleInterops.CurrentProcess.ConsoleWindowFill(tpConsoleLauncher); };
  55. //start host application
  56. var builder = Host.CreateDefaultBuilder();
  57. builder.ConfigureServices((ctx, services) =>
  58. {
  59. //控制台日志格式
  60. services.AddLogging(opt =>
  61. {
  62. opt.AddSimpleConsole(p =>
  63. {
  64. p.TimestampFormat = "[dd HH:mm:ss] ";
  65. p.SingleLine = true;
  66. p.ColorBehavior = LoggerColorBehavior.Enabled;
  67. })
  68. .AddDebug()
  69. //.AddProvider(loggingIntoEventBus)
  70. ;
  71. services.AddSingleton(opt);
  72. });
  73. services.AddSingleton(this);
  74. });
  75. var host = builder.Build();
  76. var services = host.Services;
  77. host.Start();
  78. logger = services.GetRequiredService<ILogger<MainForm>>();
  79. ExplorerInterops.logger = logger;
  80. var conf = services.GetRequiredService<IConfiguration>();
  81. var settingModel = new SinMaiLauncherSettingModel();
  82. conf.GetSection("SinMaiLauncher").Bind(settingModel);
  83. setting = new(settingModel);
  84. childProcessControlGroups = new Dictionary<ChildProcessKind, ChildProcessControlGroup>
  85. {
  86. {
  87. ChildProcessKind.MariaDb, new ChildProcessControlGroup
  88. {
  89. StateBag = new ProcessStateBagMariaDb(services.GetRequiredService<ILogger<ProcessStateBagMariaDb>>(), setting),
  90. ConsoleTab = tpConsoleMaria, PidLabel = lblPidMaria, StatusLabel = lblSubControlMaria,
  91. StartButton = btnSubControlMariaStart, StopButton = btnSubControlMariaStop
  92. }
  93. },
  94. {
  95. ChildProcessKind.AquaDx, new ChildProcessControlGroup
  96. {
  97. StateBag = new ProcessStateBagAquaDx(services.GetRequiredService<ILogger<ProcessStateBagAquaDx>>(), setting),
  98. Dep = ChildProcessKind.MariaDb,
  99. ConsoleTab = tpConsoleAquaDx, PidLabel = lblPidAquaDx, StatusLabel = lblSubControlAquaDx,
  100. StartButton = btnSubControlAquaDxStart, StopButton = btnSubControlAquaDxStop
  101. }
  102. },
  103. {
  104. ChildProcessKind.Injector, new ChildProcessControlGroup
  105. {
  106. StateBag = new ChildProcessStateBagForConsole(ChildProcessKind.Injector, setting.Injector),
  107. Dep = ChildProcessKind.AquaDx,
  108. ConsoleTab = tpConsoleInjector, PidLabel = lblPidInjector, StatusLabel = lblSubControlInjector,
  109. StartButton = btnSubControlInjectorStart, StopButton = btnSubControlInjectorStop
  110. }
  111. },
  112. {
  113. ChildProcessKind.SinMai, new ChildProcessControlGroup
  114. {
  115. StateBag = new ChildProcessStateBagForWin(ChildProcessKind.SinMai, setting.SinMai),
  116. Dep = ChildProcessKind.Injector,
  117. ConsoleTab = null, PidLabel = lblPidSinMai, StatusLabel = lblSubControlSinMai,
  118. StartButton = btnSubControlSinMaiStart, StopButton = btnSubControlSinMaiStop
  119. }
  120. }
  121. };
  122. foreach (var kvp in childProcessControlGroups)
  123. {
  124. var kind = kvp.Key;
  125. var gr = kvp.Value;
  126. var tab = gr.ConsoleTab;
  127. var cpb = gr.StateBag;
  128. var btnStart = gr.StartButton;
  129. var btnStop = gr.StopButton;
  130. var lblPid = gr.PidLabel;
  131. var lblStatus = gr.StatusLabel;
  132. //绑定tab控制台窗口填充
  133. if (cpb is ChildProcessStateBagForConsole con)
  134. {
  135. tab.Resize += delegate
  136. {
  137. if (con.HWndConsole.HasValue) WindowInterops.FillWindow(con.HWndConsole.Value, tab);
  138. };
  139. }
  140. btnStart.Click += async delegate
  141. {
  142. if (cpb.IsAlive)
  143. {
  144. MessageBox.Show("它在运行了,别重复点启动啊");
  145. return;
  146. }
  147. if (gr.Dep.HasValue && childProcessControlGroups[gr.Dep.Value].StateBag.Status != ChildProcessStatus.Ready)
  148. {
  149. MessageBox.Show("依赖的进程未就绪,启动不了的,等就绪了再启动。要按顺序启动。");
  150. return;
  151. }
  152. await cpb.StartAsync();
  153. };
  154. btnStop.Click += async delegate
  155. {
  156. if (cpb.IsAlive == false)
  157. {
  158. MessageBox.Show("都没启动,咋停止啊?");
  159. return;
  160. }
  161. await cpb.StopAsync(TimeSpan.FromSeconds(5));
  162. };
  163. cpb.StatusUpdated += async delegate
  164. {
  165. void UpdateUi()
  166. {
  167. lblPid.Text = cpb.Pid.HasValue ? cpb.Pid.Value.ToString() : "-";
  168. lblStatus.Text = cpb.Status switch
  169. {
  170. ChildProcessStatus.NoLaunched => "未启动",
  171. ChildProcessStatus.PreLaunching => "准备启动",
  172. ChildProcessStatus.MissingWorkingDir => "丢失工作目录",
  173. ChildProcessStatus.MissingExeFile => "丢失exe文件",
  174. ChildProcessStatus.Launching => "正在启动",
  175. ChildProcessStatus.WaitingReady => "等待就绪...",
  176. ChildProcessStatus.Ready => "就绪",
  177. ChildProcessStatus.Stopping => "正在停止",
  178. ChildProcessStatus.Stopped => "已停止",
  179. _ => "?"
  180. };
  181. if (cpb.Status == ChildProcessStatus.WaitingReady && cpb is ChildProcessStateBagForConsole con && cpb.IsAlive && cpb.Pid.HasValue && con.HWndConsole.HasValue)
  182. {
  183. if (tab != null)
  184. {
  185. //console 收纳
  186. Program.DisableQuickEdit(cpb.Pid.Value);
  187. WindowInterops.RemoveBorderVisible(con.HWndConsole.Value);
  188. WindowInterops.SetParent(con.HWndConsole.Value, tab.Handle);
  189. WindowInterops.FillWindow(con.HWndConsole.Value, tab);
  190. Height++;
  191. Height--;
  192. }
  193. }
  194. if (chkExplorerAuto.Checked)
  195. {
  196. if (kind == ChildProcessKind.SinMai)
  197. {
  198. if (cpb.Status == ChildProcessStatus.Ready)
  199. {
  200. ExplorerSaveAndKill();
  201. }
  202. if (cpb.Status == ChildProcessStatus.Stopped)
  203. {
  204. logger.LogInformation("SinMai Exit");
  205. _ = ExplorerRestore();
  206. }
  207. }
  208. }
  209. }
  210. if (InvokeRequired) await InvokeAsync(UpdateUi);
  211. else UpdateUi();
  212. };
  213. }
  214. logger.LogInformation("Started");
  215. Application.DoEvents();
  216. await Task.Delay(1000);
  217. tcMain.Enabled = true;
  218. try
  219. {
  220. var cams = CameraInterops.GetAllCamera();
  221. cboCameraDevices.Items.Clear();
  222. foreach (var cam in cams) cboCameraDevices.Items.Add(cam);
  223. if (cboCameraDevices.SelectedItem == null && cams.Length > 0) cboCameraDevices.SelectedIndex = 0;
  224. }
  225. catch (Exception exception)
  226. {
  227. logger.LogError(exception, "搜索摄像头");
  228. }
  229. //#if DEBUG
  230. // tcMain.SelectedTab = tpSubControls;
  231. //#else
  232. tcMain.SelectedTab = tpMainControl;
  233. //#endif
  234. }
  235. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  236. {
  237. if (
  238. childProcessControlGroups[ChildProcessKind.MariaDb].StateBag.Status == ChildProcessStatus.Ready
  239. || childProcessControlGroups[ChildProcessKind.AquaDx].StateBag.Status == ChildProcessStatus.Ready
  240. )
  241. {
  242. e.Cancel = MessageBox.Show($"基础设施进程仍在运行。{Environment.NewLine}为了避免进度数据裂开,请确保安全停止再关闭。{Environment.NewLine}要强制关闭吗?", "关闭启动器", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No;
  243. }
  244. }
  245. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  246. {
  247. //Kill all child process
  248. childProcessControlGroups[ChildProcessKind.SinMai].StateBag.StopAsync(TimeSpan.FromSeconds(10)).Wait(TimeSpan.FromSeconds(10));
  249. childProcessControlGroups[ChildProcessKind.Injector].StateBag.StopAsync(TimeSpan.FromSeconds(10)).Wait(TimeSpan.FromSeconds(10));
  250. childProcessControlGroups[ChildProcessKind.AquaDx].StateBag.StopAsync(TimeSpan.FromSeconds(10)).Wait(TimeSpan.FromSeconds(10));
  251. childProcessControlGroups[ChildProcessKind.MariaDb].StateBag.StopAsync(TimeSpan.FromSeconds(10)).Wait(TimeSpan.FromSeconds(10));
  252. cameraCapture?.Dispose();
  253. }
  254. private void tcConsoles_SelectedIndexChanged(object sender, EventArgs e)
  255. {
  256. Height++;
  257. Height--;
  258. }
  259. private async void btnMainControlStart_Click(object sender, EventArgs e)
  260. {
  261. btnMainControlStart.Enabled = false;
  262. mainControlStatusTextAnimation = true;
  263. var orderedProcesses = new List<(ChildProcessKind kind, TimeSpan timeout)>
  264. {
  265. (ChildProcessKind.MariaDb, TimeSpan.FromSeconds(30)),
  266. (ChildProcessKind.AquaDx,TimeSpan.FromSeconds(90)),
  267. (ChildProcessKind.Injector,TimeSpan.FromSeconds(30)),
  268. (ChildProcessKind.SinMai,TimeSpan.FromSeconds(30)),
  269. };
  270. var startedProcesses = new List<ChildProcessKind>();
  271. try
  272. {
  273. foreach (var (kind, timeout) in orderedProcesses)
  274. {
  275. var gr = childProcessControlGroups[kind];
  276. var cpb = gr.StateBag;
  277. mainControlStatusText = $"正在启动 {kind}";
  278. if (cpb.IsAlive)
  279. {
  280. logger.LogWarning("{Kind} is already running, skipping.", kind);
  281. startedProcesses.Add(kind); // 已运行的也记录,便于回滚
  282. continue;
  283. }
  284. await cpb.StartAsync();
  285. await WaitForReadyAsync(cpb, timeout);
  286. if (cpb.Status != ChildProcessStatus.Ready)
  287. {
  288. logger.LogError($"{kind} failed to reach Ready state (current: {cpb.Status}), rolling back.");
  289. await RollbackAsync(startedProcesses);
  290. return;
  291. }
  292. startedProcesses.Add(kind);
  293. }
  294. mainControlStatusText = $"已启动!";
  295. btnMainControlStop.Enabled = true;
  296. mainControlStatusTextAnimation = false;
  297. }
  298. catch (Exception exception)
  299. {
  300. logger.LogError(exception, "Error during startup, rolling back.");
  301. await RollbackAsync(startedProcesses);
  302. }
  303. async Task WaitForReadyAsync(ChildProcessStateBag cpb, TimeSpan timeout)
  304. {
  305. if (cpb.Status == ChildProcessStatus.Ready
  306. || cpb.Status == ChildProcessStatus.MissingWorkingDir
  307. || cpb.Status == ChildProcessStatus.MissingExeFile
  308. || cpb.Status == ChildProcessStatus.Stopped) return;
  309. var tcs = new TaskCompletionSource<bool>();
  310. EventHandler<EventArgs> handler = null;
  311. handler = delegate
  312. {
  313. if (cpb.Status == ChildProcessStatus.Ready
  314. || cpb.Status == ChildProcessStatus.MissingWorkingDir
  315. || cpb.Status == ChildProcessStatus.MissingExeFile
  316. || cpb.Status == ChildProcessStatus.Stopped
  317. )
  318. {
  319. cpb.StatusUpdated -= handler;
  320. tcs.TrySetResult(true);
  321. }
  322. };
  323. cpb.StatusUpdated += handler;
  324. try
  325. {
  326. // 等待 Ready 或超时
  327. await Task.WhenAny(tcs.Task, Task.Delay(timeout));
  328. if (!tcs.Task.IsCompleted)
  329. {
  330. cpb.StatusUpdated -= handler;
  331. throw new TimeoutException($"{cpb.Kind} did not reach Ready state within {timeout.TotalSeconds} seconds.");
  332. }
  333. }
  334. finally
  335. {
  336. cpb.StatusUpdated -= handler; // 确保清理
  337. }
  338. }
  339. async Task RollbackAsync(List<ChildProcessKind> startedProcesses)
  340. {
  341. // 按相反顺序停止
  342. foreach (var kind in startedProcesses.AsEnumerable().Reverse())
  343. {
  344. mainControlStatusText = $"正在停止 {kind}";
  345. var group = childProcessControlGroups[kind];
  346. var cpb = group.StateBag;
  347. if (cpb.IsAlive)
  348. {
  349. logger.LogInformation("Stopping {Kind} during rollback...", kind);
  350. await InvokeAsync(() => group.StatusLabel.Text = "正在停止...");
  351. await cpb.StopAsync(TimeSpan.FromSeconds(5));
  352. }
  353. }
  354. mainControlStatusText = "启动失败,已回滚到启动之前";
  355. btnMainControlStart.Enabled = true;
  356. mainControlStatusTextAnimation = false;
  357. }
  358. }
  359. private async void btnMainControlStop_Click(object sender, EventArgs e)
  360. {
  361. var orderedProcesses = new List<(ChildProcessKind kind, TimeSpan timeout)>
  362. {
  363. (ChildProcessKind.SinMai,TimeSpan.FromSeconds(5)),
  364. (ChildProcessKind.Injector,TimeSpan.FromSeconds(10)),
  365. (ChildProcessKind.AquaDx,TimeSpan.FromSeconds(90)),
  366. (ChildProcessKind.MariaDb, TimeSpan.FromSeconds(30)),
  367. };
  368. foreach (var (kind, timeout) in orderedProcesses)
  369. {
  370. mainControlStatusText = $"正在停止 {kind}";
  371. var gr = childProcessControlGroups[kind];
  372. if (gr.StateBag.IsAlive)
  373. {
  374. await gr.StateBag.StopAsync(timeout);
  375. }
  376. }
  377. btnMainControlStart.Enabled = true;
  378. btnMainControlStop.Enabled = false;
  379. mainControlStatusText = $"已停止";
  380. mainControlStatusTextAnimation = false;
  381. }
  382. private void tmrMain_Tick(object sender, EventArgs e)
  383. {
  384. if (mainControlStatusTextAnimation)
  385. {
  386. txtAni.MoveNext();
  387. lblMainControlStatus.Text = $"{mainControlStatusText} {txtAni.Current}";
  388. }
  389. else
  390. {
  391. lblMainControlStatus.Text = $"{mainControlStatusText}";
  392. }
  393. //窗口调整
  394. if (chkWinTrickAuto.Checked && lastCheckSinMaiMainWinPos < DateTime.Now.AddSeconds(-5))
  395. {
  396. lastCheckSinMaiMainWinPos = DateTime.Now;
  397. var pb = (ChildProcessStateBagForWin)childProcessControlGroups[ChildProcessKind.SinMai].StateBag;
  398. if (pb.Status == ChildProcessStatus.Ready && pb.HWndMainWindow.HasValue)
  399. {
  400. MoveSinMaiMainWindow();
  401. }
  402. }
  403. //显示系统音量
  404. if (lastCheckSysVolume < DateTime.Now.AddSeconds(-1))
  405. {
  406. lastCheckSinMaiMainWinPos = DateTime.Now;
  407. var sv = SystemAudioVolumeInterop.GetVolume();
  408. lblSysVolume.Text = sv.HasValue ? $"{sv:N2}%" : "-";
  409. }
  410. }
  411. private void btnAimeWrite_Click(object sender, EventArgs e)
  412. {
  413. var input = cboAimeCard.Text;
  414. if (string.IsNullOrEmpty(input))
  415. {
  416. MessageBox.Show("倒是输入aime卡号啊?");
  417. return;
  418. }
  419. if (Directory.Exists(setting.AimeFleSinMaiDir) == false)
  420. {
  421. MessageBox.Show("找不到写aime卡文件的目录");
  422. return;
  423. }
  424. if (Directory.Exists(setting.AimeFleSinMaiPath))
  425. {
  426. MessageBox.Show("找不到写aime卡的文件");
  427. return;
  428. }
  429. var aime = input.Replace("-", "");
  430. File.WriteAllText(setting.AimeFleSinMaiPath, aime);
  431. MessageBox.Show($"已将选定的卡号已写入 aime.txt !{Environment.NewLine}在游戏登录时按回车键读卡{Environment.NewLine}(按一次回车不行就多按几次回车键直到读卡成功🙄)");
  432. }
  433. private void btnWinTrick_Click(object sender, EventArgs e)
  434. {
  435. var pb = (ChildProcessStateBagForWin)childProcessControlGroups[ChildProcessKind.SinMai].StateBag;
  436. if (pb.Status != ChildProcessStatus.Ready || !pb.HWndMainWindow.HasValue)
  437. {
  438. MessageBox.Show("游戏启动之后再点这个按钮");
  439. return;
  440. }
  441. var moveWindowResult = MoveSinMaiMainWindow();
  442. switch (moveWindowResult)
  443. {
  444. case WindowInterops.MoveWindowResult.FailGet:
  445. MessageBox.Show("操作不成功:读取失败");
  446. break;
  447. case WindowInterops.MoveWindowResult.FailSet:
  448. MessageBox.Show("操作不成功:设置失败");
  449. break;
  450. case WindowInterops.MoveWindowResult.Success:
  451. MessageBox.Show("操作成功");
  452. break;
  453. case WindowInterops.MoveWindowResult.NoChange:
  454. MessageBox.Show("没有改变");
  455. break;
  456. }
  457. }
  458. private void btnAimeRefresh_Click(object sender, EventArgs e)
  459. {
  460. if (childProcessControlGroups[ChildProcessKind.MariaDb].StateBag.Status != ChildProcessStatus.Ready)
  461. {
  462. MessageBox.Show("启动数据库先啊!");
  463. return;
  464. }
  465. var conf = setting.Settings.Infra.MariaDb;
  466. var connectionString = $"Server={conf.Host};Port={conf.Port};Uid={conf.Usr};Pwd={conf.Pwd};";
  467. using var conn = new MySqlConnection(connectionString);
  468. conn.Open();
  469. using var cmd = conn.CreateCommand();
  470. cmd.CommandText = "SELECT `luid` FROM `aqua`.`sega_card`";
  471. using var dr = cmd.ExecuteReader();
  472. cboAimeCard.Items.Clear();
  473. while (dr.Read())
  474. {
  475. var aimeR = dr["luid"].ToString();
  476. var aimeH = string.Join("-",
  477. aimeR.Substring(0, 4),
  478. aimeR.Substring(4, 4),
  479. aimeR.Substring(8, 4),
  480. aimeR.Substring(12, 4),
  481. aimeR.Substring(16, 4));
  482. cboAimeCard.Items.Add(aimeH);
  483. }
  484. MessageBox.Show($"从数据库中读取到 {cboAimeCard.Items.Count} 个卡号已载入下拉列表");
  485. }
  486. private void btnExplorerKill_Click(object sender, EventArgs e)
  487. {
  488. ExplorerSaveAndKill();
  489. }
  490. private async void btnExplorerRestore_Click(object sender, EventArgs e)
  491. {
  492. await ExplorerRestore();
  493. }
  494. private void btnProcessLoopBackVirtualNic_Click(object sender, EventArgs e)
  495. {
  496. MessageBox.Show("还没做!");
  497. }
  498. private void btnAppDataOverwrite_Click(object sender, EventArgs e)
  499. {
  500. MessageBox.Show("还没做!");
  501. }
  502. private void btnSysVolumeDec_Click(object sender, EventArgs e)
  503. {
  504. var sv = SystemAudioVolumeInterop.GetVolume();
  505. if (!sv.HasValue)
  506. {
  507. MessageBox.Show("无法获取当前音量");
  508. return;
  509. }
  510. sv -= 5;
  511. if (sv < 0) sv = 0;
  512. if (SystemAudioVolumeInterop.SetVolume(sv.Value) == false) MessageBox.Show("音量调整失败");
  513. }
  514. private void btnSysVolumeInc_Click(object sender, EventArgs e)
  515. {
  516. var sv = SystemAudioVolumeInterop.GetVolume();
  517. if (!sv.HasValue)
  518. {
  519. MessageBox.Show("无法获取当前音量");
  520. return;
  521. }
  522. sv += 5;
  523. if (sv > 100) sv = 100;
  524. if (SystemAudioVolumeInterop.SetVolume(sv.Value) == false) MessageBox.Show("音量调整失败");
  525. }
  526. private class ChildProcessControlGroup
  527. {
  528. public ChildProcessStateBag StateBag { get; set; }
  529. public ChildProcessKind? Dep { get; set; }
  530. public TabPage? ConsoleTab { get; set; } // 可空,窗口进程为 null
  531. public Label PidLabel { get; set; }
  532. public Label StatusLabel { get; set; }
  533. public Button StartButton { get; set; }
  534. public Button StopButton { get; set; }
  535. }
  536. private void btnCameraList_Click(object sender, EventArgs e)
  537. {
  538. var cams = CameraInterops.GetAllCamera();
  539. if (cams.Length == 0) MessageBox.Show("笑死,一个摄像头都没找到!");
  540. cboCameraDevices.Items.Clear();
  541. foreach (var cam in cams) cboCameraDevices.Items.Add(cam);
  542. if (cboCameraDevices.SelectedItem == null && cams.Length > 0) cboCameraDevices.SelectedIndex = 0;
  543. }
  544. private void btnCameraOpen_Click(object sender, EventArgs e)
  545. {
  546. if (cboCameraDevices.SelectedItem == null)
  547. {
  548. MessageBox.Show("还没选择要打开的摄像头!");
  549. return;
  550. }
  551. cameraCapture?.Dispose();
  552. cameraCapture = new CameraInterops.CameraCaptureWrap((DsDevice)cboCameraDevices.SelectedItem, picBoxCamera);
  553. cboCameraFormats.Items.Clear();
  554. foreach (var format in cameraCapture.SupportedFormats) cboCameraFormats.Items.Add(format);
  555. var bestFormat = cameraCapture.FindBestFormat(450, 390, 60);
  556. if (bestFormat != null) cboCameraFormats.SelectedItem = bestFormat;
  557. }
  558. private void btnCameraClose_Click(object sender, EventArgs e)
  559. {
  560. cameraCapture?.Dispose();
  561. cameraCapture = null;
  562. }
  563. private void cboCameraFormats_SelectedIndexChanged(object sender, EventArgs e)
  564. {
  565. if (cboCameraFormats.SelectedItem != null)
  566. cameraCapture?.ApplySelectedFormat((CameraInterops.CameraCaptureWrap.ICameraFormat)cboCameraFormats.SelectedItem);
  567. }
  568. }