-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
154 lines (138 loc) · 5.77 KB
/
Program.cs
File metadata and controls
154 lines (138 loc) · 5.77 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
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace MinecraftConnectTool
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
public static Form1 MainForm { get; private set; }
public static Font AlertFont { get; } = new Font("Microsoft YaHei UI", 8.3f);
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
if (System.IO.File.Exists("debug"))
{
InitDebugConsole();
}
bool isElevated = args.Contains("--admin");
int admin = 0;
if (!isElevated)
{
// 尝试以管理员权限重新启动
var startInfo = new System.Diagnostics.ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Application.ExecutablePath,
Arguments = "--admin",
Verb = "runas" // 请求管理员权限
};
try
{
using (var proc = System.Diagnostics.Process.Start(startInfo))
{
return; // 启动新进程后退出当前进程
}
}
catch (System.ComponentModel.Win32Exception)
{
// 用户拒绝UAC,继续以普通权限运行
admin = 2;
}
}
else
{
admin = 1;
}
// 设置全局变量(你可以根据需要改为静态字段或其他方式)
Program.admin = admin;;
MainForm = new Form1();
Application.Run(MainForm);
}
// 全局方法
public static int admin = 0;
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
AntdUI.Modal.open(new AntdUI.Modal.Config(Program.MainForm, $"发生了一个意料之外的错误:\n{e.Exception.Message}", "详细信息:\n" + e.Exception.StackTrace, AntdUI.TType.Error)
{
CloseIcon = true,
Font = AlertFont,
Draggable = false,
CancelText = null,
OkText = "好的"
});
}
public static void alerterror(string message)
{
AntdUI.Modal.open(new AntdUI.Modal.Config(Program.MainForm, "Error\n发生了意料之外的错误", message, AntdUI.TType.Error)
{
CloseIcon = true,
Font = Program.AlertFont, // 可以根据需要调整字体
Draggable = false,
CancelText = null,
OkText = "知道了"
});
}
public static void alertwarn(string message)
{
AntdUI.Modal.open(new AntdUI.Modal.Config(Program.MainForm, "Warn\n警告(#°Д°)", message, AntdUI.TType.Warn)
{
CloseIcon = true,
Font = Program.AlertFont, // 可以根据需要调整字体
Draggable = false,
CancelText = null,
OkText = "知道了"
});
}
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
static extern bool AllocConsole();
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool SetConsoleOutputCP(uint wCodePageID);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool SetConsoleCP(uint wCodePageID);
static void InitDebugConsole()
{
AllocConsole();
SetConsoleOutputCP(65001);
SetConsoleCP(65001);
System.Console.OutputEncoding = System.Text.Encoding.UTF8;
// 接管输出流以实现自动着色
System.Console.SetOut(new ColorInterceptor(System.Console.Out));
System.Console.SetError(new ColorInterceptor(System.Console.Error));
}
class ColorInterceptor : System.IO.TextWriter
{
private System.IO.TextWriter _inner;
public ColorInterceptor(System.IO.TextWriter inner) => _inner = inner;
public override System.Text.Encoding Encoding => _inner.Encoding;
public override void Write(string value)
{
if (value == null) return;
var origin = System.Console.ForegroundColor;
// 无额外字符串分配,忽略大小写检测关键词
if (value.IndexOf("error", System.StringComparison.OrdinalIgnoreCase) >= 0)
System.Console.ForegroundColor = System.ConsoleColor.Red;
else if (value.IndexOf("warn", System.StringComparison.OrdinalIgnoreCase) >= 0)
System.Console.ForegroundColor = System.ConsoleColor.DarkYellow; // 橙色
else if (value.IndexOf("success", System.StringComparison.OrdinalIgnoreCase) >= 0)
System.Console.ForegroundColor = System.ConsoleColor.Green;
_inner.Write(value);
System.Console.ForegroundColor = origin;
}
public override void WriteLine(string value)
{
_inner.Write(DateTime.Now.ToString("[HH:mm:ss.fff] "));
Write(value);
_inner.WriteLine();
}
}
}
}