UCtlScroolBarH.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Windows.Forms.Design;
  9. using System.Diagnostics;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. [Designer(typeof(ScrollbarControlDesigner))]
  13. public class CtlScrollBarH : UserControl
  14. {
  15. protected Color moChannelColor = Color.Empty;
  16. protected Image moUpArrowImage = null;
  17. protected Image moDownArrowImage = null;
  18. protected Image moThumbArrowImage = null;
  19. protected Image moThumbTopImage = null;
  20. protected Image moThumbTopSpanImage = null;
  21. protected Image moThumbBottomImage = null;
  22. protected Image moThumbBottomSpanImage = null;
  23. protected Image moThumbMiddleImage = null;
  24. protected int moLargeChange = 10;
  25. protected int moSmallChange = 1;
  26. protected int moMinimum = 0;
  27. protected int moMaximum = 100;
  28. protected int moValue = 0;
  29. private int nClickPoint;
  30. protected int moThumbTop = 0;
  31. protected bool moAutoSize = false;
  32. private bool moThumbDown = false;
  33. private bool moThumbDragging = false;
  34. public new event EventHandler Scroll = null;
  35. public event EventHandler ValueChanged = null;
  36. private int GetThumbWidth()
  37. {
  38. int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
  39. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  40. int nThumbWidth = (int)fThumbWidth;
  41. if (nThumbWidth > nTrackWidth)
  42. {
  43. nThumbWidth = nTrackWidth;
  44. fThumbWidth = nTrackWidth;
  45. }
  46. if (nThumbWidth < 56)
  47. {
  48. nThumbWidth = 56;
  49. fThumbWidth = 56;
  50. }
  51. return nThumbWidth;
  52. }
  53. public CtlScrollBarH()
  54. {
  55. InitializeComponent();
  56. SetStyle(ControlStyles.ResizeRedraw, true);
  57. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  58. SetStyle(ControlStyles.DoubleBuffer, true);
  59. SetImage();
  60. this.Height = UpArrowImage.Height;
  61. base.MinimumSize = new Size(86,this.Height);
  62. }
  63. void SetImage()
  64. {
  65. moChannelColor = Color.FromArgb(194, 211, 255);
  66. UpArrowImage = LYFZ.ComponentLibrary.GetUIResources.leftarrow;
  67. DownArrowImage = LYFZ.ComponentLibrary.GetUIResources.rightarrow;
  68. ThumbBottomImage = LYFZ.ComponentLibrary.GetUIResources.ThumbRight;
  69. ThumbBottomSpanImage = LYFZ.ComponentLibrary.GetUIResources.ThumbSpanRight;
  70. ThumbTopImage = LYFZ.ComponentLibrary.GetUIResources.ThumbLeft;
  71. ThumbTopSpanImage = LYFZ.ComponentLibrary.GetUIResources.ThumbSpanLeft;
  72. ThumbMiddleImage = LYFZ.ComponentLibrary.GetUIResources.ThumbMiddleH;
  73. }
  74. protected override void OnPaint(PaintEventArgs e)
  75. {
  76. SetImage();
  77. e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
  78. if (UpArrowImage != null)
  79. {
  80. //e.Graphics.DrawImage(UpArrowImage, new Rectangle(new Point(0, 0), new Size(this.Height, UpArrowImage.Width)));
  81. e.Graphics.DrawImage(UpArrowImage, new Rectangle(new Point(0, 0), new Size(UpArrowImage.Width, this.Height)));
  82. }
  83. Brush oBrush = new SolidBrush(moChannelColor);
  84. Brush oWhiteBrush = new SolidBrush(Color.FromArgb(255, 255, 255));
  85. //draw channel left and right border colors
  86. //e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(0, UpArrowImage.Width, 1, (this.Width - DownArrowImage.Width)));
  87. //e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(this.Height - 1, UpArrowImage.Width, 1, (this.Width - DownArrowImage.Width)));
  88. e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(UpArrowImage.Width, 0, (this.Width - DownArrowImage.Width), 1));
  89. e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(UpArrowImage.Width, this.Height - 1, (this.Width - DownArrowImage.Width), 1));
  90. //draw channel
  91. //e.Graphics.FillRectangle(oBrush, new Rectangle(1, UpArrowImage.Width, this.Height - 2, (this.Width - DownArrowImage.Width)));
  92. e.Graphics.FillRectangle(oBrush, new Rectangle(UpArrowImage.Width, 1, (this.Width - DownArrowImage.Width), this.Height - 2));
  93. //draw thumb
  94. int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
  95. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  96. int nThumbWidth = (int)fThumbWidth;
  97. if (nThumbWidth > nTrackWidth)
  98. {
  99. nThumbWidth = nTrackWidth;
  100. fThumbWidth = nTrackWidth;
  101. }
  102. if (nThumbWidth < 56)
  103. {
  104. nThumbWidth = 56;
  105. fThumbWidth = 56;
  106. }
  107. //Debug.WriteLine(nThumbWidth.ToString());
  108. float fSpanWidth = (fThumbWidth - (ThumbMiddleImage.Width + ThumbTopImage.Width + ThumbBottomImage.Width)) / 2.0f;
  109. int nSpanWidth = (int)fSpanWidth;
  110. int nTop = moThumbTop;
  111. nTop += UpArrowImage.Width;
  112. //draw top
  113. //e.Graphics.DrawImage(ThumbTopImage, new Rectangle(1, nTop, this.Height - 2, ThumbTopImage.Width));
  114. e.Graphics.DrawImage(ThumbTopImage, new Rectangle(nTop, 1, ThumbTopImage.Width, this.Height - 2));
  115. nTop += ThumbTopImage.Width;
  116. //draw top span
  117. Rectangle rect = new Rectangle(1, nTop, this.Height - 2, nSpanWidth);
  118. //e.Graphics.DrawImage(ThumbTopSpanImage, 1.0f, (float)nTop, (float)this.Height - 2.0f, (float)fSpanWidth * 2);
  119. e.Graphics.DrawImage(ThumbTopSpanImage, (float)nTop, 1.0f, (float)fSpanWidth * 2, (float)this.Height - 2.0f);
  120. nTop += nSpanWidth;
  121. //draw middle
  122. //e.Graphics.DrawImage(ThumbMiddleImage, new Rectangle(1, nTop, this.Height - 2, ThumbMiddleImage.Width));
  123. e.Graphics.DrawImage(ThumbMiddleImage, new Rectangle(nTop, 1, ThumbMiddleImage.Width, this.Height - 2));
  124. nTop += ThumbMiddleImage.Width;
  125. //draw top span
  126. //rect = new Rectangle(1, nTop, this.Height - 2, nSpanWidth * 2);
  127. rect = new Rectangle(nTop, 1, nSpanWidth * 2, this.Height - 2);
  128. e.Graphics.DrawImage(ThumbBottomSpanImage, rect);
  129. nTop += nSpanWidth;
  130. //draw bottom
  131. //e.Graphics.DrawImage(ThumbBottomImage, new Rectangle(1, nTop, this.Height - 2, nSpanWidth));
  132. e.Graphics.DrawImage(ThumbBottomImage, new Rectangle(nTop, 1, nSpanWidth, this.Height - 2));
  133. if (DownArrowImage != null)
  134. {
  135. //e.Graphics.DrawImage(DownArrowImage, new Rectangle(new Point(0, (this.Width - DownArrowImage.Width)), new Size(this.Height, DownArrowImage.Width)));
  136. e.Graphics.DrawImage(DownArrowImage, new Rectangle(new Point((this.Width - DownArrowImage.Width), 0), new Size(DownArrowImage.Width, this.Height)));
  137. }
  138. }
  139. public override bool AutoSize
  140. {
  141. get
  142. {
  143. return base.AutoSize;
  144. }
  145. set
  146. {
  147. base.AutoSize = value;
  148. if (base.AutoSize)
  149. {
  150. this.Height = moUpArrowImage.Height;
  151. }
  152. }
  153. }
  154. private void InitializeComponent()
  155. {
  156. this.SuspendLayout();
  157. //
  158. // CustomScrollbar
  159. //
  160. this.Name = "CustomScrollbar";
  161. this.Size = new System.Drawing.Size(200, 200);
  162. this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseDown);
  163. this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseMove);
  164. this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseUp);
  165. this.ResumeLayout(false);
  166. }
  167. private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e)
  168. {
  169. Point ptPoint = this.PointToClient(Cursor.Position);
  170. int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
  171. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  172. int nThumbWidth = (int)fThumbWidth;
  173. if (nThumbWidth > nTrackWidth)
  174. {
  175. nThumbWidth = nTrackWidth;
  176. fThumbWidth = nTrackWidth;
  177. }
  178. if (nThumbWidth < 56)
  179. {
  180. nThumbWidth = 56;
  181. fThumbWidth = 56;
  182. }
  183. int nTop = moThumbTop;
  184. nTop += UpArrowImage.Width;
  185. Rectangle thumbrect = new Rectangle(new Point(nTop,1 ), new Size(nThumbWidth,ThumbMiddleImage.Height ));
  186. if (thumbrect.Contains(ptPoint))
  187. {
  188. //hit the thumb
  189. nClickPoint = (ptPoint.X - nTop);
  190. this.moThumbDown = true;
  191. }
  192. Rectangle uparrowrect = new Rectangle(new Point(0,1 ), new Size(UpArrowImage.Width,UpArrowImage.Height ));
  193. if (uparrowrect.Contains(ptPoint))
  194. {
  195. int nRealRange = (Maximum - Minimum) - LargeChange;
  196. int nPixelRange = (nTrackWidth - nThumbWidth);
  197. if (nRealRange > 0)
  198. {
  199. if (nPixelRange > 0)
  200. {
  201. if ((moThumbTop - SmallChange) < 0)
  202. moThumbTop = 0;
  203. else
  204. moThumbTop -= SmallChange;
  205. //figure out value
  206. float fPerc = (float)moThumbTop / (float)nPixelRange;
  207. float fValue = fPerc * (Maximum - LargeChange);
  208. moValue = (int)fValue;
  209. Debug.WriteLine(moValue.ToString());
  210. if (ValueChanged != null)
  211. ValueChanged(this, new EventArgs());
  212. if (Scroll != null)
  213. Scroll(this, new EventArgs());
  214. Invalidate();
  215. }
  216. }
  217. }
  218. Rectangle downarrowrect = new Rectangle(new Point(UpArrowImage.Width + nTrackWidth,1 ), new Size(UpArrowImage.Width,UpArrowImage.Height ));
  219. if (downarrowrect.Contains(ptPoint))
  220. {
  221. int nRealRange = (Maximum - Minimum) - LargeChange;
  222. int nPixelRange = (nTrackWidth - nThumbWidth);
  223. if (nRealRange > 0)
  224. {
  225. if (nPixelRange > 0)
  226. {
  227. if ((moThumbTop + SmallChange) > nPixelRange)
  228. moThumbTop = nPixelRange;
  229. else
  230. moThumbTop += SmallChange;
  231. //figure out value
  232. float fPerc = (float)moThumbTop / (float)nPixelRange;
  233. float fValue = fPerc * (Maximum - LargeChange);
  234. moValue = (int)fValue;
  235. Debug.WriteLine(moValue.ToString());
  236. if (ValueChanged != null)
  237. ValueChanged(this, new EventArgs());
  238. if (Scroll != null)
  239. Scroll(this, new EventArgs());
  240. Invalidate();
  241. }
  242. }
  243. }
  244. }
  245. private void CustomScrollbar_MouseUp(object sender, MouseEventArgs e)
  246. {
  247. this.moThumbDown = false;
  248. this.moThumbDragging = false;
  249. }
  250. private void MoveThumb(int y)
  251. {
  252. int nRealRange = Maximum - Minimum;
  253. int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
  254. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  255. int nThumbWidth = (int)fThumbWidth;
  256. if (nThumbWidth > nTrackWidth)
  257. {
  258. nThumbWidth = nTrackWidth;
  259. fThumbWidth = nTrackWidth;
  260. }
  261. if (nThumbWidth < 56)
  262. {
  263. nThumbWidth = 56;
  264. fThumbWidth = 56;
  265. }
  266. int nSpot = nClickPoint;
  267. int nPixelRange = (nTrackWidth - nThumbWidth);
  268. if (moThumbDown && nRealRange > 0)
  269. {
  270. if (nPixelRange > 0)
  271. {
  272. int nNewThumbTop = y - (UpArrowImage.Width + nSpot);
  273. if (nNewThumbTop < 0)
  274. {
  275. moThumbTop = nNewThumbTop = 0;
  276. }
  277. else if (nNewThumbTop > nPixelRange)
  278. {
  279. moThumbTop = nNewThumbTop = nPixelRange;
  280. }
  281. else
  282. {
  283. moThumbTop = y - (UpArrowImage.Width + nSpot);
  284. }
  285. //figure out value
  286. float fPerc = (float)moThumbTop / (float)nPixelRange;
  287. float fValue = fPerc * (Maximum - LargeChange);
  288. moValue = (int)fValue;
  289. Debug.WriteLine(moValue.ToString());
  290. Application.DoEvents();
  291. Invalidate();
  292. }
  293. }
  294. }
  295. private void CustomScrollbar_MouseMove(object sender, MouseEventArgs e)
  296. {
  297. if (moThumbDown == true)
  298. {
  299. this.moThumbDragging = true;
  300. }
  301. if (this.moThumbDragging)
  302. {
  303. //MoveThumb(e.Y);
  304. MoveThumb(e.X);
  305. }
  306. if (ValueChanged != null)
  307. ValueChanged(this, new EventArgs());
  308. if (Scroll != null)
  309. Scroll(this, new EventArgs());
  310. }
  311. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("LargeChange")]
  312. public int LargeChange
  313. {
  314. get { return moLargeChange; }
  315. set
  316. {
  317. moLargeChange = value;
  318. Invalidate();
  319. }
  320. }
  321. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("SmallChange")]
  322. public int SmallChange
  323. {
  324. get { return moSmallChange; }
  325. set
  326. {
  327. moSmallChange = value;
  328. Invalidate();
  329. }
  330. }
  331. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Minimum")]
  332. public int Minimum
  333. {
  334. get { return moMinimum; }
  335. set
  336. {
  337. moMinimum = value;
  338. Invalidate();
  339. }
  340. }
  341. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Maximum")]
  342. public int Maximum
  343. {
  344. get { return moMaximum; }
  345. set
  346. {
  347. moMaximum = value;
  348. Invalidate();
  349. }
  350. }
  351. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Value")]
  352. public int Value
  353. {
  354. get { return moValue; }
  355. set
  356. {
  357. moValue = value;
  358. int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
  359. float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
  360. int nThumbWidth = (int)fThumbWidth;
  361. if (nThumbWidth > nTrackWidth)
  362. {
  363. nThumbWidth = nTrackWidth;
  364. fThumbWidth = nTrackWidth;
  365. }
  366. if (nThumbWidth < 56)
  367. {
  368. nThumbWidth = 56;
  369. fThumbWidth = 56;
  370. }
  371. //figure out value
  372. int nPixelRange = nTrackWidth - nThumbWidth;
  373. int nRealRange = (Maximum - Minimum) - LargeChange;
  374. float fPerc = 0.0f;
  375. if (nRealRange != 0)
  376. {
  377. fPerc = (float)moValue / (float)nRealRange;
  378. }
  379. float fTop = fPerc * nPixelRange;
  380. moThumbTop = (int)fTop;
  381. Invalidate();
  382. }
  383. }
  384. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Channel Color")]
  385. public Color ChannelColor
  386. {
  387. get { return moChannelColor; }
  388. set { moChannelColor = value; }
  389. }
  390. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
  391. public Image UpArrowImage
  392. {
  393. get { return moUpArrowImage; }
  394. set { moUpArrowImage = value; }
  395. }
  396. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
  397. public Image DownArrowImage
  398. {
  399. get { return moDownArrowImage; }
  400. set { moDownArrowImage = value; }
  401. }
  402. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
  403. public Image ThumbTopImage
  404. {
  405. get { return moThumbTopImage; }
  406. set { moThumbTopImage = value; }
  407. }
  408. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
  409. public Image ThumbTopSpanImage
  410. {
  411. get { return moThumbTopSpanImage; }
  412. set { moThumbTopSpanImage = value; }
  413. }
  414. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
  415. public Image ThumbBottomImage
  416. {
  417. get { return moThumbBottomImage; }
  418. set { moThumbBottomImage = value; }
  419. }
  420. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
  421. public Image ThumbBottomSpanImage
  422. {
  423. get { return moThumbBottomSpanImage; }
  424. set { moThumbBottomSpanImage = value; }
  425. }
  426. [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
  427. public Image ThumbMiddleImage
  428. {
  429. get { return moThumbMiddleImage; }
  430. set { moThumbMiddleImage = value; }
  431. }
  432. }
  433. //internal class ScrollbarControlDesigner : System.Windows.Forms.Design.ControlDesigner
  434. //{
  435. // public override SelectionRules SelectionRules
  436. // {
  437. // get
  438. // {
  439. // SelectionRules selectionRules = base.SelectionRules;
  440. // PropertyDescriptor propDescriptor = TypeDescriptor.GetProperties(this.Component)["AutoSize"];
  441. // if (propDescriptor != null)
  442. // {
  443. // bool autoSize = (bool)propDescriptor.GetValue(this.Component);
  444. // if (autoSize)
  445. // {
  446. // selectionRules = SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.BottomSizeable | SelectionRules.TopSizeable;
  447. // }
  448. // else
  449. // {
  450. // selectionRules = SelectionRules.Visible | SelectionRules.AllSizeable | SelectionRules.Moveable;
  451. // }
  452. // }
  453. // return selectionRules;
  454. // }
  455. // }
  456. //}
  457. }