RichTextAutoBottom.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 partial class RichTextAutoBottom : RichTextBox
  10. {
  11. Thread thread;
  12. string FileName = "";
  13. public RichTextAutoBottom()
  14. {
  15. InitializeComponent();
  16. thread = new Thread(PlaySound);
  17. TextChanged += RichTextBox_TextChange;
  18. }
  19. private void RichTextBox_TextChange(object sender, EventArgs e)
  20. {
  21. SelectionStart = Text.Length;
  22. ScrollToCaret();
  23. }
  24. /// <summary>
  25. /// color设置AppendText的颜色
  26. /// </summary>
  27. /// <param name="str"></param>
  28. /// <param name="color"></param>
  29. public void AppendText(string str, Color color)
  30. {
  31. SelectionColor = color;
  32. string timePrefix = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss} ] ";
  33. base.AppendText(timePrefix + str);
  34. //如果颜色是红色则进行提示音
  35. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  36. {
  37. if(str.Contains("缺料"))
  38. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  39. else
  40. FileName = Application.StartupPath + @"\Resources\Sound\5185.wav";
  41. thread.Start();
  42. thread = new Thread(PlaySound);
  43. }
  44. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  45. {
  46. //颜色是绿色,进行正确提示音
  47. if (str.Contains("缺料"))
  48. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  49. else
  50. FileName = Application.StartupPath + @"\Resources\Sound\8378.wav";
  51. thread.Start();
  52. thread = new Thread(PlaySound);
  53. }
  54. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  55. {
  56. //颜色是绿色,进行正确提示音
  57. FileName = Application.StartupPath + @"\Resources\Sound\4082.wav";
  58. thread.Start();
  59. thread = new Thread(PlaySound);
  60. }
  61. LogManager.DoLog(FindForm().Tag + str);
  62. }
  63. /// <summary>
  64. /// 提示错误信息清楚指定控件的值
  65. /// </summary>
  66. /// <param name="str"></param>
  67. /// <param name="color"></param>
  68. /// <param name="ctl"></param>
  69. public void AppendText(string str, Color color, Control ctl)
  70. {
  71. SelectionColor = color;
  72. base.AppendText(str);
  73. ctl.Text = "";
  74. //如果颜色是红色则进行提示音
  75. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  76. {
  77. if (str.Contains("缺料"))
  78. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  79. else
  80. FileName = Application.StartupPath + @"\Resources\Sound\采集失败.wav";
  81. thread.Start();
  82. thread = new Thread(PlaySound);
  83. }
  84. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  85. {
  86. //颜色是绿色,进行正确提示音
  87. if (str.Contains("缺料"))
  88. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  89. else
  90. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  91. thread.Start();
  92. thread = new Thread(PlaySound);
  93. }
  94. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  95. {
  96. //颜色是绿色,进行正确提示音
  97. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  98. thread.Start();
  99. thread = new Thread(PlaySound);
  100. }
  101. LogManager.DoLog(FindForm().Tag + str);
  102. }
  103. private void PlaySound()
  104. {
  105. //要加载COM组件:Microsoft speech object Library
  106. if (!System.IO.File.Exists(FileName))
  107. {
  108. return;
  109. }
  110. try
  111. {
  112. SoundPlayer player = new SoundPlayer();
  113. player.SoundLocation = FileName;
  114. player.Load();
  115. player.Play();
  116. }
  117. catch (Exception e)
  118. {
  119. MessageBox.Show(e.Message);
  120. Entity.SystemInf.CheckAudioEnable = false;
  121. }
  122. }
  123. }
  124. }