NumberParamControl.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Windows.Forms;
  3. namespace PaintDotNet.CustomControl
  4. {
  5. public partial class NumberParamControl : UserControl
  6. {
  7. /// <summary>
  8. /// 参数的index
  9. /// </summary>
  10. public int paramIndex;
  11. /// <summary>
  12. /// 参数值改变
  13. /// </summary>
  14. public event EventHandler ValueChanged;
  15. /// <summary>
  16. /// 是否是奇数类型的参数
  17. /// </summary>
  18. public bool isOddNumber;
  19. /// <summary>
  20. /// 设置奇数类型
  21. /// </summary>
  22. public bool IsOddNumber
  23. {
  24. //get
  25. //{
  26. // return this.isOddNumber;
  27. //}
  28. set
  29. {
  30. this.isOddNumber = value;
  31. if (this.isOddNumber)
  32. {
  33. this.sliderR.Maximum = (int)(this.sliderR.Maximum - this.sliderR.Minimum) / 2 + this.sliderR.Minimum;
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// 十进制位数
  39. /// </summary>
  40. public int DecimalPlaces
  41. {
  42. get
  43. {
  44. return this.numericUpDownR.DecimalPlaces;
  45. }
  46. set
  47. {
  48. this.numericUpDownR.DecimalPlaces = value;
  49. this.numericUpDownR.Increment = (decimal)Math.Pow(0.1, DecimalPlaces);
  50. }
  51. }
  52. public int Increment
  53. {
  54. set
  55. {
  56. this.sliderR.TickFrequency = value;
  57. this.numericUpDownR.Increment = value;
  58. }
  59. }
  60. /// <summary>
  61. /// 根据十进制位数获取slider的放大倍数
  62. /// </summary>
  63. public double magnification
  64. {
  65. get
  66. {
  67. return Math.Pow(10, DecimalPlaces);
  68. }
  69. }
  70. public double Maximum
  71. {
  72. //get
  73. //{
  74. // return this.sliderR.Maximum / magnification;
  75. //}
  76. set
  77. {
  78. if (this.isOddNumber)
  79. {
  80. this.sliderR.Maximum = (int)(value * magnification - this.sliderR.Minimum) / 2 + this.sliderR.Minimum;
  81. }
  82. else
  83. {
  84. this.sliderR.Maximum = (int)(value * magnification);
  85. }
  86. this.numericUpDownR.Maximum = (Decimal)value;
  87. }
  88. }
  89. public double Minimum
  90. {
  91. get
  92. {
  93. return this.sliderR.Minimum / magnification;
  94. }
  95. set
  96. {
  97. this.sliderR.Minimum = (int)(value * magnification);
  98. this.numericUpDownR.Minimum = (decimal)value;
  99. }
  100. }
  101. public double Value
  102. {
  103. get
  104. {
  105. if (this.isOddNumber)
  106. {
  107. return (double)(((this.sliderR.Value / magnification) - this.sliderR.Minimum) * 2 + this.sliderR.Minimum);// ((this.sliderR.Value / magnification) - this.sliderR.Minimum) / 2 + this.sliderR.Minimum;
  108. }
  109. else
  110. {
  111. return (double)(this.sliderR.Value / magnification);
  112. }
  113. }
  114. set
  115. {
  116. if (this.isOddNumber)
  117. {
  118. this.sliderR.Value = ((int)(value * magnification) - this.sliderR.Minimum) / 2 + this.sliderR.Minimum;
  119. }
  120. else
  121. {
  122. this.sliderR.Value = (int)(value * magnification);
  123. }
  124. this.numericUpDownR.Value = (decimal)value;
  125. }
  126. }
  127. public NumberParamControl()
  128. {
  129. InitializeComponent();
  130. ResetUIRanges();
  131. }
  132. protected void ResetUIRanges()
  133. {
  134. this.sliderR.Minimum = 0;
  135. if (this.isOddNumber)
  136. {
  137. this.sliderR.Maximum = (int)(255 - this.sliderR.Minimum) / 2 + this.sliderR.Minimum;
  138. }
  139. else
  140. {
  141. this.sliderR.Maximum = 255;
  142. }
  143. this.sliderR.SmallChange = 1;
  144. this.numericUpDownR.Minimum = 0;
  145. this.numericUpDownR.Maximum = 255;
  146. this.numericUpDownR.Increment = 1;
  147. this.numericUpDownR.DecimalPlaces = 0;
  148. }
  149. private void sliderR_Scroll(object sender, EventArgs e)
  150. {
  151. if (this.isOddNumber)
  152. {
  153. numericUpDownR.Value = (decimal)(((this.sliderR.Value / magnification) - this.sliderR.Minimum) * 2 + this.sliderR.Minimum);
  154. }
  155. else
  156. {
  157. numericUpDownR.Value = (decimal)(sliderR.Value / magnification);
  158. }
  159. if (this.ValueChanged != null)
  160. {
  161. this.ValueChanged(this, e);
  162. }
  163. }
  164. private void numericUpDownR_ValueChanged(object sender, EventArgs e)
  165. {
  166. if (this.isOddNumber)
  167. {
  168. if (numericUpDownR.Value % 2 == 0)
  169. {
  170. numericUpDownR.Value -= 1;
  171. return;
  172. }
  173. sliderR.Value = ((int)(numericUpDownR.Value * (decimal)magnification) - this.sliderR.Minimum) / 2 + this.sliderR.Minimum;
  174. }
  175. else
  176. {
  177. sliderR.Value = (int)(numericUpDownR.Value * (decimal)magnification);
  178. }
  179. if (this.ValueChanged != null)
  180. {
  181. this.ValueChanged(this, e);
  182. }
  183. }
  184. }
  185. }