CameraParamBar.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. namespace PaintDotNet.ImageCollect
  8. {
  9. public class CameraParamBar : UserControl
  10. {
  11. #region Designer
  12. private Label label1;
  13. private Label lblUnit;
  14. private TextBox txbValue;
  15. private Panel panel1;
  16. private TrackBar tkbValue;
  17. private Label lblMin;
  18. private Label lblMax;
  19. private Label lblCaption;
  20. public CameraParamBar()
  21. {
  22. InitializeComponent();
  23. Dock = DockStyle.Top;
  24. SizeChanged += (s1, e1) => { this.Height = 44; };
  25. }
  26. private void InitializeComponent()
  27. {
  28. this.lblCaption = new System.Windows.Forms.Label();
  29. this.label1 = new System.Windows.Forms.Label();
  30. this.lblUnit = new System.Windows.Forms.Label();
  31. this.txbValue = new System.Windows.Forms.TextBox();
  32. this.panel1 = new System.Windows.Forms.Panel();
  33. this.lblMax = new System.Windows.Forms.Label();
  34. this.lblMin = new System.Windows.Forms.Label();
  35. this.tkbValue = new System.Windows.Forms.TrackBar();
  36. this.panel1.SuspendLayout();
  37. ((System.ComponentModel.ISupportInitialize)(this.tkbValue)).BeginInit();
  38. this.SuspendLayout();
  39. //
  40. // lblCaption
  41. //
  42. this.lblCaption.Dock = System.Windows.Forms.DockStyle.Left;
  43. this.lblCaption.Location = new System.Drawing.Point(2, 2);
  44. this.lblCaption.Name = "lblCaption";
  45. this.lblCaption.Padding = new System.Windows.Forms.Padding(0, 4, 0, 0);
  46. this.lblCaption.Size = new System.Drawing.Size(74, 40);
  47. this.lblCaption.TabIndex = 0;
  48. this.lblCaption.Text = "ParmName";
  49. //
  50. // label1
  51. //
  52. this.label1.Dock = System.Windows.Forms.DockStyle.Left;
  53. this.label1.Location = new System.Drawing.Point(76, 2);
  54. this.label1.Name = "label1";
  55. this.label1.Padding = new System.Windows.Forms.Padding(0, 4, 0, 0);
  56. this.label1.Size = new System.Drawing.Size(11, 40);
  57. this.label1.TabIndex = 1;
  58. this.label1.Text = ":";
  59. //
  60. // lblUnit
  61. //
  62. this.lblUnit.Dock = System.Windows.Forms.DockStyle.Right;
  63. this.lblUnit.Location = new System.Drawing.Point(485, 2);
  64. this.lblUnit.Name = "lblUnit";
  65. this.lblUnit.Padding = new System.Windows.Forms.Padding(0, 4, 0, 0);
  66. this.lblUnit.Size = new System.Drawing.Size(40, 40);
  67. this.lblUnit.TabIndex = 3;
  68. this.lblUnit.Text = "unit";
  69. //
  70. // txbValue
  71. //
  72. this.txbValue.Dock = System.Windows.Forms.DockStyle.Right;
  73. this.txbValue.Location = new System.Drawing.Point(425, 2);
  74. this.txbValue.Name = "txbValue";
  75. this.txbValue.Size = new System.Drawing.Size(60, 21);
  76. this.txbValue.TabIndex = 4;
  77. this.txbValue.Text = "0";
  78. this.txbValue.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txbValue_KeyPress);
  79. //
  80. // panel1
  81. //
  82. this.panel1.Controls.Add(this.lblMax);
  83. this.panel1.Controls.Add(this.lblMin);
  84. this.panel1.Controls.Add(this.tkbValue);
  85. this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
  86. this.panel1.Location = new System.Drawing.Point(87, 2);
  87. this.panel1.Name = "panel1";
  88. this.panel1.Size = new System.Drawing.Size(338, 40);
  89. this.panel1.TabIndex = 5;
  90. //
  91. // lblMax
  92. //
  93. this.lblMax.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  94. this.lblMax.Location = new System.Drawing.Point(298, 24);
  95. this.lblMax.Name = "lblMax";
  96. this.lblMax.Size = new System.Drawing.Size(35, 12);
  97. this.lblMax.TabIndex = 2;
  98. this.lblMax.Text = "10000";
  99. this.lblMax.TextAlign = System.Drawing.ContentAlignment.TopRight;
  100. //
  101. // lblMin
  102. //
  103. this.lblMin.AutoSize = true;
  104. this.lblMin.Location = new System.Drawing.Point(9, 24);
  105. this.lblMin.Name = "lblMin";
  106. this.lblMin.Size = new System.Drawing.Size(11, 12);
  107. this.lblMin.TabIndex = 1;
  108. this.lblMin.Text = "1";
  109. //
  110. // tkbValue
  111. //
  112. this.tkbValue.Dock = System.Windows.Forms.DockStyle.Fill;
  113. this.tkbValue.Location = new System.Drawing.Point(0, 0);
  114. this.tkbValue.Maximum = 100;
  115. this.tkbValue.Name = "tkbValue";
  116. this.tkbValue.Size = new System.Drawing.Size(338, 40);
  117. this.tkbValue.TabIndex = 0;
  118. this.tkbValue.TickStyle = System.Windows.Forms.TickStyle.None;
  119. this.tkbValue.Scroll += new System.EventHandler(this.tkbValue_Scroll);
  120. //
  121. // CameraParamBar
  122. //
  123. this.AutoSize = true;
  124. this.Controls.Add(this.panel1);
  125. this.Controls.Add(this.txbValue);
  126. this.Controls.Add(this.lblUnit);
  127. this.Controls.Add(this.label1);
  128. this.Controls.Add(this.lblCaption);
  129. this.Name = "CameraParamBar";
  130. this.Padding = new System.Windows.Forms.Padding(2);
  131. this.Size = new System.Drawing.Size(527, 44);
  132. this.panel1.ResumeLayout(false);
  133. this.panel1.PerformLayout();
  134. ((System.ComponentModel.ISupportInitialize)(this.tkbValue)).EndInit();
  135. this.ResumeLayout(false);
  136. this.PerformLayout();
  137. }
  138. #endregion
  139. public event Action<double> OnValueChange;
  140. public string Caption
  141. {
  142. get { return lblCaption.Text; }
  143. set { lblCaption.Text = value; }
  144. }
  145. public string unit { get => lblUnit.Text; set => lblUnit.Text = value; }
  146. public int Value
  147. {
  148. get
  149. {
  150. try { int value = int.Parse(txbValue.Text); return value; } catch (Exception ex) { MessageBox.Show(ex.Message); return 0; }
  151. }
  152. set
  153. {
  154. tkbValue.Value = Math.Max(tkbValue.Minimum, Math.Min(value, tkbValue.Maximum));
  155. txbValue.Text = value.ToString();
  156. }
  157. }
  158. #region Bar
  159. private void tkbValue_Scroll(object sender, EventArgs e)
  160. {
  161. var value = tkbValue.Value;
  162. txbValue.Text = value.ToString();
  163. OnValueChange?.Invoke(Value);
  164. }
  165. #endregion
  166. #region max/min
  167. public int Max
  168. {
  169. get => tkbValue.Maximum; set
  170. {
  171. tkbValue.Maximum = value;
  172. lblMax.Text = value.ToString();
  173. }
  174. }
  175. public int Min
  176. {
  177. get => tkbValue.Minimum;
  178. set
  179. {
  180. tkbValue.Minimum = value; ;
  181. lblMin.Text = value.ToString();
  182. }
  183. }
  184. #endregion
  185. private void txbValue_KeyPress(object sender, KeyPressEventArgs e)
  186. {
  187. if (e.KeyChar == (char)Keys.Enter)
  188. {
  189. var value = Value;
  190. value = Math.Min(Math.Max(Min, value), value);
  191. Value = value;
  192. OnValueChange?.Invoke(value);
  193. }
  194. else if (!char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Delete)) // 非数字键, 放弃该输入
  195. {
  196. e.Handled = true;
  197. }
  198. }
  199. }
  200. }