RichTextAutoBottom.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. base.AppendText(str);
  33. //如果颜色是红色则进行提示音
  34. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  35. {
  36. if(str.Contains("缺料"))
  37. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  38. else
  39. FileName = Application.StartupPath + @"\Resources\Sound\5185.wav";
  40. thread.Start();
  41. thread = new Thread(PlaySound);
  42. MessageBox.Show(str, "异常提示");
  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. else if (color == Color.LightGreen && Entity.SystemInf.CheckAudioEnable)
  62. {
  63. //颜色是绿色,进行正确提示音
  64. FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
  65. thread.Start();
  66. thread = new Thread(PlaySound);
  67. }
  68. else if (color == Color.DarkRed && Entity.SystemInf.CheckAudioEnable)
  69. {
  70. FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
  71. thread.Start();
  72. thread = new Thread(PlaySound);
  73. MessageBox.Show(str, "异常提示");
  74. }
  75. LogManager.DoLog(FindForm().Tag + str);
  76. }
  77. /// <summary>
  78. /// 提示错误信息清楚指定控件的值
  79. /// </summary>
  80. /// <param name="str"></param>
  81. /// <param name="color"></param>
  82. /// <param name="ctl"></param>
  83. public void AppendText(string str, Color color, Control ctl)
  84. {
  85. SelectionColor = color;
  86. base.AppendText(str);
  87. ctl.Text = "";
  88. //如果颜色是红色则进行提示音
  89. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  90. {
  91. if (str.Contains("缺料"))
  92. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  93. else
  94. FileName = Application.StartupPath + @"\Resources\Sound\采集失败.wav";
  95. thread.Start();
  96. thread = new Thread(PlaySound);
  97. MessageBox.Show(str, "异常提示");
  98. }
  99. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  100. {
  101. //颜色是绿色,进行正确提示音
  102. if (str.Contains("缺料"))
  103. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  104. else
  105. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  106. thread.Start();
  107. thread = new Thread(PlaySound);
  108. }
  109. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  110. {
  111. //颜色是绿色,进行正确提示音
  112. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  113. thread.Start();
  114. thread = new Thread(PlaySound);
  115. }
  116. else if (color == Color.LightGreen && Entity.SystemInf.CheckAudioEnable)
  117. {
  118. //颜色是绿色,进行正确提示音
  119. FileName = Application.StartupPath + @"\Resources\Sound\3853.wav";
  120. thread.Start();
  121. thread = new Thread(PlaySound);
  122. }
  123. else if (color == Color.DarkRed && Entity.SystemInf.CheckAudioEnable)
  124. {
  125. FileName = Application.StartupPath + @"\Resources\Sound\1454.wav";
  126. thread.Start();
  127. thread = new Thread(PlaySound);
  128. MessageBox.Show(str, "异常提示");
  129. }
  130. LogManager.DoLog(FindForm().Tag + str);
  131. }
  132. private void PlaySound()
  133. {
  134. //要加载COM组件:Microsoft speech object Library
  135. if (!System.IO.File.Exists(FileName))
  136. {
  137. return;
  138. }
  139. try
  140. {
  141. SoundPlayer player = new SoundPlayer();
  142. player.SoundLocation = FileName;
  143. player.Load();
  144. player.Play();
  145. }
  146. catch (Exception e)
  147. {
  148. MessageBox.Show(e.Message);
  149. Entity.SystemInf.CheckAudioEnable = false;
  150. }
  151. }
  152. }
  153. }