RichTextAutoBottom.cs 3.2 KB

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