| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- using System;
- using System.Drawing;
- using System.Media;
- using System.Threading;
- using System.Windows.Forms;
- 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;
- string FileName = "";
- public RichTextAutoBottom()
- {
- InitializeComponent();
- thread = new Thread(PlaySound);
- TextChanged += RichTextBox_TextChange;
- }
- private void RichTextBox_TextChange(object sender, EventArgs e)
- {
- SelectionStart = Text.Length;
- ScrollToCaret();
- }
- /// <summary>
- /// color设置AppendText的颜色
- /// </summary>
- /// <param name="str"></param>
- /// <param name="color"></param>
- public void AppendText(string str, Color color)
- {
- SelectionColor = color;
- base.AppendText(str);
- //如果颜色是红色则进行提示音
- if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
- {
- if(str.Contains("缺料"))
- FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
- else
- FileName = Application.StartupPath + @"\Resources\Sound\5185.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- CustomMessageBox.Show(str, "异常提示");
- }
- else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
- {
- //颜色是绿色,进行正确提示音
- if (str.Contains("缺料"))
- FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
- else
- FileName = Application.StartupPath + @"\Resources\Sound\8378.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- }
- else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
- {
- //颜色是绿色,进行正确提示音
- FileName = Application.StartupPath + @"\Resources\Sound\4082.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- }
- else if (color == Color.LightGreen && Entity.SystemInf.CheckAudioEnable)
- {
- //颜色是绿色,进行正确提示音
- FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- }
- else if (color == Color.DarkRed && Entity.SystemInf.CheckAudioEnable)
- {
- FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- CustomMessageBox.Show(str, "异常提示");
- }
- LogManager.DoLog(FindForm().Tag + str);
- }
- /// <summary>
- /// 提示错误信息清楚指定控件的值
- /// </summary>
- /// <param name="str"></param>
- /// <param name="color"></param>
- /// <param name="ctl"></param>
- public void AppendText(string str, Color color, Control ctl)
- {
- SelectionColor = color;
- base.AppendText(str);
- ctl.Text = "";
- //如果颜色是红色则进行提示音
- if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
- {
- if (str.Contains("缺料"))
- FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
- else
- FileName = Application.StartupPath + @"\Resources\Sound\采集失败.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- CustomMessageBox.Show(str, "异常提示");
- }
- else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
- {
- //颜色是绿色,进行正确提示音
- if (str.Contains("缺料"))
- FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
- else
- FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- }
- else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
- {
- //颜色是绿色,进行正确提示音
- FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- }
- else if (color == Color.LightGreen && Entity.SystemInf.CheckAudioEnable)
- {
- //颜色是绿色,进行正确提示音
- FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- }
- else if (color == Color.DarkRed && Entity.SystemInf.CheckAudioEnable)
- {
- FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
- thread.Start();
- thread = new Thread(PlaySound);
- CustomMessageBox.Show(str, "异常提示");
- }
- LogManager.DoLog(FindForm().Tag + str);
- }
- private void PlaySound()
- {
- //要加载COM组件:Microsoft speech object Library
- if (!System.IO.File.Exists(FileName))
- {
- return;
- }
- try
- {
- SoundPlayer player = new SoundPlayer();
- player.SoundLocation = FileName;
- player.Load();
- player.Play();
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- Entity.SystemInf.CheckAudioEnable = false;
- }
- }
- }
- }
|