Przeglądaj źródła

修改弹窗样式

yhluo 2 miesięcy temu
rodzic
commit
445485c767

+ 152 - 4
UAS_MES_YTDZ/CustomControl/RichText/RichTextAutoBottom.cs

@@ -7,6 +7,154 @@ using UAS_MES_NEW.PublicMethod;
 
 namespace UAS_MES_NEW.CustomControl.RichText
 {
+    public class CustomMessageBox : Form
+    {
+        private bool _isClosing = false;
+
+        public CustomMessageBox(string message, string title)
+        {
+            try
+            {
+                if (string.IsNullOrEmpty(message)) message = "未知错误";
+                if (string.IsNullOrEmpty(title)) title = "提示";
+
+                // 设置窗体的Tag为"ShowDialogWindow",避免权限校验
+                this.Tag = "ShowDialogWindow";
+
+                this.FormBorderStyle = FormBorderStyle.None;
+                this.StartPosition = FormStartPosition.CenterParent;
+                this.ShowInTaskbar = false;
+                this.MaximizeBox = false;
+                this.MinimizeBox = false;
+                this.Size = new Size(500, 300);
+                this.BackColor = Color.White;
+                this.TopMost = true;
+
+                // 自定义标题栏
+                Panel titlePanel = new Panel
+                {
+                    Size = new Size(500, 50),
+                    Location = new Point(0, 0),
+                    BackColor = Color.FromArgb(0, 122, 204)
+                };
+
+                Label lblTitle = new Label
+                {
+                    Text = title,
+                    AutoSize = false,
+                    Size = new Size(400, 50),
+                    Location = new Point(20, 0),
+                    Font = new Font("微软雅黑", 18, FontStyle.Bold),
+                    ForeColor = Color.White,
+                    TextAlign = ContentAlignment.MiddleLeft
+                };
+
+                Button btnClose = new Button
+                {
+                    Text = "✕",
+                    Size = new Size(50, 50),
+                    Location = new Point(450, 0),
+                    FlatStyle = FlatStyle.Flat,
+                    Font = new Font("微软雅黑", 16, FontStyle.Bold),
+                    ForeColor = Color.White,
+                    BackColor = Color.Transparent
+                };
+                btnClose.FlatAppearance.BorderSize = 0;
+                btnClose.FlatAppearance.MouseOverBackColor = Color.FromArgb(192, 0, 0);
+                btnClose.Click += (sender, e) =>
+                {
+                    this.DialogResult = DialogResult.Cancel;
+                    SafeClose();
+                };
+
+                titlePanel.Controls.Add(lblTitle);
+                titlePanel.Controls.Add(btnClose);
+
+                Label lblMessage = new Label
+                {
+                    Text = message,
+                    AutoSize = false,
+                    Size = new Size(450, 150),
+                    Location = new Point(25, 70),
+                    Font = new Font("微软雅黑", 14, FontStyle.Regular),
+                    ForeColor = Color.Red,
+                    TextAlign = ContentAlignment.MiddleCenter
+                };
+
+                Button btnOK = new Button
+                {
+                    Text = "确定",
+                    Size = new Size(120, 40),
+                    Location = new Point(190, 240),
+                    BackColor = Color.FromArgb(0, 122, 204),
+                    ForeColor = Color.White,
+                    FlatStyle = FlatStyle.Flat,
+                    Font = new Font("微软雅黑", 11, FontStyle.Regular)
+                };
+                btnOK.Click += (sender, e) =>
+                {
+                    this.DialogResult = DialogResult.OK;
+                    SafeClose();
+                };
+
+                this.Controls.Add(titlePanel);
+                this.Controls.Add(lblMessage);
+                this.Controls.Add(btnOK);
+
+                // 窗口拖动支持
+                titlePanel.MouseDown += (sender, e) =>
+                {
+                    if (e.Button == MouseButtons.Left && !_isClosing)
+                    {
+                        ReleaseCapture();
+                        SendMessage(this.Handle, 0xA1, 0x2, 0);
+                    }
+                };
+
+                this.FormClosing += (s, e) =>
+                {
+                    _isClosing = true;
+                };
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show($"消息框创建失败: {ex.Message}", "错误");
+            }
+        }
+
+        private void SafeClose()
+        {
+            if (!_isClosing)
+            {
+                _isClosing = true;
+                this.Close();
+            }
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing)
+            {
+                _isClosing = true;
+            }
+            base.Dispose(disposing);
+        }
+
+        [System.Runtime.InteropServices.DllImport("user32.dll")]
+        public static extern bool ReleaseCapture();
+
+        [System.Runtime.InteropServices.DllImport("user32.dll")]
+        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
+
+        public static DialogResult Show(string message, string title)
+        {
+            using (CustomMessageBox msgBox = new CustomMessageBox(message, title))
+            {
+                return msgBox.ShowDialog();
+            }
+        }
+    }
+
     public partial class RichTextAutoBottom : RichTextBox
     {
         Thread thread;
@@ -42,7 +190,7 @@ namespace UAS_MES_NEW.CustomControl.RichText
                 FileName = Application.StartupPath + @"\Resources\Sound\5185.wav";
                 thread.Start();
                 thread = new Thread(PlaySound);
-                MessageBox.Show(str, "异常提示");
+                CustomMessageBox.Show(str, "异常提示");
             }
             else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
             {
@@ -73,7 +221,7 @@ namespace UAS_MES_NEW.CustomControl.RichText
                 FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
                 thread.Start();
                 thread = new Thread(PlaySound);
-                MessageBox.Show(str, "异常提示");
+                CustomMessageBox.Show(str, "异常提示");
             }
             LogManager.DoLog(FindForm().Tag + str);
         }
@@ -98,7 +246,7 @@ namespace UAS_MES_NEW.CustomControl.RichText
                     FileName = Application.StartupPath + @"\Resources\Sound\采集失败.wav";
                 thread.Start();
                 thread = new Thread(PlaySound);
-                MessageBox.Show(str, "异常提示");
+                CustomMessageBox.Show(str, "异常提示");
             }
             else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
             {
@@ -129,7 +277,7 @@ namespace UAS_MES_NEW.CustomControl.RichText
                 FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
                 thread.Start();
                 thread = new Thread(PlaySound);
-                MessageBox.Show(str, "异常提示");
+                CustomMessageBox.Show(str, "异常提示");
             }
             LogManager.DoLog(FindForm().Tag + str);
         }

+ 1 - 1
UAS_MES_YTDZ/UAS_MES_YTDZ.csproj

@@ -368,7 +368,7 @@
       <DependentUpon>ProcessBar.cs</DependentUpon>
     </Compile>
     <Compile Include="CustomControl\RichText\RichTextAutoBottom.cs">
-      <SubType>Component</SubType>
+      <SubType>Form</SubType>
     </Compile>
     <Compile Include="CustomControl\RichText\RichTextAutoBottom.Designer.cs">
       <DependentUpon>RichTextAutoBottom.cs</DependentUpon>