using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PaintDotNet.ImageCollect { public class CameraParamBar : UserControl { #region Designer private Label label1; private Label lblUnit; private TextBox txbValue; private Panel panel1; private TrackBar tkbValue; private Label lblMin; private Label lblMax; private Label lblCaption; public CameraParamBar() { InitializeComponent(); Dock = DockStyle.Top; SizeChanged += (s1, e1) => { this.Height = 44; }; } private void InitializeComponent() { this.lblCaption = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.lblUnit = new System.Windows.Forms.Label(); this.txbValue = new System.Windows.Forms.TextBox(); this.panel1 = new System.Windows.Forms.Panel(); this.lblMax = new System.Windows.Forms.Label(); this.lblMin = new System.Windows.Forms.Label(); this.tkbValue = new System.Windows.Forms.TrackBar(); this.panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.tkbValue)).BeginInit(); this.SuspendLayout(); // // lblCaption // this.lblCaption.Dock = System.Windows.Forms.DockStyle.Left; this.lblCaption.Location = new System.Drawing.Point(2, 2); this.lblCaption.Name = "lblCaption"; this.lblCaption.Padding = new System.Windows.Forms.Padding(0, 4, 0, 0); this.lblCaption.Size = new System.Drawing.Size(74, 40); this.lblCaption.TabIndex = 0; this.lblCaption.Text = "ParmName"; // // label1 // this.label1.Dock = System.Windows.Forms.DockStyle.Left; this.label1.Location = new System.Drawing.Point(76, 2); this.label1.Name = "label1"; this.label1.Padding = new System.Windows.Forms.Padding(0, 4, 0, 0); this.label1.Size = new System.Drawing.Size(11, 40); this.label1.TabIndex = 1; this.label1.Text = ":"; // // lblUnit // this.lblUnit.Dock = System.Windows.Forms.DockStyle.Right; this.lblUnit.Location = new System.Drawing.Point(485, 2); this.lblUnit.Name = "lblUnit"; this.lblUnit.Padding = new System.Windows.Forms.Padding(0, 4, 0, 0); this.lblUnit.Size = new System.Drawing.Size(40, 40); this.lblUnit.TabIndex = 3; this.lblUnit.Text = "unit"; // // txbValue // this.txbValue.Dock = System.Windows.Forms.DockStyle.Right; this.txbValue.Location = new System.Drawing.Point(425, 2); this.txbValue.Name = "txbValue"; this.txbValue.Size = new System.Drawing.Size(60, 21); this.txbValue.TabIndex = 4; this.txbValue.Text = "0"; this.txbValue.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txbValue_KeyPress); // // panel1 // this.panel1.Controls.Add(this.lblMax); this.panel1.Controls.Add(this.lblMin); this.panel1.Controls.Add(this.tkbValue); this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(87, 2); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(338, 40); this.panel1.TabIndex = 5; // // lblMax // this.lblMax.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.lblMax.Location = new System.Drawing.Point(298, 24); this.lblMax.Name = "lblMax"; this.lblMax.Size = new System.Drawing.Size(35, 12); this.lblMax.TabIndex = 2; this.lblMax.Text = "10000"; this.lblMax.TextAlign = System.Drawing.ContentAlignment.TopRight; // // lblMin // this.lblMin.AutoSize = true; this.lblMin.Location = new System.Drawing.Point(9, 24); this.lblMin.Name = "lblMin"; this.lblMin.Size = new System.Drawing.Size(11, 12); this.lblMin.TabIndex = 1; this.lblMin.Text = "1"; // // tkbValue // this.tkbValue.Dock = System.Windows.Forms.DockStyle.Fill; this.tkbValue.Location = new System.Drawing.Point(0, 0); this.tkbValue.Maximum = 100; this.tkbValue.Name = "tkbValue"; this.tkbValue.Size = new System.Drawing.Size(338, 40); this.tkbValue.TabIndex = 0; this.tkbValue.TickStyle = System.Windows.Forms.TickStyle.None; this.tkbValue.Scroll += new System.EventHandler(this.tkbValue_Scroll); // // CameraParamBar // this.AutoSize = true; this.Controls.Add(this.panel1); this.Controls.Add(this.txbValue); this.Controls.Add(this.lblUnit); this.Controls.Add(this.label1); this.Controls.Add(this.lblCaption); this.Name = "CameraParamBar"; this.Padding = new System.Windows.Forms.Padding(2); this.Size = new System.Drawing.Size(527, 44); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.tkbValue)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion public event Action OnValueChange; public string Caption { get { return lblCaption.Text; } set { lblCaption.Text = value; } } public string unit { get => lblUnit.Text; set => lblUnit.Text = value; } public int Value { get { try { int value = int.Parse(txbValue.Text); return value; } catch (Exception ex) { MessageBox.Show(ex.Message); return 0; } } set { tkbValue.Value = Math.Max(tkbValue.Minimum, Math.Min(value, tkbValue.Maximum)); txbValue.Text = value.ToString(); } } #region Bar private void tkbValue_Scroll(object sender, EventArgs e) { var value = tkbValue.Value; txbValue.Text = value.ToString(); OnValueChange?.Invoke(Value); } #endregion #region max/min public int Max { get => tkbValue.Maximum; set { tkbValue.Maximum = value; lblMax.Text = value.ToString(); } } public int Min { get => tkbValue.Minimum; set { tkbValue.Minimum = value; ; lblMin.Text = value.ToString(); } } #endregion private void txbValue_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { var value = Value; value = Math.Min(Math.Max(Min, value), value); Value = value; OnValueChange?.Invoke(value); } else if (!char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Delete)) // 非数字键, 放弃该输入 { e.Handled = true; } } } }