UCtlScrollBarV.cs 19 KB

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