RichTextAutoBottom.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Drawing;
  3. using System.Media;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using UAS_MES_NEW.PublicMethod;
  7. namespace UAS_MES_NEW.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. if(str.Contains("缺料"))
  37. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  38. else
  39. FileName = Application.StartupPath + @"\Resources\Sound\5185.wav";
  40. thread.Start();
  41. thread = new Thread(PlaySound);
  42. }
  43. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  44. {
  45. //颜色是绿色,进行正确提示音
  46. if (str.Contains("缺料"))
  47. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  48. else
  49. FileName = Application.StartupPath + @"\Resources\Sound\8378.wav";
  50. thread.Start();
  51. thread = new Thread(PlaySound);
  52. }
  53. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  54. {
  55. //颜色是绿色,进行正确提示音
  56. FileName = Application.StartupPath + @"\Resources\Sound\4082.wav";
  57. thread.Start();
  58. thread = new Thread(PlaySound);
  59. }
  60. LogManager.DoLog(FindForm().Tag + str);
  61. }
  62. /// <summary>
  63. /// 提示错误信息清楚指定控件的值
  64. /// </summary>
  65. /// <param name="str"></param>
  66. /// <param name="color"></param>
  67. /// <param name="ctl"></param>
  68. public void AppendText(string str, Color color, Control ctl)
  69. {
  70. SelectionColor = color;
  71. base.AppendText(str);
  72. ctl.Text = "";
  73. //如果颜色是红色则进行提示音
  74. if (color == Color.Red && Entity.SystemInf.CheckAudioEnable)
  75. {
  76. if (str.Contains("缺料"))
  77. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  78. else
  79. FileName = Application.StartupPath + @"\Resources\Sound\采集失败.wav";
  80. thread.Start();
  81. thread = new Thread(PlaySound);
  82. }
  83. else if (color == Color.Green && Entity.SystemInf.CheckAudioEnable)
  84. {
  85. //颜色是绿色,进行正确提示音
  86. if (str.Contains("缺料"))
  87. FileName = Application.StartupPath + @"\Resources\Sound\缺料.wav";
  88. else
  89. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  90. thread.Start();
  91. thread = new Thread(PlaySound);
  92. }
  93. else if (color == Color.Blue && Entity.SystemInf.CheckAudioEnable)
  94. {
  95. //颜色是绿色,进行正确提示音
  96. FileName = Application.StartupPath + @"\Resources\Sound\采集正确.wav";
  97. thread.Start();
  98. thread = new Thread(PlaySound);
  99. }
  100. LogManager.DoLog(FindForm().Tag + str);
  101. }
  102. private void PlaySound()
  103. {
  104. //要加载COM组件:Microsoft speech object Library
  105. if (!System.IO.File.Exists(FileName))
  106. {
  107. return;
  108. }
  109. try
  110. {
  111. SoundPlayer player = new SoundPlayer();
  112. player.SoundLocation = FileName;
  113. player.Load();
  114. player.Play();
  115. }
  116. catch (Exception e)
  117. {
  118. MessageBox.Show(e.Message);
  119. Entity.SystemInf.CheckAudioEnable = false;
  120. }
  121. }
  122. }
  123. }