CameraParamBar2.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.Preview2
  8. {
  9. public class CameraParamBar2 : UserControl
  10. {
  11. #region Designer
  12. private TextBox txbValue;
  13. private Panel panel1;
  14. private Label lblMin;
  15. private Label lblMax;
  16. public System.Windows.Forms.TrackBar tkbValue;
  17. private Label lblCaption;
  18. public CameraParamBar2()
  19. {
  20. InitializeComponent();
  21. }
  22. private void InitializeComponent()
  23. {
  24. this.lblCaption = new System.Windows.Forms.Label();
  25. this.txbValue = new System.Windows.Forms.TextBox();
  26. this.panel1 = new System.Windows.Forms.Panel();
  27. this.lblMax = new System.Windows.Forms.Label();
  28. this.lblMin = new System.Windows.Forms.Label();
  29. this.tkbValue = new System.Windows.Forms.TrackBar();
  30. this.panel1.SuspendLayout();
  31. ((System.ComponentModel.ISupportInitialize)(this.tkbValue)).BeginInit();
  32. this.SuspendLayout();
  33. //
  34. // lblCaption
  35. //
  36. this.lblCaption.Dock = System.Windows.Forms.DockStyle.Left;
  37. this.lblCaption.Location = new System.Drawing.Point(0, 0);
  38. this.lblCaption.Name = "lblCaption";
  39. this.lblCaption.Padding = new System.Windows.Forms.Padding(0, 2, 0, 0);
  40. this.lblCaption.Size = new System.Drawing.Size(69, 32);
  41. this.lblCaption.TabIndex = 0;
  42. this.lblCaption.Text = "ParmName";
  43. //
  44. // txbValue
  45. //
  46. this.txbValue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  47. this.txbValue.Dock = System.Windows.Forms.DockStyle.Right;
  48. this.txbValue.Location = new System.Drawing.Point(487, 0);
  49. this.txbValue.Name = "txbValue";
  50. this.txbValue.Size = new System.Drawing.Size(40, 21);
  51. this.txbValue.TabIndex = 4;
  52. this.txbValue.Text = "0";
  53. this.txbValue.WordWrap = false;
  54. this.txbValue.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txbValue_KeyPress);
  55. //
  56. // panel1
  57. //
  58. this.panel1.Controls.Add(this.lblMax);
  59. this.panel1.Controls.Add(this.lblMin);
  60. this.panel1.Controls.Add(this.tkbValue);
  61. this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
  62. this.panel1.Location = new System.Drawing.Point(69, 0);
  63. this.panel1.Name = "panel1";
  64. this.panel1.Padding = new System.Windows.Forms.Padding(0, 0, 4, 0);
  65. this.panel1.Size = new System.Drawing.Size(418, 32);
  66. this.panel1.TabIndex = 5;
  67. //
  68. // lblMax
  69. //
  70. this.lblMax.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  71. this.lblMax.Location = new System.Drawing.Point(383, 18);
  72. this.lblMax.Name = "lblMax";
  73. this.lblMax.Size = new System.Drawing.Size(35, 12);
  74. this.lblMax.TabIndex = 2;
  75. this.lblMax.Text = "10000";
  76. this.lblMax.TextAlign = System.Drawing.ContentAlignment.TopRight;
  77. //
  78. // lblMin
  79. //
  80. this.lblMin.AutoSize = true;
  81. this.lblMin.Location = new System.Drawing.Point(3, 18);
  82. this.lblMin.Name = "lblMin";
  83. this.lblMin.Size = new System.Drawing.Size(11, 12);
  84. this.lblMin.TabIndex = 1;
  85. this.lblMin.Text = "0";
  86. //
  87. // tkbValue
  88. //
  89. this.tkbValue.AutoSize = false;
  90. this.tkbValue.Dock = System.Windows.Forms.DockStyle.Top;
  91. this.tkbValue.Location = new System.Drawing.Point(0, 0);
  92. this.tkbValue.Name = "tkbValue";
  93. this.tkbValue.Size = new System.Drawing.Size(414, 20);
  94. this.tkbValue.TabIndex = 3;
  95. this.tkbValue.TabStop = false;
  96. this.tkbValue.Text = "metroTrackBar1";
  97. this.tkbValue.TickStyle = System.Windows.Forms.TickStyle.None;
  98. this.tkbValue.Scroll += new System.EventHandler(this.tkbValue_Scroll);
  99. //
  100. // CameraParamBar2
  101. //
  102. this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
  103. this.Controls.Add(this.panel1);
  104. this.Controls.Add(this.txbValue);
  105. this.Controls.Add(this.lblCaption);
  106. this.Name = "CameraParamBar2";
  107. this.Size = new System.Drawing.Size(527, 32);
  108. this.panel1.ResumeLayout(false);
  109. this.panel1.PerformLayout();
  110. ((System.ComponentModel.ISupportInitialize)(this.tkbValue)).EndInit();
  111. this.ResumeLayout(false);
  112. this.PerformLayout();
  113. }
  114. #endregion
  115. public event Action<int> OnValueChange;
  116. /// <summary>
  117. /// 保留小数位数
  118. /// </summary>
  119. public int DecimalPalces { get; set; } = -1;
  120. public string Caption
  121. {
  122. get { return lblCaption.Text; }
  123. set {
  124. if (!value.Contains(":"))
  125. value = value + ":";
  126. lblCaption.Text = value; }
  127. }
  128. /// <summary>
  129. /// 值为浮点数
  130. /// </summary>
  131. public bool IsFloat
  132. {
  133. get; set;
  134. } = false;
  135. public int Value
  136. {
  137. get
  138. {
  139. try { int value = int.Parse(txbValue.Text); return value; } catch (Exception ex) { MessageBox.Show(ex.Message); return 0; }
  140. }
  141. set
  142. {
  143. txbValue.Text = value.ToString();
  144. SetBarValue(value);
  145. }
  146. }
  147. #region Bar
  148. void SetBarValue(int value)
  149. {
  150. tkbValue.Value = Math.Min(tkbValue.Maximum, Math.Max(tkbValue.Minimum, value));
  151. }
  152. private void tkbValue_Scroll(object sender, EventArgs e)
  153. {
  154. var value = tkbValue.Value;
  155. txbValue.Text = value.ToString();
  156. OnValueChange?.Invoke(value);
  157. }
  158. #endregion
  159. #region max/min
  160. bool _rangeVisible = true;
  161. public bool IsRangeVisible
  162. {
  163. get => _rangeVisible; set
  164. {
  165. _rangeVisible = value;
  166. lblMax.Visible = value;
  167. lblMin.Visible = value;
  168. if (value)
  169. {
  170. this.Height = 41;
  171. }
  172. else
  173. {
  174. this.Height = 26;
  175. }
  176. }
  177. }
  178. int _max = 100;
  179. int _min = 0;
  180. public int Max
  181. {
  182. get => _max;
  183. set
  184. {
  185. _max = value;
  186. tkbValue.Maximum = value;
  187. lblMax.Text = value.ToString();
  188. }
  189. }
  190. public int Min
  191. {
  192. get => _min;
  193. set
  194. {
  195. _min = value;
  196. tkbValue.Minimum = value;
  197. lblMin.Text = value.ToString();
  198. }
  199. }
  200. public void SetBarRange(int min, int max)
  201. {
  202. tkbValue.Minimum = min;
  203. tkbValue.Maximum = max;
  204. }
  205. #endregion
  206. private void txbValue_KeyPress(object sender, KeyPressEventArgs e)
  207. {
  208. if (e.KeyChar == (char)Keys.Enter)
  209. {
  210. var value = Value;
  211. value = Math.Min(Math.Max(Min, value), value);
  212. Value = value;
  213. OnValueChange?.Invoke(value);
  214. }
  215. else if (!char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Delete)) // 非数字键, 放弃该输入
  216. {
  217. e.Handled = true;
  218. }
  219. else if (e.KeyChar == (char)Keys.Delete)
  220. {
  221. if (!IsFloat
  222. || txbValue.Text.Contains("."))
  223. e.Handled = true;
  224. }
  225. }
  226. }
  227. }