using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PaintDotNet.Preview2 { public class CameraParamBar2 : UserControl { #region Designer private TextBox txbValue; private Panel panel1; private Label lblMin; private Label lblMax; public System.Windows.Forms.TrackBar tkbValue; private Label lblCaption; public CameraParamBar2() { InitializeComponent(); } private void InitializeComponent() { this.lblCaption = 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(0, 0); this.lblCaption.Name = "lblCaption"; this.lblCaption.Padding = new System.Windows.Forms.Padding(0, 2, 0, 0); this.lblCaption.Size = new System.Drawing.Size(69, 32); this.lblCaption.TabIndex = 0; this.lblCaption.Text = "ParmName"; // // txbValue // this.txbValue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.txbValue.Dock = System.Windows.Forms.DockStyle.Right; this.txbValue.Location = new System.Drawing.Point(487, 0); this.txbValue.Name = "txbValue"; this.txbValue.Size = new System.Drawing.Size(40, 21); this.txbValue.TabIndex = 4; this.txbValue.Text = "0"; this.txbValue.WordWrap = false; 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(69, 0); this.panel1.Name = "panel1"; this.panel1.Padding = new System.Windows.Forms.Padding(0, 0, 4, 0); this.panel1.Size = new System.Drawing.Size(418, 32); 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(383, 18); 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(3, 18); this.lblMin.Name = "lblMin"; this.lblMin.Size = new System.Drawing.Size(11, 12); this.lblMin.TabIndex = 1; this.lblMin.Text = "0"; // // tkbValue // this.tkbValue.AutoSize = false; this.tkbValue.Dock = System.Windows.Forms.DockStyle.Top; this.tkbValue.Location = new System.Drawing.Point(0, 0); this.tkbValue.Name = "tkbValue"; this.tkbValue.Size = new System.Drawing.Size(414, 20); this.tkbValue.TabIndex = 3; this.tkbValue.TabStop = false; this.tkbValue.Text = "metroTrackBar1"; this.tkbValue.TickStyle = System.Windows.Forms.TickStyle.None; this.tkbValue.Scroll += new System.EventHandler(this.tkbValue_Scroll); // // CameraParamBar2 // this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.Controls.Add(this.panel1); this.Controls.Add(this.txbValue); this.Controls.Add(this.lblCaption); this.Name = "CameraParamBar2"; this.Size = new System.Drawing.Size(527, 32); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.tkbValue)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion public event Action OnValueChange; /// /// 保留小数位数 /// public int DecimalPalces { get; set; } = -1; public string Caption { get { return lblCaption.Text; } set { if (!value.Contains(":")) value = value + ":"; lblCaption.Text = value; } } /// /// 值为浮点数 /// public bool IsFloat { get; set; } = false; public int Value { get { try { int value = int.Parse(txbValue.Text); return value; } catch (Exception ex) { MessageBox.Show(ex.Message); return 0; } } set { txbValue.Text = value.ToString(); SetBarValue(value); } } #region Bar void SetBarValue(int value) { tkbValue.Value = Math.Min(tkbValue.Maximum, Math.Max(tkbValue.Minimum, value)); } private void tkbValue_Scroll(object sender, EventArgs e) { var value = tkbValue.Value; txbValue.Text = value.ToString(); OnValueChange?.Invoke(value); } #endregion #region max/min bool _rangeVisible = true; public bool IsRangeVisible { get => _rangeVisible; set { _rangeVisible = value; lblMax.Visible = value; lblMin.Visible = value; if (value) { this.Height = 41; } else { this.Height = 26; } } } int _max = 100; int _min = 0; public int Max { get => _max; set { _max = value; tkbValue.Maximum = value; lblMax.Text = value.ToString(); } } public int Min { get => _min; set { _min = value; tkbValue.Minimum = value; lblMin.Text = value.ToString(); } } public void SetBarRange(int min, int max) { tkbValue.Minimum = min; tkbValue.Maximum = max; } #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; } else if (e.KeyChar == (char)Keys.Delete) { if (!IsFloat || txbValue.Text.Contains(".")) e.Handled = true; } } } }