-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
185 lines (170 loc) · 7.12 KB
/
Form1.cs
File metadata and controls
185 lines (170 loc) · 7.12 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
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace activate_windows_manager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
miscell();
unpackFile();
this.Resize += MyForm_Resize;
}
readonly string tips = "参数:\n-p: 预设参数(\"mac\"、\"bsd\"、\"linux\"、\"hurd\"、\"windows\"、\"unix\"、\"deck\"、\"reactos\"、\"m$\")\n-t: 自定义标题行\n-m: 自定义内容行\n-b: 加粗\n-i: 斜体\n-f: 自定义字体\n-c: 自定义颜色\n-w: 自定义叠加层宽度\n-h: 自定义叠加层高度\n-s: 自定义缩放\n";
static readonly string resourceName = "activate_windows";
static readonly string resourcePath = "activate_windows_manager.Resources.activate_windows.exe";
static string tempExePath = Path.Combine(Path.GetTempPath(), resourceName + ".exe");
private async void startClick_Click(object sender, EventArgs e)
{
await stableActivity("start", customArguments);
}
private async void stopClick_Click(object sender, EventArgs e)
{
await stableActivity("stop", customArguments);
}
private void helpButton_Click(object sender, EventArgs e)
{
toolTip.Show(tips, helpButton, 5000);
}
private void consoleButton_Click(object sender, EventArgs e)
{
new consoleForm().Show();
}
private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
ShowWindow(sender, e);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ExitApplication(sender, e);
}
private void ShowWindow(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
private void ExitApplication(object sender, EventArgs e)
{
try
{
stopClick_Click(sender, e);
System.Threading.Thread.Sleep(500);
if (File.Exists(tempExePath))
{
File.Delete(tempExePath);
}
Application.Exit();
}
catch (Exception ex)
{
// 捕获并处理异常
Console.WriteLine("Error: " + ex.Message);
MessageBox.Show("程序已退出,但临时文件未删除", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void MyForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
}
}
public void miscell()
{
ToolTip tooltip = new ToolTip()
{
OwnerDraw = true // 启用自定义绘制
};
toolTip.SetToolTip(customArguments, tips);
toolTip.SetToolTip(helpButton, tips);
toolTip.Draw += ToolTip_Draw;
// 初始化 NotifyIcon
NotifyIcon notifyIcon = new NotifyIcon
{
Icon = this.Icon, // 设置托盘图标(可以替换为自定义图标)
Text = "activate-windows", // 鼠标悬停时显示的提示文本
Visible = true
};
// 创建右键菜单
ContextMenuStrip contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("显示窗口", null, ShowWindow);
contextMenu.Items.Add("退出", null, ExitApplication);
notifyIcon.ContextMenuStrip = contextMenu;
// 添加双击事件
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
}
private void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
using (Font font = new Font("Consolas", 9, FontStyle.Bold)) // 自定义字体
{
// 设置背景颜色
e.Graphics.FillRectangle(Brushes.Azure, e.Bounds);
// 绘制文本
e.Graphics.DrawString(e.ToolTipText, font, Brushes.Black, e.Bounds);
}
}
static async void unpackFile()
{
try
{
// 获取当前程序的程序集
Assembly assembly = Assembly.GetExecutingAssembly();
// 创建临时文件路径
// 将嵌入的资源文件解压到临时目录
using (Stream resourceStream = assembly.GetManifestResourceStream(resourcePath))
{
if (resourceStream == null)
{
throw new Exception($"找不到嵌入的资源文件: {resourceName}");
}
using (FileStream fileStream = new FileStream(tempExePath, FileMode.Create, FileAccess.Write))
{
await resourceStream.CopyToAsync(fileStream); // 异步复制到临时文件
}
}
}
catch { }
}
static async Task stableActivity(string stable, System.Windows.Forms.TextBox customArguments)
{
try
{
// 创建 ProcessStartInfo 对象
ProcessStartInfo processInfo = new ProcessStartInfo
{
FileName = tempExePath, // 可执行文件路径
Arguments = stable == "start" ? customArguments.Text : "-K", // 传递给可执行文件的参数
RedirectStandardOutput = true, // 重定向标准输出(可选)
RedirectStandardError = true, // 重定向标准错误(可选)
CreateNoWindow = true, // 禁止创建窗口
UseShellExecute = false // 使用操作系统 shell 执行,设为 false 可隐藏窗口
};
// 启动进程
using (Process process = Process.Start(processInfo))
{
// 读取标准输出(如果需要查看执行结果)
string output = await process.StandardOutput.ReadToEndAsync();
string error = await process.StandardError.ReadToEndAsync();
// 等待进程结束
await Task.Run(() => process.WaitForExit());
// 输出结果(可选)
Console.WriteLine("Output: " + output);
Console.WriteLine("Error: " + error);
}
}
catch (Exception ex)
{
// 捕获并处理异常
Console.WriteLine("Error: " + ex.Message);
}
}
}
}