RichTextAutoBottom.cs 5.5 KB

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