RichTextAutoBottom.cs 2.6 KB

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