-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadingForm.cs
More file actions
155 lines (140 loc) · 5.24 KB
/
LoadingForm.cs
File metadata and controls
155 lines (140 loc) · 5.24 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
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GlassLoader
{
public class LoadingForm
{
private static SynchronizationContext _syncContext;
public static Form GMLForm;
public const int LoadingItems = 10;
public static int LoadedItems = 0;
public static void LoadingFinished()
{
ManuallyClose = false;
_syncContext.Post(_ => GMLForm?.Close(), null);
//GMLForm?.Close();
}
public static bool ManuallyClose = true;
public static float m_LoadingProcess = 0.00f;
public static string m_LoadingData = "Initialize GML... ";
public static float LoadingProcess
{
get { return m_LoadingProcess; }
set
{
m_LoadingProcess = value;
GMLForm?.Invalidate();
}
}
public static string LoadingData
{
get { return m_LoadingData; }
set
{
m_LoadingData = value;
GMLForm?.Invalidate();
}
}
public static void UpdateLoadingData(string loadingData, int plus = 1)
{
m_LoadingData = loadingData;
LoadedItems += plus;
LoadingProcess = (float)LoadedItems / LoadingItems;
Thread.Sleep(10);
}
public static void CreateForm()
{
Application.SetCompatibleTextRenderingDefault(false);
// 创建一个新的同步上下文
var syncContext = new WindowsFormsSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(syncContext);
_syncContext = SynchronizationContext.Current;
GLog.Info("GML Window Initialize");
//Application.SetCompatibleTextRenderingDefault(false);
Application.EnableVisualStyles();
Icon icon = null;
try
{
icon = new Icon(FileManager.AbsoluteGMLIconPath);
}
catch (Exception ex)
{
GLog.Warn(ex);
}
Form form = new Form
{
Text = "Glass ModLoader [Preloading]",
Width = 600,
Height = 400,
Icon = icon,
MaximizeBox = false
};
GMLForm = form;
//PictureBox pictureBox = new PictureBox
//{
// ImageLocation = "GML\\Assets\\logo.png", // 图片路径
// SizeMode = PictureBoxSizeMode.Zoom, // 适应窗口大小
// Dock = DockStyle.Fill // 填充整个窗口
//};
//form.Controls.Add(pictureBox);
string relativePath = FileManager.AbsoluteGMLLogoPath;
string fullPath = Path.Combine(AppContext.BaseDirectory, relativePath);
//pictureBox.Image = Image.FromFile(fullPath);
Image img = null;
try
{
img = Image.FromFile(relativePath); // logo
}
catch (Exception ex)
{
GLog.Warn(ex);
}
form.Paint += (sender, e) =>
{
Graphics g = e.Graphics;
g.Clear(form.BackColor);
// 最近邻插值,避免模糊
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.PixelOffsetMode = PixelOffsetMode.None;
int w = form.ClientSize.Width;
int h = form.ClientSize.Height;
if (img != null)
{
int targetWidth = img.Width / 2;
int targetHeight = img.Height / 2;
int x = (w - targetWidth) / 2; // x居中
//int y = (form.ClientSize.Height - targetHeight) / 2;
g.DrawImage(img, new Rectangle(x, 50, targetWidth, targetHeight));
}
Pen bluepen = new Pen(Color.FromArgb(255, 52, 152, 219));
Pen graypen = new Pen(Color.FromArgb(255, 44, 62, 80));
Font f = new Font("微软雅黑", 10);
int left = (w - 400) / 2;
int top = h - 100;
int outline = 3;
g.DrawRectangle(bluepen, left, top, 400, 21);
g.FillRectangle(bluepen.Brush, left + outline, top + outline, 400 * LoadingProcess - outline * 2, 21 - outline * 2);
g.DrawString($"[{GUtil.GetTimeString()}] Loading: {LoadingData} {Math.Round(LoadingProcess * 100, 2)} %", f, graypen.Brush, left, top - 30);
};
// 当窗口大小变化时,强制重绘
form.Resize += (sender, e) => form.Invalidate();
form.FormClosing += (sender, e) =>
{
GMLForm?.Dispose();
GMLForm = null;
if (ManuallyClose)
{
GLog.Info("Exiting glass modloader...");
Environment.Exit(0);
}
};
Application.Run(form);
}
}
}