RichTextAutoBottom.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. FileName = Application.StartupPath + @"\Resources\Sound\FAIL.wav";
  37. thread.Start();
  38. thread = new Thread(PlaySound);
  39. }
  40. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  41. {
  42. //颜色是绿色,进行正确提示音
  43. FileName = Application.StartupPath + @"\Resources\Sound\PASS.wav";
  44. thread.Start();
  45. thread = new Thread(PlaySound);
  46. }
  47. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  48. {
  49. //颜色是绿色,进行正确提示音
  50. FileName = Application.StartupPath + @"\Resources\Sound\4082.wav";
  51. thread.Start();
  52. thread = new Thread(PlaySound);
  53. }
  54. else if (color == Color.OrangeRed && Entity.SystemInf.CheckAudioEnable)
  55. {
  56. //颜色是绿色,进行正确提示音
  57. FileName = Application.StartupPath + @"\Resources\Sound\失败.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. FileName = Application.StartupPath + @"\Resources\Sound\FAIL.wav";
  78. thread.Start();
  79. thread = new Thread(PlaySound);
  80. }
  81. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  82. {
  83. //颜色是绿色,进行正确提示音
  84. FileName = Application.StartupPath + @"\Resources\Sound\PASS.wav";
  85. thread.Start();
  86. thread = new Thread(PlaySound);
  87. }
  88. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  89. {
  90. //颜色是绿色,进行正确提示音
  91. FileName = Application.StartupPath + @"\Resources\Sound\4082.wav";
  92. thread.Start();
  93. thread = new Thread(PlaySound);
  94. }
  95. else if (color == Color.OrangeRed && Entity.SystemInf.CheckAudioEnable)
  96. {
  97. //颜色是绿色,进行正确提示音
  98. FileName = Application.StartupPath + @"\Resources\Sound\失败.wav";
  99. thread.Start();
  100. thread = new Thread(PlaySound);
  101. }
  102. LogManager.DoLog(FindForm().Tag + str);
  103. }
  104. /// <summary>
  105. /// 提示错误信息清楚指定控件的值
  106. /// </summary>
  107. /// <param name="str"></param>
  108. /// <param name="color"></param>
  109. /// <param name="ctl"></param>
  110. public void AppendText(string str, Color color, Control ctl, Control ct2)
  111. {
  112. SelectionColor = color;
  113. base.AppendText(str);
  114. ctl.Text = "";
  115. ct2.Text = "";
  116. //如果颜色是红色则进行提示音
  117. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  118. {
  119. FileName = Application.StartupPath + @"\Resources\Sound\FAIL.wav";
  120. thread.Start();
  121. thread = new Thread(PlaySound);
  122. }
  123. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  124. {
  125. //颜色是绿色,进行正确提示音
  126. FileName = Application.StartupPath + @"\Resources\Sound\PASS.wav";
  127. thread.Start();
  128. thread = new Thread(PlaySound);
  129. }
  130. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  131. {
  132. //颜色是绿色,进行正确提示音
  133. FileName = Application.StartupPath + @"\Resources\Sound\4082.wav";
  134. thread.Start();
  135. thread = new Thread(PlaySound);
  136. }
  137. else if (color == Color.OrangeRed && Entity.SystemInf.CheckAudioEnable)
  138. {
  139. //颜色是绿色,进行正确提示音
  140. FileName = Application.StartupPath + @"\Resources\Sound\失败.wav";
  141. thread.Start();
  142. thread = new Thread(PlaySound);
  143. }
  144. LogManager.DoLog(FindForm().Tag + str);
  145. }
  146. private void PlaySound()
  147. {
  148. //要加载COM组件:Microsoft speech object Library
  149. if (!System.IO.File.Exists(FileName))
  150. {
  151. return;
  152. }
  153. try
  154. {
  155. SoundPlayer player = new SoundPlayer();
  156. player.SoundLocation = FileName;
  157. player.Load();
  158. player.Play();
  159. }
  160. catch (Exception e)
  161. {
  162. MessageBox.Show(e.Message);
  163. Entity.SystemInf.CheckAudioEnable = false;
  164. }
  165. }
  166. }
  167. }