UCHScrollbar.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace HZH_Controls.Controls
  9. {
  10. [Designer(typeof(ScrollbarControlDesigner))]
  11. [DefaultEvent("Scroll")]
  12. public class UCHScrollbar : UCControlBase
  13. {
  14. #region 属性 English:attribute
  15. /// <summary>
  16. /// The mo large change
  17. /// </summary>
  18. protected int moLargeChange = 10;
  19. /// <summary>
  20. /// The mo small change
  21. /// </summary>
  22. protected int moSmallChange = 1;
  23. /// <summary>
  24. /// The mo minimum
  25. /// </summary>
  26. protected int moMinimum = 0;
  27. /// <summary>
  28. /// The mo maximum
  29. /// </summary>
  30. protected int moMaximum = 100;
  31. /// <summary>
  32. /// The mo value
  33. /// </summary>
  34. protected int moValue = 0;
  35. /// <summary>
  36. /// The n click point
  37. /// </summary>
  38. private int nClickPoint;
  39. /// <summary>
  40. /// The mo thumb top
  41. /// </summary>
  42. protected int moThumbLeft = 0;
  43. /// <summary>
  44. /// The mo automatic size
  45. /// </summary>
  46. protected bool moAutoSize = false;
  47. /// <summary>
  48. /// The mo thumb down
  49. /// </summary>
  50. private bool moThumbMouseDown = false;
  51. /// <summary>
  52. /// The mo thumb dragging
  53. /// </summary>
  54. private bool moThumbMouseDragging = false;
  55. /// <summary>
  56. /// Occurs when [scroll].
  57. /// </summary>
  58. public new event EventHandler Scroll = null;
  59. /// <summary>
  60. /// Occurs when [value changed].
  61. /// </summary>
  62. public event EventHandler ValueChanged = null;
  63. /// <summary>
  64. /// The BTN height
  65. /// </summary>
  66. private int btnWidth = 18;
  67. /// <summary>
  68. /// The m int thumb minimum height
  69. /// </summary>
  70. private int m_intThumbMinWidth = 15;
  71. /// <summary>
  72. /// Gets or sets the height of the BTN.
  73. /// </summary>
  74. /// <value>The height of the BTN.</value>
  75. public int BtnWidth
  76. {
  77. get { return btnWidth; }
  78. set { btnWidth = value; }
  79. }
  80. /// <summary>
  81. /// Gets or sets the large change.
  82. /// </summary>
  83. /// <value>The large change.</value>
  84. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定义"), Description("LargeChange")]
  85. public int LargeChange
  86. {
  87. get { return moLargeChange; }
  88. set
  89. {
  90. moLargeChange = value;
  91. Invalidate();
  92. }
  93. }
  94. /// <summary>
  95. /// Gets or sets the small change.
  96. /// </summary>
  97. /// <value>The small change.</value>
  98. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定义"), Description("SmallChange")]
  99. public int SmallChange
  100. {
  101. get { return moSmallChange; }
  102. set
  103. {
  104. moSmallChange = value;
  105. Invalidate();
  106. }
  107. }
  108. /// <summary>
  109. /// Gets or sets the minimum.
  110. /// </summary>
  111. /// <value>The minimum.</value>
  112. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定义"), Description("Minimum")]
  113. public int Minimum
  114. {
  115. get { return moMinimum; }
  116. set
  117. {
  118. moMinimum = value;
  119. Invalidate();
  120. }
  121. }
  122. /// <summary>
  123. /// Gets or sets the maximum.
  124. /// </summary>
  125. /// <value>The maximum.</value>
  126. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定义"), Description("Maximum")]
  127. public int Maximum
  128. {
  129. get { return moMaximum; }
  130. set
  131. {
  132. moMaximum = value;
  133. Invalidate();
  134. }
  135. }
  136. /// <summary>
  137. /// Gets or sets the value.
  138. /// </summary>
  139. /// <value>The value.</value>
  140. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定义"), Description("Value")]
  141. public int Value
  142. {
  143. get { return moValue; }
  144. set
  145. {
  146. moValue = value;
  147. int nTrackWidth = (this.Width - btnWidth * 2);
  148. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  149. int nThumbWidth = (int)fThumbWidth;
  150. if (nThumbWidth > nTrackWidth)
  151. {
  152. nThumbWidth = nTrackWidth;
  153. fThumbWidth = nTrackWidth;
  154. }
  155. if (nThumbWidth < m_intThumbMinWidth)
  156. {
  157. nThumbWidth = m_intThumbMinWidth;
  158. fThumbWidth = m_intThumbMinWidth;
  159. }
  160. //figure out value
  161. int nPixelRange = nTrackWidth - nThumbWidth;
  162. int nRealRange = (Maximum - Minimum) - LargeChange;
  163. float fPerc = 0.0f;
  164. if (nRealRange != 0)
  165. {
  166. fPerc = (float)moValue / (float)nRealRange;
  167. }
  168. float fLeft = fPerc * nPixelRange;
  169. moThumbLeft = (int)fLeft;
  170. Invalidate();
  171. }
  172. }
  173. /// <summary>
  174. /// Gets or sets a value indicating whether [automatic size].
  175. /// </summary>
  176. /// <value><c>true</c> if [automatic size]; otherwise, <c>false</c>.</value>
  177. public override bool AutoSize
  178. {
  179. get
  180. {
  181. return base.AutoSize;
  182. }
  183. set
  184. {
  185. base.AutoSize = value;
  186. if (base.AutoSize)
  187. {
  188. this.Width = 15;
  189. }
  190. }
  191. }
  192. /// <summary>
  193. /// The thumb color
  194. /// </summary>
  195. private Color thumbColor = Color.FromArgb(255, 77, 58);
  196. /// <summary>
  197. /// Gets or sets the color of the thumb.
  198. /// </summary>
  199. /// <value>The color of the thumb.</value>
  200. public Color ThumbColor
  201. {
  202. get { return thumbColor; }
  203. set { thumbColor = value; }
  204. }
  205. #endregion
  206. public UCHScrollbar()
  207. {
  208. InitializeComponent();
  209. ConerRadius = 2;
  210. FillColor = Color.FromArgb(239, 239, 239);
  211. IsShowRect = false;
  212. IsRadius = true;
  213. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  214. this.SetStyle(ControlStyles.DoubleBuffer, true);
  215. this.SetStyle(ControlStyles.ResizeRedraw, true);
  216. this.SetStyle(ControlStyles.Selectable, true);
  217. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  218. this.SetStyle(ControlStyles.UserPaint, true);
  219. }
  220. /// <summary>
  221. /// Initializes the component.
  222. /// </summary>
  223. private void InitializeComponent()
  224. {
  225. this.SuspendLayout();
  226. this.MinimumSize = new System.Drawing.Size(0, 10);
  227. this.Name = "UCHScrollbar";
  228. this.Size = new System.Drawing.Size(150, 18);
  229. this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseDown);
  230. this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseMove);
  231. this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseUp);
  232. this.ResumeLayout(false);
  233. }
  234. #region 鼠标事件 English:Mouse event
  235. /// <summary>
  236. /// Handles the MouseDown event of the CustomScrollbar control.
  237. /// </summary>
  238. /// <param name="sender">The source of the event.</param>
  239. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  240. private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e)
  241. {
  242. Point ptPoint = this.PointToClient(Cursor.Position);
  243. int nTrackWidth = (this.Width - btnWidth * 2);
  244. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  245. int nThumbWidth = (int)fThumbWidth;
  246. if (nThumbWidth > nTrackWidth)
  247. {
  248. nThumbWidth = nTrackWidth;
  249. fThumbWidth = nTrackWidth;
  250. }
  251. if (nThumbWidth < m_intThumbMinWidth)
  252. {
  253. nThumbWidth = m_intThumbMinWidth;
  254. fThumbWidth = m_intThumbMinWidth;
  255. }
  256. int nLeft = moThumbLeft;
  257. nLeft += btnWidth;
  258. Rectangle thumbrect = new Rectangle(new Point(nLeft, 1), new Size(nThumbWidth, this.Height - 2));
  259. //滑块
  260. if (thumbrect.Contains(ptPoint))
  261. {
  262. //hit the thumb
  263. nClickPoint = (ptPoint.X - nLeft);
  264. this.moThumbMouseDown = true;
  265. }
  266. else
  267. {
  268. //左按钮
  269. Rectangle leftarrowrect = new Rectangle(new Point(0, 1), new Size(btnWidth, this.Height));
  270. if (leftarrowrect.Contains(ptPoint))
  271. {
  272. int nRealRange = (Maximum - Minimum) - LargeChange;
  273. int nPixelRange = (nTrackWidth - nThumbWidth);
  274. if (nRealRange > 0)
  275. {
  276. if (nPixelRange > 0)
  277. {
  278. if ((moThumbLeft - SmallChange) < 0)
  279. moThumbLeft = 0;
  280. else
  281. moThumbLeft -= SmallChange;
  282. //figure out value
  283. float fPerc = (float)moThumbLeft / (float)nPixelRange;
  284. float fValue = fPerc * (Maximum - LargeChange);
  285. moValue = (int)fValue;
  286. if (ValueChanged != null)
  287. ValueChanged(this, new EventArgs());
  288. if (Scroll != null)
  289. Scroll(this, new EventArgs());
  290. Invalidate();
  291. }
  292. }
  293. }
  294. else
  295. {
  296. Rectangle rightarrowrect = new Rectangle(new Point(btnWidth + nTrackWidth, 1), new Size(btnWidth, this.Height));
  297. if (rightarrowrect.Contains(ptPoint))
  298. {
  299. int nRealRange = (Maximum - Minimum) - LargeChange;
  300. int nPixelRange = (nTrackWidth - nThumbWidth);
  301. if (nRealRange > 0)
  302. {
  303. if (nPixelRange > 0)
  304. {
  305. if ((moThumbLeft + SmallChange) > nPixelRange)
  306. moThumbLeft = nPixelRange;
  307. else
  308. moThumbLeft += SmallChange;
  309. //figure out value
  310. float fPerc = (float)moThumbLeft / (float)nPixelRange;
  311. float fValue = fPerc * (Maximum - LargeChange);
  312. moValue = (int)fValue;
  313. if (ValueChanged != null)
  314. ValueChanged(this, new EventArgs());
  315. if (Scroll != null)
  316. Scroll(this, new EventArgs());
  317. Invalidate();
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. /// <summary>
  325. /// Handles the MouseUp event of the CustomScrollbar control.
  326. /// </summary>
  327. /// <param name="sender">The source of the event.</param>
  328. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  329. private void CustomScrollbar_MouseUp(object sender, MouseEventArgs e)
  330. {
  331. this.moThumbMouseDown = false;
  332. this.moThumbMouseDragging = false;
  333. }
  334. /// <summary>
  335. /// Moves the thumb.
  336. /// </summary>
  337. /// <param name="x">The y.</param>
  338. private void MoveThumb(int x)
  339. {
  340. int nRealRange = Maximum - Minimum;
  341. int nTrackWidth = (this.Width - btnWidth * 2);
  342. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  343. int nThumbWidth = (int)fThumbWidth;
  344. if (nThumbWidth > nTrackWidth)
  345. {
  346. nThumbWidth = nTrackWidth;
  347. fThumbWidth = nTrackWidth;
  348. }
  349. if (nThumbWidth < m_intThumbMinWidth)
  350. {
  351. nThumbWidth = m_intThumbMinWidth;
  352. fThumbWidth = m_intThumbMinWidth;
  353. }
  354. int nSpot = nClickPoint;
  355. int nPixelRange = (nTrackWidth - nThumbWidth);
  356. if (moThumbMouseDown && nRealRange > 0)
  357. {
  358. if (nPixelRange > 0)
  359. {
  360. int nNewThumbLeft = x - (btnWidth + nSpot);
  361. if (nNewThumbLeft < 0)
  362. {
  363. moThumbLeft = nNewThumbLeft = 0;
  364. }
  365. else if (nNewThumbLeft > nPixelRange)
  366. {
  367. moThumbLeft = nNewThumbLeft = nPixelRange;
  368. }
  369. else
  370. {
  371. moThumbLeft = x - (btnWidth + nSpot);
  372. }
  373. float fPerc = (float)moThumbLeft / (float)nPixelRange;
  374. float fValue = fPerc * (Maximum - LargeChange);
  375. moValue = (int)fValue;
  376. Application.DoEvents();
  377. Invalidate();
  378. }
  379. }
  380. }
  381. /// <summary>
  382. /// Handles the MouseMove event of the CustomScrollbar control.
  383. /// </summary>
  384. /// <param name="sender">The source of the event.</param>
  385. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  386. private void CustomScrollbar_MouseMove(object sender, MouseEventArgs e)
  387. {
  388. if (!moThumbMouseDown)
  389. return;
  390. if (moThumbMouseDown == true)
  391. {
  392. this.moThumbMouseDragging = true;
  393. }
  394. if (this.moThumbMouseDragging)
  395. {
  396. MoveThumb(e.X);
  397. }
  398. if (ValueChanged != null)
  399. ValueChanged(this, new EventArgs());
  400. if (Scroll != null)
  401. Scroll(this, new EventArgs());
  402. }
  403. #endregion
  404. protected override void OnPaint(PaintEventArgs e)
  405. {
  406. base.OnPaint(e);
  407. e.Graphics.SetGDIHigh();
  408. //draw thumb
  409. int nTrackWidth = (this.Width - btnWidth * 2);
  410. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  411. int nThumbWidth = (int)fThumbWidth;
  412. if (nThumbWidth > nTrackWidth)
  413. {
  414. nThumbWidth = nTrackWidth;
  415. fThumbWidth = nTrackWidth;
  416. }
  417. if (nThumbWidth < m_intThumbMinWidth)
  418. {
  419. nThumbWidth = m_intThumbMinWidth;
  420. fThumbWidth = m_intThumbMinWidth;
  421. }
  422. int nLeft = moThumbLeft;
  423. nLeft += btnWidth;
  424. e.Graphics.FillPath(new SolidBrush(thumbColor), new Rectangle(nLeft, 1, nThumbWidth, this.Height - 3).CreateRoundedRectanglePath(this.ConerRadius));
  425. ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(thumbColor), new Point(btnWidth - Math.Min(5, this.Height / 2), this.Height / 2), Math.Min(5, this.Height / 2), GraphDirection.Leftward);
  426. ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(thumbColor), new Point(this.Width - (btnWidth - Math.Min(5, this.Height / 2)), this.Height / 2), Math.Min(5, this.Height / 2), GraphDirection.Rightward);
  427. }
  428. }
  429. }