Skip to content

Commit b902bc2

Browse files
committed
feat: 开机自启
1 parent a7f82d2 commit b902bc2

9 files changed

Lines changed: 1827 additions & 57 deletions

File tree

MaiChartManager/Launcher.Designer.cs

Lines changed: 89 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MaiChartManager/Launcher.cs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System.Net;
22
using System.Net.Sockets;
33
using System.Text.Json;
4-
using System.Windows.Forms;
4+
using Windows.ApplicationModel;
5+
using Windows.ApplicationModel.Activation;
56
using Microsoft.AspNetCore.Hosting.Server;
67
using Microsoft.AspNetCore.Hosting.Server.Features;
78

@@ -15,13 +16,66 @@ public Launcher()
1516
label3.Text = $@"v{Application.ProductVersion}";
1617
checkBox1.Checked = StaticSettings.Config.Export;
1718
textBox1.Text = StaticSettings.Config.GamePath;
19+
CheckStartupStatus();
1820
# if DEBUG
1921
checkBox1.Checked = true;
2022
StaticSettings.Config.Export = true;
2123
textBox1.Text = @"D:\Maimai HDD\sdga145";
22-
button2_Click(null, null);
24+
StartClicked(null, null);
25+
notifyIcon1.Visible = true;
2326
WindowState = FormWindowState.Minimized;
2427
# endif
28+
if (!isFromStartup())
29+
{
30+
Visible = true;
31+
return;
32+
}
33+
34+
// 开机自启
35+
Visible = false;
36+
notifyIcon1.Visible = true;
37+
checkBox1.Checked = true;
38+
StaticSettings.Config.Export = true;
39+
StartClicked(null, null);
40+
}
41+
42+
private bool isFromStartup()
43+
{
44+
try
45+
{
46+
var aeArgs = AppInstance.GetActivatedEventArgs();
47+
return aeArgs?.Kind == ActivationKind.StartupTask;
48+
}
49+
catch (Exception e)
50+
{
51+
Console.WriteLine(e);
52+
SentrySdk.CaptureException(e);
53+
}
54+
55+
return false;
56+
}
57+
58+
private async Task CheckStartupStatus()
59+
{
60+
var startupTask = await StartupTask.GetAsync("MaiChartManagerStartupId");
61+
switch (startupTask.State)
62+
{
63+
case StartupTaskState.Disabled:
64+
checkBox_startup.Checked = false;
65+
break;
66+
case StartupTaskState.Enabled:
67+
checkBox_startup.Checked = true;
68+
break;
69+
case StartupTaskState.DisabledByUser:
70+
case StartupTaskState.DisabledByPolicy:
71+
checkBox_startup.Enabled = false;
72+
checkBox_startup.Checked = false;
73+
break;
74+
case StartupTaskState.EnabledByPolicy: // ??
75+
checkBox_startup.Enabled = false;
76+
checkBox_startup.Checked = true;
77+
break;
78+
}
2579
}
2680

2781
private void button1_Click(object sender, EventArgs e)
@@ -33,7 +87,7 @@ private void button1_Click(object sender, EventArgs e)
3387

3488
private string loopbackUrl;
3589

36-
private void button2_Click(object sender, EventArgs e)
90+
private void StartClicked(object sender, EventArgs e)
3791
{
3892
if (button2.Text == "停止")
3993
{
@@ -60,6 +114,12 @@ private void button2_Click(object sender, EventArgs e)
60114
return;
61115
}
62116

117+
if (!checkBox1.Checked && checkBox_startup.Checked)
118+
{
119+
checkBox_startup.Checked = false;
120+
checkBox_startup_Click(null, null);
121+
}
122+
63123
# if !DEBUG
64124
StaticSettings.Config.GamePath = textBox1.Text;
65125
File.WriteAllText(Path.Combine(StaticSettings.appData, "config.json"), JsonSerializer.Serialize(StaticSettings.Config));
@@ -119,5 +179,27 @@ private void Launcher_FormClosed(object sender, FormClosedEventArgs e)
119179
private void checkBox1_CheckedChanged(object sender, EventArgs e)
120180
{
121181
StaticSettings.Config.Export = checkBox1.Checked;
182+
checkBox_startup.Visible = checkBox1.Checked;
183+
}
184+
185+
private async void checkBox_startup_Click(object sender, EventArgs e)
186+
{
187+
await File.WriteAllTextAsync(Path.Combine(StaticSettings.appData, "config.json"), JsonSerializer.Serialize(StaticSettings.Config));
188+
var startupTask = await StartupTask.GetAsync("MaiChartManagerStartupId");
189+
if (checkBox_startup.Checked)
190+
{
191+
await startupTask.RequestEnableAsync();
192+
}
193+
else
194+
{
195+
startupTask.Disable();
196+
}
197+
}
198+
199+
private void notifyIcon1_Click(object sender, EventArgs e)
200+
{
201+
Visible = true;
202+
WindowState = FormWindowState.Normal;
203+
notifyIcon1.Visible = false;
122204
}
123205
}

0 commit comments

Comments
 (0)