NumberParamControl.cs 5.7 KB

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