123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using System;
- using System.Windows.Forms;
- namespace PaintDotNet.CustomControl
- {
- public partial class NumberParamControl : UserControl
- {
- /// <summary>
- /// 参数的index
- /// </summary>
- public int paramIndex;
- /// <summary>
- /// 参数值改变
- /// </summary>
- public event EventHandler ValueChanged;
- /// <summary>
- /// 是否是奇数类型的参数
- /// </summary>
- public bool isOddNumber;
- /// <summary>
- /// 设置奇数类型
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// 十进制位数
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 根据十进制位数获取slider的放大倍数
- /// </summary>
- 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);
- }
- }
- }
- }
|