MessageboxNeo.cs
10.8 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BLL
{
public class MessageboxNeo:Form
{
private Panel contentPanel;
private Panel titlePanel;
private Label titleLabel;
private Button closeButton;
private PictureBox iconPictureBox;
private Label headerLabel;
private Label descriptionLabel;
private Button confirmButton;
private Button cancelButton;
// 回调
public bool Result { get; private set; }
// 定义颜色
private static readonly Color BackgroundColor = Color.FromArgb(15, 15, 15);
private static readonly Color TitleBarColor = Color.FromArgb(20, 20, 20);
private static readonly Color TextColor = Color.White;
private static readonly Color SubTextColor = Color.FromArgb(180, 180, 180);
private static readonly Color AccentColor = Color.FromArgb(255, 70, 70);
private static readonly Color ButtonBlueColor = Color.FromArgb(0, 120, 215);
private static readonly Color ButtonTextColor = Color.White;
private static readonly Color NeoScanBlue = Color.FromArgb(0, 174, 239);
private static readonly Color WarningYellow = Color.FromArgb(255, 217, 0);
public MessageboxNeo(string headerText, string descriptionText, string title = "NEO SCAN", bool showCancelButton = true)
{
InitializeComponent();
this.TopMost = true; // 添加这一行,使窗体总是在最前面
this.StartPosition = FormStartPosition.CenterScreen; // 改为CenterScreen以相对于整个屏幕居中
titleLabel.Text = title;
headerLabel.Text = headerText;
descriptionLabel.Text = descriptionText;
if (!showCancelButton)
{
cancelButton.Visible = false;
confirmButton.Left = (this.Width - confirmButton.Width) / 2;
}
CreateWarningIcon();
}
private void InitializeComponent()
{
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.Size = new Size(500, 350);
this.BackColor = BackgroundColor;
this.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular);
// 添加标题面板
titlePanel = new Panel
{
Dock = DockStyle.Top,
Height = 40,
BackColor = TitleBarColor
};
this.Controls.Add(titlePanel);
// NEO SCAN LOGO 标签
titleLabel = new Label
{
Text = "NEO SCAN",
ForeColor = NeoScanBlue,
Font = new Font("Arial", 14, FontStyle.Bold),
Location = new Point(15, 8),
AutoSize = true
};
titlePanel.Controls.Add(titleLabel);
// 关闭按钮
closeButton = new Button
{
Text = "×",
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0 },
Size = new Size(40, 40),
Location = new Point(titlePanel.Width - 40, 0),
TextAlign = ContentAlignment.MiddleCenter,
ForeColor = TextColor,
Font = new Font("Arial", 14),
Cursor = Cursors.Hand,
BackColor = TitleBarColor,
Anchor = AnchorStyles.Top | AnchorStyles.Right
};
closeButton.Click += (s, e) => this.Close();
titlePanel.Controls.Add(closeButton);
// 添加窗口拖动事件
titlePanel.MouseDown += (s, e) => {
if (e.Button == MouseButtons.Left)
{
const int WM_NCLBUTTONDOWN = 0xA1;
const int HT_CAPTION = 0x2;
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
};
// 内容面板
contentPanel = new Panel
{
Dock = DockStyle.Fill,
BackColor = BackgroundColor,
Padding = new Padding(20)
};
this.Controls.Add(contentPanel);
// 警告图标
iconPictureBox = new PictureBox
{
Size = new Size(80, 80),
SizeMode = PictureBoxSizeMode.StretchImage,
BackColor = Color.Transparent,
Location = new Point((this.Width - 80) / 2, 40)
};
contentPanel.Controls.Add(iconPictureBox);
// 错误标题文本
headerLabel = new Label
{
AutoSize = false,
TextAlign = ContentAlignment.MiddleCenter,
Size = new Size(400, 30),
Location = new Point((this.Width - 400) / 2, iconPictureBox.Bottom + 20),
Font = new Font("Microsoft YaHei UI", 14, FontStyle.Regular),
ForeColor = AccentColor
};
contentPanel.Controls.Add(headerLabel);
// 详细描述文本
descriptionLabel = new Label
{
AutoSize = false,
TextAlign = ContentAlignment.TopCenter,
Size = new Size(440, 80),
Location = new Point((this.Width - 440) / 2, headerLabel.Bottom + 10),
Font = new Font("Microsoft YaHei UI", 10, FontStyle.Regular),
ForeColor = SubTextColor
};
contentPanel.Controls.Add(descriptionLabel);
// 确认按钮
confirmButton = new Button
{
Text = "YES",
Size = new Size(130, 40),
Location = new Point((this.Width / 2) - 140, this.Height - 70),
ForeColor = ButtonTextColor,
FlatStyle = FlatStyle.Flat,
BackColor = ButtonBlueColor,
Font = new Font("Microsoft YaHei UI", 10F, FontStyle.Regular),
Cursor = Cursors.Hand
};
confirmButton.FlatAppearance.BorderSize = 0;
confirmButton.Click += (sender, e) =>
{
this.Result = true;
this.DialogResult = DialogResult.OK;
this.Close();
};
contentPanel.Controls.Add(confirmButton);
// 取消按钮
cancelButton = new Button
{
Text = "NO",
Size = new Size(130, 40),
Location = new Point((this.Width / 2) + 10, this.Height - 70),
ForeColor = TextColor,
FlatStyle = FlatStyle.Flat,
BackColor = Color.FromArgb(45, 45, 45),
Font = new Font("Microsoft YaHei UI", 10F, FontStyle.Regular),
Cursor = Cursors.Hand
};
cancelButton.FlatAppearance.BorderSize = 0;
cancelButton.Click += (sender, e) =>
{
this.Result = false;
this.DialogResult = DialogResult.Cancel;
this.Close();
};
contentPanel.Controls.Add(cancelButton);
// 窗体边框效果
this.Paint += (s, e) => {
using (Pen p = new Pen(Color.FromArgb(50, 50, 50)))
{
e.Graphics.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
}
};
}
// 创建黄色三角形警告图标
private void CreateWarningIcon()
{
int size = 80;
Bitmap iconBitmap = new Bitmap(size, size);
using (Graphics g = Graphics.FromImage(iconBitmap))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.Clear(Color.Transparent);
// 三角形坐标
PointF[] trianglePoints = new PointF[]
{
new PointF(size / 2, 5), // 顶点
new PointF(5, size - 10), // 左下角
new PointF(size - 5, size - 10) // 右下角
};
// 填充三角形(黄色)
using (SolidBrush brush = new SolidBrush(WarningYellow))
{
g.FillPolygon(brush, trianglePoints);
}
// 绘制三角形边框(深一点的颜色或黑色)
using (Pen pen = new Pen(Color.FromArgb(180, 160, 0), 2))
{
g.DrawPolygon(pen, trianglePoints);
}
// 绘制感叹号(黑色)
using (SolidBrush brush = new SolidBrush(Color.Black))
{
// 感叹号的点
g.FillEllipse(brush, size / 2 - 4, size - 25, 8, 8);
// 感叹号的竖线部分
using (GraphicsPath path = new GraphicsPath())
{
RectangleF rect = new RectangleF(size / 2 - 4f, size / 2 - 10, 8, 30);
path.AddRectangle(rect);
g.FillPath(brush, path);
}
}
}
iconPictureBox.Image = iconBitmap;
}
// 允许拖动窗口所需的外部方法
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
// 静态方法,方便调用
public static bool Show(IWin32Window owner, string headerText, string descriptionText, string title = "NEO SCAN", bool showCancelButton = true)
{
using (MessageboxNeo alertBox = new MessageboxNeo(headerText, descriptionText, title, showCancelButton))
{
alertBox.ShowDialog(owner);
return alertBox.Result;
}
}
public static bool Show( string headerText, string descriptionText, string title = "NEO SCAN", bool showCancelButton = true)
{
using (MessageboxNeo alertBox = new MessageboxNeo(headerText, descriptionText, title, showCancelButton))
{
alertBox.ShowDialog();
return alertBox.Result;
}
}
}
}