MessageboxNeo.cs 10.8 KB
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;
            }
        }
    }
}