RichTextAutoBottom.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Drawing;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. using UAS_MES.PublicMethod;
  6. namespace UAS_MES.CustomControl.RichText
  7. {
  8. public partial class RichTextAutoBottom : RichTextBox
  9. {
  10. Thread thread;
  11. string FileName = "";
  12. public RichTextAutoBottom()
  13. {
  14. InitializeComponent();
  15. thread = new Thread(PlaySound);
  16. TextChanged += RichTextBox_TextChange;
  17. }
  18. private void RichTextBox_TextChange(object sender, EventArgs e)
  19. {
  20. SelectionStart = Text.Length;
  21. ScrollToCaret();
  22. }
  23. /// <summary>
  24. /// color设置AppendText的颜色
  25. /// </summary>
  26. /// <param name="str"></param>
  27. /// <param name="color"></param>
  28. public void AppendText(string str, Color color)
  29. {
  30. SelectionColor = color;
  31. base.AppendText(str);
  32. //如果颜色是红色则进行提示音
  33. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  34. {
  35. FileName = Application.StartupPath + @"\Resources\Sound\5185.wav";
  36. thread.Start();
  37. thread = new Thread(PlaySound);
  38. }
  39. else if(color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  40. {
  41. //颜色是绿色,进行正确提示音
  42. FileName = Application.StartupPath + @"\Resources\Sound\8378.wav";
  43. thread.Start();
  44. thread = new Thread(PlaySound);
  45. }
  46. }
  47. /// <summary>
  48. /// 提示错误信息清楚指定控件的值
  49. /// </summary>
  50. /// <param name="str"></param>
  51. /// <param name="color"></param>
  52. /// <param name="ctl"></param>
  53. public void AppendText(string str, Color color, Control ctl)
  54. {
  55. SelectionColor = color;
  56. base.AppendText(str);
  57. ctl.Text = "";
  58. //如果颜色是红色则进行提示音
  59. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  60. {
  61. FileName = Application.StartupPath + @"\Resources\Sound\5185.wav";
  62. thread.Start();
  63. thread = new Thread(PlaySound);
  64. }
  65. else if(color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  66. {
  67. //颜色是绿色,进行正确提示音
  68. FileName = Application.StartupPath + @"\Resources\Sound\8378.wav";
  69. thread.Start();
  70. thread = new Thread(PlaySound);
  71. }
  72. }
  73. private void PlaySound()
  74. {
  75. //要加载COM组件:Microsoft speech object Library
  76. if (!System.IO.File.Exists(FileName))
  77. {
  78. return;
  79. }
  80. try
  81. {
  82. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  83. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  84. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  85. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  86. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  87. spFs.Close();
  88. }
  89. catch (Exception)
  90. {
  91. Entity.SystemInf.CheckAudioEnable = false;
  92. }
  93. }
  94. }
  95. }