RichTextAutoBottom.cs 3.1 KB

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