RichTextAutoBottom.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 = Application.StartupPath + @"\Resources\Sound\Windows Background.wav";
  12. public RichTextAutoBottom()
  13. {
  14. InitializeComponent();
  15. TextChanged += RichTextBox_TextChange;
  16. }
  17. private void RichTextBox_TextChange(object sender, EventArgs e)
  18. {
  19. SelectionStart = Text.Length;
  20. ScrollToCaret();
  21. }
  22. /// <summary>
  23. /// color设置AppendText的颜色
  24. /// </summary>
  25. /// <param name="str"></param>
  26. /// <param name="color"></param>
  27. public void AppendText(string str, Color color)
  28. {
  29. SelectionColor = color;
  30. base.AppendText(str);
  31. //如果颜色是红色则进行提示音
  32. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  33. {
  34. thread = new Thread(PlaySound);
  35. thread.Start();
  36. }
  37. }
  38. private void PlaySound()
  39. {
  40. //要加载COM组件:Microsoft speech object Library
  41. if (!System.IO.File.Exists(FileName))
  42. {
  43. return;
  44. }
  45. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  46. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  47. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  48. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  49. try
  50. {
  51. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  52. }
  53. catch (Exception)
  54. {
  55. Entity.SystemInf.CheckAudioEnable = false;
  56. }
  57. spFs.Close();
  58. }
  59. }
  60. }