using System; using System.Windows.Forms; namespace PaintDotNet.CustomControl { public partial class NumberParamControl : UserControl { /// /// 参数的index /// public int paramIndex; /// /// 参数值改变 /// public event EventHandler ValueChanged; /// /// 是否是奇数类型的参数 /// public bool isOddNumber; /// /// 设置奇数类型 /// public bool IsOddNumber { //get //{ // return this.isOddNumber; //} set { this.isOddNumber = value; if (this.isOddNumber) { this.sliderR.Maximum = (int)(this.sliderR.Maximum - this.sliderR.Minimum) / 2 + this.sliderR.Minimum; } } } /// /// 十进制位数 /// public int DecimalPlaces { get { return this.numericUpDownR.DecimalPlaces; } set { this.numericUpDownR.DecimalPlaces = value; this.numericUpDownR.Increment = (decimal)Math.Pow(0.1, DecimalPlaces); } } public int Increment { set { this.sliderR.TickFrequency = value; this.numericUpDownR.Increment = value; } } /// /// 根据十进制位数获取slider的放大倍数 /// public double magnification { get { return Math.Pow(10, DecimalPlaces); } } public double Maximum { //get //{ // return this.sliderR.Maximum / magnification; //} set { if (this.isOddNumber) { this.sliderR.Maximum = (int)(value * magnification - this.sliderR.Minimum) / 2 + this.sliderR.Minimum; } else { this.sliderR.Maximum = (int)(value * magnification); } this.numericUpDownR.Maximum = (Decimal)value; } } public double Minimum { get { return this.sliderR.Minimum / magnification; } set { this.sliderR.Minimum = (int)(value * magnification); this.numericUpDownR.Minimum = (decimal)value; } } public double Value { get { if (this.isOddNumber) { return (double)(((this.sliderR.Value / magnification) - this.sliderR.Minimum) * 2 + this.sliderR.Minimum);// ((this.sliderR.Value / magnification) - this.sliderR.Minimum) / 2 + this.sliderR.Minimum; } else { return (double)(this.sliderR.Value / magnification); } } set { if (this.isOddNumber) { this.sliderR.Value = ((int)(value * magnification) - this.sliderR.Minimum) / 2 + this.sliderR.Minimum; } else { this.sliderR.Value = (int)(value * magnification); } this.numericUpDownR.Value = (decimal)value; } } public NumberParamControl() { InitializeComponent(); ResetUIRanges(); } protected void ResetUIRanges() { this.sliderR.Minimum = 0; if (this.isOddNumber) { this.sliderR.Maximum = (int)(255 - this.sliderR.Minimum) / 2 + this.sliderR.Minimum; } else { this.sliderR.Maximum = 255; } this.sliderR.SmallChange = 1; this.numericUpDownR.Minimum = 0; this.numericUpDownR.Maximum = 255; this.numericUpDownR.Increment = 1; this.numericUpDownR.DecimalPlaces = 0; } private void sliderR_Scroll(object sender, EventArgs e) { if (this.isOddNumber) { numericUpDownR.Value = (decimal)(((this.sliderR.Value / magnification) - this.sliderR.Minimum) * 2 + this.sliderR.Minimum); } else { numericUpDownR.Value = (decimal)(sliderR.Value / magnification); } if (this.ValueChanged != null) { this.ValueChanged(this, e); } } private void numericUpDownR_ValueChanged(object sender, EventArgs e) { if (this.isOddNumber) { if (numericUpDownR.Value % 2 == 0) { numericUpDownR.Value -= 1; return; } sliderR.Value = ((int)(numericUpDownR.Value * (decimal)magnification) - this.sliderR.Minimum) / 2 + this.sliderR.Minimum; } else { sliderR.Value = (int)(numericUpDownR.Value * (decimal)magnification); } if (this.ValueChanged != null) { this.ValueChanged(this, e); } } } }