ucWeightCarTJXJG.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Common;
  11. using Infragistics.Win.UltraWinEditors;
  12. namespace MeterModuleLibrary
  13. {
  14. public partial class ucWeightCarTJXJG : UserControl
  15. {
  16. /// <summary>
  17. /// 记录当前的颜色,在线程中窑调用下面的setControl方法前
  18. /// 可先判断当前的颜色与显示颜色是否一致
  19. /// 若一致则可不调用setControl,不一致再调用
  20. /// </summary>
  21. public bool isGreenWgt = true;
  22. public bool isGrennExceed = true;
  23. public ucWeightCarTJXJG()
  24. {
  25. InitializeComponent();
  26. }
  27. /// <summary>
  28. /// 默认加载窗格线
  29. /// </summary>
  30. /// <param name="sender"></param>
  31. /// <param name="e"></param>
  32. private void ucWeightCarTJXJG_Load(object sender, EventArgs e)
  33. {
  34. tableLayoutPanelWeigtAndType.CellBorderStyle = (TableLayoutPanelCellBorderStyle)BorderStyle.FixedSingle;
  35. }
  36. #region 线程中赋值的控件需使用委托
  37. private delegate void UpdateUIEventHander(object sender, UpdateUIArgs args); //自定义事件用来从线程中更新控件的值
  38. public class UpdateUIArgs : EventArgs
  39. {
  40. public string textValue { get; private set; }
  41. public UpdateUIArgs(string textValue)
  42. {
  43. this.textValue = textValue;
  44. }
  45. }
  46. /// <summary>
  47. /// 重量稳定
  48. /// </summary>
  49. /// <param name="bGreen"></param>
  50. public void setStable(bool bGreen)
  51. {
  52. if (!PbCache.isLockWgt)
  53. {
  54. if (picStable.IsHandleCreated)
  55. {
  56. isGreenWgt = bGreen;
  57. string sValue = isGreenWgt ? "green" : "red";
  58. picStable.Invoke(new UpdateUIEventHander(UpdateUI_Method), picStable, new UpdateUIArgs(sValue));
  59. }
  60. }
  61. }
  62. /// <summary> txtWeightType
  63. /// 实时写入重量
  64. /// PbCache.isLockWgt 重量未锁定则可写入,否则调用了也不会写入
  65. /// </summary>
  66. /// <param name="db">重量信息</param>
  67. public void setWgt(double db)
  68. {
  69. if (!PbCache.isLockWgt)
  70. {
  71. if (txtWeight.IsHandleCreated)
  72. {
  73. txtWeight.Invoke(new UpdateUIEventHander(UpdateUI_Method), txtWeight, new UpdateUIArgs(db.ToString("0.00") + ""));
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// 写入重量类型
  79. /// PbCache.isLockWgt 重量未锁定则可写入,否则调用了也不会写入
  80. /// </summary>
  81. /// <param name="db">重量信息</param>
  82. public void setWeightType(string db)
  83. {
  84. if (!PbCache.isLockWgt)
  85. {
  86. if (txtWeightType.IsHandleCreated)
  87. {
  88. txtWeightType.Invoke(new UpdateUIEventHander(UpdateUI_Method), txtWeightType, new UpdateUIArgs(db + ""));
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// 设置重量字体的背景颜色:Lime及NotShow(不显示)
  94. /// </summary>
  95. /// <param name="db"></param>
  96. public void setWgtBackColor(bool bLime)
  97. {
  98. if (!PbCache.isLockWgt)
  99. {
  100. if (txtWeight.IsHandleCreated)
  101. {
  102. isGreenWgt = bLime;
  103. string sValue = isGreenWgt ? "Lime" : "NotShow";
  104. txtWeight.Invoke(new UpdateUIEventHander(UpdateUIBackColor_Method), txtWeight, new UpdateUIArgs(sValue));
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// 设置背景色
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="args"></param>
  113. private void UpdateUIBackColor_Method(object sender, UpdateUIArgs args)
  114. {
  115. if (sender is UltraTextEditor)
  116. {
  117. if (args.textValue == "Lime")
  118. {
  119. ((UltraTextEditor)sender).Appearance.BackColor = Color.Black;
  120. ((UltraTextEditor)sender).Appearance.ForeColorDisabled = Color.Black;
  121. }
  122. else
  123. {
  124. //System.Drawing.Color.Transparent;跟随背景色
  125. ((UltraTextEditor)sender).Appearance.BackColor = Color.Transparent;
  126. ((UltraTextEditor)sender).Appearance.ForeColorDisabled = Color.Transparent;
  127. //((UltraTextEditor)sender).Appearance.BackColor = Color.FromArgb(((int)(((byte)(236)))), ((int)(((byte)(244)))), ((int)(((byte)(252)))));
  128. //((UltraTextEditor)sender).Appearance.ForeColorDisabled = Color.FromArgb(((int)(((byte)(236)))), ((int)(((byte)(244)))), ((int)(((byte)(252)))));
  129. }
  130. }
  131. }
  132. private void UpdateUI_Method(object sender, UpdateUIArgs args)
  133. {
  134. if(sender is UltraTextEditor)
  135. {
  136. if (args.textValue == "" || args.textValue == null)
  137. {
  138. ((UltraTextEditor)sender).Text = "0";
  139. }
  140. else
  141. {
  142. ((UltraTextEditor)sender).Text = args.textValue;
  143. }
  144. }
  145. else if (sender is Button)
  146. {
  147. if (args.textValue == "red")
  148. {
  149. ((Button)sender).BackColor = Color.Red;
  150. ((Button)sender).ForeColor = Color.White;
  151. }
  152. else if (args.textValue == "green")
  153. {
  154. ((Button)sender).BackColor = Color.White;
  155. ((Button)sender).ForeColor = Color.Black;
  156. }
  157. else
  158. {
  159. ((Label)sender).Text = args.textValue;
  160. }
  161. }
  162. else if (sender is PictureBox)
  163. {
  164. if (args.textValue == "red")
  165. {
  166. ((PictureBox)sender).Load(PbCache.path + "\\image\\icon\\不具备.png");
  167. }
  168. else
  169. {
  170. ((PictureBox)sender).Load(PbCache.path + "\\image\\icon\\具备.png");
  171. }
  172. }
  173. }
  174. #endregion
  175. }
  176. }