RichTextAutoBottom.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\3291.wav";
  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. thread.Start();
  36. thread = new Thread(PlaySound);
  37. }
  38. }
  39. /// <summary>
  40. /// 提示错误信息清楚指定控件的值
  41. /// </summary>
  42. /// <param name="str"></param>
  43. /// <param name="color"></param>
  44. /// <param name="ctl"></param>
  45. public void AppendText(string str, Color color,Control ctl)
  46. {
  47. SelectionColor = color;
  48. base.AppendText(str);
  49. ctl.Text = "";
  50. //如果颜色是红色则进行提示音
  51. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  52. {
  53. thread.Start();
  54. thread = new Thread(PlaySound);
  55. }
  56. }
  57. private void PlaySound()
  58. {
  59. //要加载COM组件:Microsoft speech object Library
  60. if (!System.IO.File.Exists(FileName))
  61. {
  62. return;
  63. }
  64. try
  65. {
  66. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  67. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  68. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  69. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  70. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  71. spFs.Close();
  72. }
  73. catch (Exception)
  74. {
  75. Entity.SystemInf.CheckAudioEnable = false;
  76. }
  77. }
  78. }
  79. }