| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 | 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<int> OnValueChange;        /// <summary>        /// 保留小数位数        /// </summary>        public int DecimalPalces { get; set; } = -1;        public string Caption        {            get { return lblCaption.Text; }            set {                if (!value.Contains(":"))                    value = value + ":";                lblCaption.Text = value; }        }        /// <summary>        /// 值为浮点数        /// </summary>        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;            }        }    }}
 |