NeoMessageBox.cs 17.3 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmartScan.SetControl.WPF.Model
{
    public class NeoAlertBox : System.Windows.Forms.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;
        private AlertType  alertType;  
        // 回调
        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 SuccessColor = Color.FromArgb(46, 204, 113);
        private static readonly Color WarningColor = Color.FromArgb(241, 196, 15);
        public NeoAlertBox(string headerText, string descriptionText,AlertType alertType=AlertType.Warning, string title = "NEO SCAN", bool showCancelButton = true)
        {
            this.alertType = alertType;
            InitializeComponent();
            this.TopMost = true; // 添加这一行,使窗体总是在最前面
            titleLabel.Text = title;
            headerLabel.Text = headerText;
            descriptionLabel.Text = descriptionText;
            // 根据alertType设置headerLabel颜色
            switch (alertType)
            {
                case AlertType.Success:
                    headerLabel.ForeColor = SuccessColor;
                    break;
                case AlertType.Warning:
                    headerLabel.ForeColor = AccentColor;  // 使用原来定义的红色
                    break;
                case AlertType.Error:
                    headerLabel.ForeColor = AccentColor;
                    break;
                case AlertType.Info:
                    headerLabel.ForeColor = NeoScanBlue;
                    break;
            }
            this.StartPosition = FormStartPosition.CenterScreen; // 改为CenterScreen以相对于整个屏幕居中
            if (!showCancelButton)
            {
                cancelButton.Visible = false;
                confirmButton.Left = (this.Width - confirmButton.Width) / 2;

            }
            CreateIcon();
        }
        // 创建图标的方法需要修改
        private void CreateIcon()
        {
            switch (alertType)
            {
                case AlertType.Warning:
                    CreateWarningIcon();
                    break;
                case AlertType.Success:
                    CreateSuccessIcon();
                    break;
                case AlertType.Error:
                    CreateErrorIcon(); // 如果需要的话可以创建一个错误图标
                    break;
                case AlertType.Info:
                    CreateInfoIcon(); // 如果需要的话可以创建一个信息图标
                    break;
                default:
                    CreateWarningIcon();
                    break;
            }
        }
        // 创建警告图标(保留原来的实现)
        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(WarningColor))
                {
                    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;
        }

        // 添加创建成功图标(对号)的方法
        private void CreateSuccessIcon()
        {
            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);

                // 绘制圆形背景
                using (SolidBrush brush = new SolidBrush(SuccessColor))
                {
                    g.FillEllipse(brush, 5, 5, size - 10, size - 10);
                }

                // 绘制对号
                using (Pen pen = new Pen(Color.White, 6))
                {
                    pen.StartCap = LineCap.Round;
                    pen.EndCap = LineCap.Round;
                    pen.LineJoin = LineJoin.Round;

                    // 对号的两段线
                    g.DrawLine(pen,
                        new Point(size / 4, size / 2),
                        new Point(size / 2 - 5, size * 3 / 4));
                    g.DrawLine(pen,
                        new Point(size / 2 - 5, size * 3 / 4),
                        new Point(size * 3 / 4 + 5, size / 4));
                }
            }

            iconPictureBox.Image = iconBitmap;
        }

       

        private void CreateInfoIcon()
        {
            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);

                // 绘制蓝色圆形背景
                using (SolidBrush brush = new SolidBrush(NeoScanBlue))
                {
                    g.FillEllipse(brush, 5, 5, size - 10, size - 10);
                }

                // 绘制i的点
                using (SolidBrush brush = new SolidBrush(Color.White))
                {
                    g.FillEllipse(brush, size / 2 - 4, 20, 8, 8);
                }

                // 绘制i的竖线部分
                using (SolidBrush brush = new SolidBrush(Color.White))
                {
                    g.FillRectangle(brush, size / 2 - 4, 35, 8, 30);
                }
            }

            iconPictureBox.Image = iconBitmap;
        }
        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);
            this.TopMost = true; // 添加这一行,使窗体总是在最前面
            // 添加标题面板
            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("微软雅黑", 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("微软雅黑", 12, 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("微软雅黑", 12F, 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("微软雅黑", 12F, 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 CreateErrorIcon()
        {
            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(Color.Yellow))
                {
                    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, AlertType alertType = AlertType.Warning, string title = "NEO SCAN", bool showCancelButton = true)
        {
            using (NeoAlertBox alertBox = new NeoAlertBox(headerText, descriptionText, alertType, title, showCancelButton))
            {
                alertBox.ShowDialog(owner);
                return alertBox.Result;
            }
        }
        public static bool Show(string headerText, string descriptionText, AlertType alertType = AlertType.Warning, string title = "NEO SCAN", bool showCancelButton = true)
        {
            using (NeoAlertBox alertBox = new NeoAlertBox(headerText, descriptionText, alertType,title, showCancelButton))
            {
                alertBox.ShowDialog();
                return alertBox.Result;
            }
        }
        public enum AlertType
        {
            Warning,    // 警告图标(感叹号)
            Success,    // 成功图标(对号)
            Error,      // 错误图标(叉号,可选)
            Info        // 信息图标(i,可选)
        }
    }
}