RichTextAutoBottom.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Drawing;
  3. using System.Media;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using UAS_MES_NEW.PublicMethod;
  7. namespace UAS_MES_NEW.CustomControl.RichText
  8. {
  9. public class CustomMessageBox : Form
  10. {
  11. private bool _isClosing = false;
  12. public CustomMessageBox(string message, string title)
  13. {
  14. try
  15. {
  16. if (string.IsNullOrEmpty(message)) message = "未知错误";
  17. if (string.IsNullOrEmpty(title)) title = "提示";
  18. // 设置窗体的Tag为"ShowDialogWindow",避免权限校验
  19. this.Tag = "ShowDialogWindow";
  20. this.FormBorderStyle = FormBorderStyle.None;
  21. this.StartPosition = FormStartPosition.CenterParent;
  22. this.ShowInTaskbar = false;
  23. this.MaximizeBox = false;
  24. this.MinimizeBox = false;
  25. this.Size = new Size(500, 300);
  26. this.BackColor = Color.White;
  27. this.TopMost = true;
  28. // 自定义标题栏
  29. Panel titlePanel = new Panel
  30. {
  31. Size = new Size(500, 50),
  32. Location = new Point(0, 0),
  33. BackColor = Color.FromArgb(0, 122, 204)
  34. };
  35. Label lblTitle = new Label
  36. {
  37. Text = title,
  38. AutoSize = false,
  39. Size = new Size(400, 50),
  40. Location = new Point(20, 0),
  41. Font = new Font("微软雅黑", 18, FontStyle.Bold),
  42. ForeColor = Color.White,
  43. TextAlign = ContentAlignment.MiddleLeft
  44. };
  45. Button btnClose = new Button
  46. {
  47. Text = "✕",
  48. Size = new Size(50, 50),
  49. Location = new Point(450, 0),
  50. FlatStyle = FlatStyle.Flat,
  51. Font = new Font("微软雅黑", 16, FontStyle.Bold),
  52. ForeColor = Color.White,
  53. BackColor = Color.Transparent
  54. };
  55. btnClose.FlatAppearance.BorderSize = 0;
  56. btnClose.FlatAppearance.MouseOverBackColor = Color.FromArgb(192, 0, 0);
  57. btnClose.Click += (sender, e) =>
  58. {
  59. this.DialogResult = DialogResult.Cancel;
  60. SafeClose();
  61. };
  62. titlePanel.Controls.Add(lblTitle);
  63. titlePanel.Controls.Add(btnClose);
  64. Label lblMessage = new Label
  65. {
  66. Text = message,
  67. AutoSize = false,
  68. Size = new Size(450, 150),
  69. Location = new Point(25, 70),
  70. Font = new Font("微软雅黑", 14, FontStyle.Regular),
  71. ForeColor = Color.Red,
  72. TextAlign = ContentAlignment.MiddleCenter
  73. };
  74. Button btnOK = new Button
  75. {
  76. Text = "确定",
  77. Size = new Size(120, 40),
  78. Location = new Point(190, 240),
  79. BackColor = Color.FromArgb(0, 122, 204),
  80. ForeColor = Color.White,
  81. FlatStyle = FlatStyle.Flat,
  82. Font = new Font("微软雅黑", 11, FontStyle.Regular)
  83. };
  84. btnOK.Click += (sender, e) =>
  85. {
  86. this.DialogResult = DialogResult.OK;
  87. SafeClose();
  88. };
  89. this.Controls.Add(titlePanel);
  90. this.Controls.Add(lblMessage);
  91. this.Controls.Add(btnOK);
  92. // 窗口拖动支持
  93. titlePanel.MouseDown += (sender, e) =>
  94. {
  95. if (e.Button == MouseButtons.Left && !_isClosing)
  96. {
  97. ReleaseCapture();
  98. SendMessage(this.Handle, 0xA1, 0x2, 0);
  99. }
  100. };
  101. this.FormClosing += (s, e) =>
  102. {
  103. _isClosing = true;
  104. };
  105. }
  106. catch (Exception ex)
  107. {
  108. MessageBox.Show($"消息框创建失败: {ex.Message}", "错误");
  109. }
  110. }
  111. private void SafeClose()
  112. {
  113. if (!_isClosing)
  114. {
  115. _isClosing = true;
  116. this.Close();
  117. }
  118. }
  119. protected override void Dispose(bool disposing)
  120. {
  121. if (disposing)
  122. {
  123. _isClosing = true;
  124. }
  125. base.Dispose(disposing);
  126. }
  127. [System.Runtime.InteropServices.DllImport("user32.dll")]
  128. public static extern bool ReleaseCapture();
  129. [System.Runtime.InteropServices.DllImport("user32.dll")]
  130. public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
  131. public static DialogResult Show(string message, string title)
  132. {
  133. using (CustomMessageBox msgBox = new CustomMessageBox(message, title))
  134. {
  135. return msgBox.ShowDialog();
  136. }
  137. }
  138. }
  139. public partial class RichTextAutoBottom : RichTextBox
  140. {
  141. Thread thread;
  142. string FileName = "";
  143. public RichTextAutoBottom()
  144. {
  145. InitializeComponent();
  146. thread = new Thread(PlaySound);
  147. TextChanged += RichTextBox_TextChange;
  148. }
  149. private void RichTextBox_TextChange(object sender, EventArgs e)
  150. {
  151. SelectionStart = Text.Length;
  152. ScrollToCaret();
  153. }
  154. /// <summary>
  155. /// color设置AppendText的颜色
  156. /// </summary>
  157. /// <param name="str"></param>
  158. /// <param name="color"></param>
  159. public void AppendText(string str, Color color)
  160. {
  161. SelectionColor = color;
  162. base.AppendText(str);
  163. //如果颜色是红色则进行提示音
  164. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  165. {
  166. if(str.Contains("缺料"))
  167. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  168. else
  169. FileName = Application.StartupPath + @"\Resources\Sound\5185.wav";
  170. thread.Start();
  171. thread = new Thread(PlaySound);
  172. CustomMessageBox.Show(str, "异常提示");
  173. }
  174. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  175. {
  176. //颜色是绿色,进行正确提示音
  177. if (str.Contains("缺料"))
  178. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  179. else
  180. FileName = Application.StartupPath + @"\Resources\Sound\8378.wav";
  181. thread.Start();
  182. thread = new Thread(PlaySound);
  183. }
  184. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  185. {
  186. //颜色是绿色,进行正确提示音
  187. FileName = Application.StartupPath + @"\Resources\Sound\4082.wav";
  188. thread.Start();
  189. thread = new Thread(PlaySound);
  190. }
  191. else if (color == Color.LightGreen && Entity.SystemInf.CheckAudioEnable)
  192. {
  193. //颜色是绿色,进行正确提示音
  194. FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
  195. thread.Start();
  196. thread = new Thread(PlaySound);
  197. }
  198. else if (color == Color.DarkRed && Entity.SystemInf.CheckAudioEnable)
  199. {
  200. FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
  201. thread.Start();
  202. thread = new Thread(PlaySound);
  203. CustomMessageBox.Show(str, "异常提示");
  204. }
  205. LogManager.DoLog(FindForm().Tag + str);
  206. }
  207. /// <summary>
  208. /// 提示错误信息清楚指定控件的值
  209. /// </summary>
  210. /// <param name="str"></param>
  211. /// <param name="color"></param>
  212. /// <param name="ctl"></param>
  213. public void AppendText(string str, Color color, Control ctl)
  214. {
  215. SelectionColor = color;
  216. base.AppendText(str);
  217. ctl.Text = "";
  218. //如果颜色是红色则进行提示音
  219. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  220. {
  221. if (str.Contains("缺料"))
  222. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  223. else
  224. FileName = Application.StartupPath + @"\Resources\Sound\采集失败.wav";
  225. thread.Start();
  226. thread = new Thread(PlaySound);
  227. CustomMessageBox.Show(str, "异常提示");
  228. }
  229. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  230. {
  231. //颜色是绿色,进行正确提示音
  232. if (str.Contains("缺料"))
  233. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  234. else
  235. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  236. thread.Start();
  237. thread = new Thread(PlaySound);
  238. }
  239. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  240. {
  241. //颜色是绿色,进行正确提示音
  242. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  243. thread.Start();
  244. thread = new Thread(PlaySound);
  245. }
  246. else if (color == Color.LightGreen && Entity.SystemInf.CheckAudioEnable)
  247. {
  248. //颜色是绿色,进行正确提示音
  249. FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
  250. thread.Start();
  251. thread = new Thread(PlaySound);
  252. }
  253. else if (color == Color.DarkRed && Entity.SystemInf.CheckAudioEnable)
  254. {
  255. FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
  256. thread.Start();
  257. thread = new Thread(PlaySound);
  258. CustomMessageBox.Show(str, "异常提示");
  259. }
  260. LogManager.DoLog(FindForm().Tag + str);
  261. }
  262. private void PlaySound()
  263. {
  264. //要加载COM组件:Microsoft speech object Library
  265. if (!System.IO.File.Exists(FileName))
  266. {
  267. return;
  268. }
  269. try
  270. {
  271. SoundPlayer player = new SoundPlayer();
  272. player.SoundLocation = FileName;
  273. player.Load();
  274. player.Play();
  275. }
  276. catch (Exception e)
  277. {
  278. MessageBox.Show(e.Message);
  279. Entity.SystemInf.CheckAudioEnable = false;
  280. }
  281. }
  282. }
  283. }