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();
}
///
/// color设置AppendText的颜色
///
///
///
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);
}
///
/// 提示错误信息清楚指定控件的值
///
///
///
///
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;
}
}
}
}