RichTextAutoBottom.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
  171. thread.Start();
  172. thread = new Thread(PlaySound);
  173. CustomMessageBox.Show(str, "异常提示");
  174. }
  175. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  176. {
  177. //颜色是绿色,进行正确提示音
  178. if (str.Contains("缺料"))
  179. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  180. else
  181. //FileName = Application.StartupPath + @"\Resources\Sound\8378.wav";
  182. FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
  183. thread.Start();
  184. thread = new Thread(PlaySound);
  185. }
  186. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  187. {
  188. //颜色是绿色,进行正确提示音
  189. FileName = Application.StartupPath + @"\Resources\Sound\4082.wav";
  190. thread.Start();
  191. thread = new Thread(PlaySound);
  192. }
  193. else if (color == Color.LightGreen && Entity.SystemInf.CheckAudioEnable)
  194. {
  195. //颜色是绿色,进行正确提示音
  196. FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
  197. thread.Start();
  198. thread = new Thread(PlaySound);
  199. }
  200. else if (color == Color.DarkRed && Entity.SystemInf.CheckAudioEnable)
  201. {
  202. FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
  203. thread.Start();
  204. thread = new Thread(PlaySound);
  205. CustomMessageBox.Show(str, "异常提示");
  206. }
  207. LogManager.DoLog(FindForm().Tag + str);
  208. }
  209. /// <summary>
  210. /// 提示错误信息清楚指定控件的值
  211. /// </summary>
  212. /// <param name="str"></param>
  213. /// <param name="color"></param>
  214. /// <param name="ctl"></param>
  215. public void AppendText(string str, Color color, Control ctl)
  216. {
  217. SelectionColor = color;
  218. base.AppendText(str);
  219. ctl.Text = "";
  220. //如果颜色是红色则进行提示音
  221. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  222. {
  223. if (str.Contains("缺料"))
  224. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  225. else
  226. //FileName = Application.StartupPath + @"\Resources\Sound\采集失败.wav";
  227. FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
  228. thread.Start();
  229. thread = new Thread(PlaySound);
  230. CustomMessageBox.Show(str, "异常提示");
  231. }
  232. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  233. {
  234. //颜色是绿色,进行正确提示音
  235. if (str.Contains("缺料"))
  236. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  237. else
  238. //FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  239. FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
  240. thread.Start();
  241. thread = new Thread(PlaySound);
  242. }
  243. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  244. {
  245. //颜色是绿色,进行正确提示音
  246. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  247. thread.Start();
  248. thread = new Thread(PlaySound);
  249. }
  250. else if (color == Color.LightGreen && Entity.SystemInf.CheckAudioEnable)
  251. {
  252. //颜色是绿色,进行正确提示音
  253. FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
  254. thread.Start();
  255. thread = new Thread(PlaySound);
  256. }
  257. else if (color == Color.DarkRed && Entity.SystemInf.CheckAudioEnable)
  258. {
  259. FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
  260. thread.Start();
  261. thread = new Thread(PlaySound);
  262. CustomMessageBox.Show(str, "异常提示");
  263. }
  264. LogManager.DoLog(FindForm().Tag + str);
  265. }
  266. private void PlaySound()
  267. {
  268. //要加载COM组件:Microsoft speech object Library
  269. if (!System.IO.File.Exists(FileName))
  270. {
  271. return;
  272. }
  273. try
  274. {
  275. SoundPlayer player = new SoundPlayer();
  276. player.SoundLocation = FileName;
  277. player.Load();
  278. player.Play();
  279. }
  280. catch (Exception e)
  281. {
  282. MessageBox.Show(e.Message);
  283. Entity.SystemInf.CheckAudioEnable = false;
  284. }
  285. }
  286. }
  287. }