RichTextAutoBottom.cs 5.7 KB

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