123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
- using System.Windows.Forms.Design;
- using System.Diagnostics;
- namespace LYFZ.ComponentLibrary
- {
- [Designer(typeof(ScrollbarControlDesigner))]
- public class CtlScrollBarH : UserControl
- {
- protected Color moChannelColor = Color.Empty;
- protected Image moUpArrowImage = null;
- protected Image moDownArrowImage = null;
- protected Image moThumbArrowImage = null;
- protected Image moThumbTopImage = null;
- protected Image moThumbTopSpanImage = null;
- protected Image moThumbBottomImage = null;
- protected Image moThumbBottomSpanImage = null;
- protected Image moThumbMiddleImage = null;
- protected int moLargeChange = 10;
- protected int moSmallChange = 1;
- protected int moMinimum = 0;
- protected int moMaximum = 100;
- protected int moValue = 0;
- private int nClickPoint;
- protected int moThumbTop = 0;
- protected bool moAutoSize = false;
- private bool moThumbDown = false;
- private bool moThumbDragging = false;
- public new event EventHandler Scroll = null;
- public event EventHandler ValueChanged = null;
- private int GetThumbWidth()
- {
- int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
- float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
- int nThumbWidth = (int)fThumbWidth;
- if (nThumbWidth > nTrackWidth)
- {
- nThumbWidth = nTrackWidth;
- fThumbWidth = nTrackWidth;
- }
- if (nThumbWidth < 56)
- {
- nThumbWidth = 56;
- fThumbWidth = 56;
- }
- return nThumbWidth;
- }
- public CtlScrollBarH()
- {
- InitializeComponent();
- SetStyle(ControlStyles.ResizeRedraw, true);
- SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- SetStyle(ControlStyles.DoubleBuffer, true);
- SetImage();
- this.Height = UpArrowImage.Height;
- base.MinimumSize = new Size(86,this.Height);
- }
- void SetImage()
- {
- moChannelColor = Color.FromArgb(194, 211, 255);
- UpArrowImage = LYFZ.ComponentLibrary.GetUIResources.leftarrow;
- DownArrowImage = LYFZ.ComponentLibrary.GetUIResources.rightarrow;
- ThumbBottomImage = LYFZ.ComponentLibrary.GetUIResources.ThumbRight;
- ThumbBottomSpanImage = LYFZ.ComponentLibrary.GetUIResources.ThumbSpanRight;
- ThumbTopImage = LYFZ.ComponentLibrary.GetUIResources.ThumbLeft;
- ThumbTopSpanImage = LYFZ.ComponentLibrary.GetUIResources.ThumbSpanLeft;
- ThumbMiddleImage = LYFZ.ComponentLibrary.GetUIResources.ThumbMiddleH;
-
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- SetImage();
- e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
- if (UpArrowImage != null)
- {
- //e.Graphics.DrawImage(UpArrowImage, new Rectangle(new Point(0, 0), new Size(this.Height, UpArrowImage.Width)));
- e.Graphics.DrawImage(UpArrowImage, new Rectangle(new Point(0, 0), new Size(UpArrowImage.Width, this.Height)));
- }
- Brush oBrush = new SolidBrush(moChannelColor);
- Brush oWhiteBrush = new SolidBrush(Color.FromArgb(255, 255, 255));
- //draw channel left and right border colors
- //e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(0, UpArrowImage.Width, 1, (this.Width - DownArrowImage.Width)));
- //e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(this.Height - 1, UpArrowImage.Width, 1, (this.Width - DownArrowImage.Width)));
- e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(UpArrowImage.Width, 0, (this.Width - DownArrowImage.Width), 1));
- e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(UpArrowImage.Width, this.Height - 1, (this.Width - DownArrowImage.Width), 1));
- //draw channel
- //e.Graphics.FillRectangle(oBrush, new Rectangle(1, UpArrowImage.Width, this.Height - 2, (this.Width - DownArrowImage.Width)));
- e.Graphics.FillRectangle(oBrush, new Rectangle(UpArrowImage.Width, 1, (this.Width - DownArrowImage.Width), this.Height - 2));
- //draw thumb
- int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
- float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
- int nThumbWidth = (int)fThumbWidth;
- if (nThumbWidth > nTrackWidth)
- {
- nThumbWidth = nTrackWidth;
- fThumbWidth = nTrackWidth;
- }
- if (nThumbWidth < 56)
- {
- nThumbWidth = 56;
- fThumbWidth = 56;
- }
- //Debug.WriteLine(nThumbWidth.ToString());
- float fSpanWidth = (fThumbWidth - (ThumbMiddleImage.Width + ThumbTopImage.Width + ThumbBottomImage.Width)) / 2.0f;
- int nSpanWidth = (int)fSpanWidth;
- int nTop = moThumbTop;
- nTop += UpArrowImage.Width;
- //draw top
- //e.Graphics.DrawImage(ThumbTopImage, new Rectangle(1, nTop, this.Height - 2, ThumbTopImage.Width));
- e.Graphics.DrawImage(ThumbTopImage, new Rectangle(nTop, 1, ThumbTopImage.Width, this.Height - 2));
- nTop += ThumbTopImage.Width;
- //draw top span
- Rectangle rect = new Rectangle(1, nTop, this.Height - 2, nSpanWidth);
- //e.Graphics.DrawImage(ThumbTopSpanImage, 1.0f, (float)nTop, (float)this.Height - 2.0f, (float)fSpanWidth * 2);
- e.Graphics.DrawImage(ThumbTopSpanImage, (float)nTop, 1.0f, (float)fSpanWidth * 2, (float)this.Height - 2.0f);
- nTop += nSpanWidth;
- //draw middle
- //e.Graphics.DrawImage(ThumbMiddleImage, new Rectangle(1, nTop, this.Height - 2, ThumbMiddleImage.Width));
- e.Graphics.DrawImage(ThumbMiddleImage, new Rectangle(nTop, 1, ThumbMiddleImage.Width, this.Height - 2));
- nTop += ThumbMiddleImage.Width;
- //draw top span
- //rect = new Rectangle(1, nTop, this.Height - 2, nSpanWidth * 2);
- rect = new Rectangle(nTop, 1, nSpanWidth * 2, this.Height - 2);
- e.Graphics.DrawImage(ThumbBottomSpanImage, rect);
- nTop += nSpanWidth;
- //draw bottom
- //e.Graphics.DrawImage(ThumbBottomImage, new Rectangle(1, nTop, this.Height - 2, nSpanWidth));
- e.Graphics.DrawImage(ThumbBottomImage, new Rectangle(nTop, 1, nSpanWidth, this.Height - 2));
- if (DownArrowImage != null)
- {
- //e.Graphics.DrawImage(DownArrowImage, new Rectangle(new Point(0, (this.Width - DownArrowImage.Width)), new Size(this.Height, DownArrowImage.Width)));
- e.Graphics.DrawImage(DownArrowImage, new Rectangle(new Point((this.Width - DownArrowImage.Width), 0), new Size(DownArrowImage.Width, this.Height)));
- }
- }
- public override bool AutoSize
- {
- get
- {
- return base.AutoSize;
- }
- set
- {
- base.AutoSize = value;
- if (base.AutoSize)
- {
- this.Height = moUpArrowImage.Height;
- }
- }
- }
- private void InitializeComponent()
- {
- this.SuspendLayout();
- //
- // CustomScrollbar
- //
- this.Name = "CustomScrollbar";
- this.Size = new System.Drawing.Size(200, 200);
- this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseDown);
- this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseMove);
- this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseUp);
- this.ResumeLayout(false);
- }
- private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e)
- {
- Point ptPoint = this.PointToClient(Cursor.Position);
- int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
- float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
- int nThumbWidth = (int)fThumbWidth;
- if (nThumbWidth > nTrackWidth)
- {
- nThumbWidth = nTrackWidth;
- fThumbWidth = nTrackWidth;
- }
- if (nThumbWidth < 56)
- {
- nThumbWidth = 56;
- fThumbWidth = 56;
- }
- int nTop = moThumbTop;
- nTop += UpArrowImage.Width;
- Rectangle thumbrect = new Rectangle(new Point(nTop,1 ), new Size(nThumbWidth,ThumbMiddleImage.Height ));
- if (thumbrect.Contains(ptPoint))
- {
- //hit the thumb
- nClickPoint = (ptPoint.X - nTop);
- this.moThumbDown = true;
- }
- Rectangle uparrowrect = new Rectangle(new Point(0,1 ), new Size(UpArrowImage.Width,UpArrowImage.Height ));
- if (uparrowrect.Contains(ptPoint))
- {
- int nRealRange = (Maximum - Minimum) - LargeChange;
- int nPixelRange = (nTrackWidth - nThumbWidth);
- if (nRealRange > 0)
- {
- if (nPixelRange > 0)
- {
- if ((moThumbTop - SmallChange) < 0)
- moThumbTop = 0;
- else
- moThumbTop -= SmallChange;
- //figure out value
- float fPerc = (float)moThumbTop / (float)nPixelRange;
- float fValue = fPerc * (Maximum - LargeChange);
- moValue = (int)fValue;
- Debug.WriteLine(moValue.ToString());
- if (ValueChanged != null)
- ValueChanged(this, new EventArgs());
- if (Scroll != null)
- Scroll(this, new EventArgs());
- Invalidate();
- }
- }
- }
- Rectangle downarrowrect = new Rectangle(new Point(UpArrowImage.Width + nTrackWidth,1 ), new Size(UpArrowImage.Width,UpArrowImage.Height ));
- if (downarrowrect.Contains(ptPoint))
- {
- int nRealRange = (Maximum - Minimum) - LargeChange;
- int nPixelRange = (nTrackWidth - nThumbWidth);
- if (nRealRange > 0)
- {
- if (nPixelRange > 0)
- {
- if ((moThumbTop + SmallChange) > nPixelRange)
- moThumbTop = nPixelRange;
- else
- moThumbTop += SmallChange;
- //figure out value
- float fPerc = (float)moThumbTop / (float)nPixelRange;
- float fValue = fPerc * (Maximum - LargeChange);
- moValue = (int)fValue;
- Debug.WriteLine(moValue.ToString());
- if (ValueChanged != null)
- ValueChanged(this, new EventArgs());
- if (Scroll != null)
- Scroll(this, new EventArgs());
- Invalidate();
- }
- }
- }
- }
- private void CustomScrollbar_MouseUp(object sender, MouseEventArgs e)
- {
- this.moThumbDown = false;
- this.moThumbDragging = false;
- }
- private void MoveThumb(int y)
- {
- int nRealRange = Maximum - Minimum;
- int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
- float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
- int nThumbWidth = (int)fThumbWidth;
- if (nThumbWidth > nTrackWidth)
- {
- nThumbWidth = nTrackWidth;
- fThumbWidth = nTrackWidth;
- }
- if (nThumbWidth < 56)
- {
- nThumbWidth = 56;
- fThumbWidth = 56;
- }
- int nSpot = nClickPoint;
- int nPixelRange = (nTrackWidth - nThumbWidth);
- if (moThumbDown && nRealRange > 0)
- {
- if (nPixelRange > 0)
- {
- int nNewThumbTop = y - (UpArrowImage.Width + nSpot);
- if (nNewThumbTop < 0)
- {
- moThumbTop = nNewThumbTop = 0;
- }
- else if (nNewThumbTop > nPixelRange)
- {
- moThumbTop = nNewThumbTop = nPixelRange;
- }
- else
- {
- moThumbTop = y - (UpArrowImage.Width + nSpot);
- }
- //figure out value
- float fPerc = (float)moThumbTop / (float)nPixelRange;
- float fValue = fPerc * (Maximum - LargeChange);
- moValue = (int)fValue;
- Debug.WriteLine(moValue.ToString());
- Application.DoEvents();
- Invalidate();
- }
- }
- }
- private void CustomScrollbar_MouseMove(object sender, MouseEventArgs e)
- {
- if (moThumbDown == true)
- {
- this.moThumbDragging = true;
- }
- if (this.moThumbDragging)
- {
- //MoveThumb(e.Y);
- MoveThumb(e.X);
- }
- if (ValueChanged != null)
- ValueChanged(this, new EventArgs());
- if (Scroll != null)
- Scroll(this, new EventArgs());
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("LargeChange")]
- public int LargeChange
- {
- get { return moLargeChange; }
- set
- {
- moLargeChange = value;
- Invalidate();
- }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("SmallChange")]
- public int SmallChange
- {
- get { return moSmallChange; }
- set
- {
- moSmallChange = value;
- Invalidate();
- }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Minimum")]
- public int Minimum
- {
- get { return moMinimum; }
- set
- {
- moMinimum = value;
- Invalidate();
- }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Maximum")]
- public int Maximum
- {
- get { return moMaximum; }
- set
- {
- moMaximum = value;
- Invalidate();
- }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Value")]
- public int Value
- {
- get { return moValue; }
- set
- {
- moValue = value;
- int nTrackWidth = (this.Width - (UpArrowImage.Width + DownArrowImage.Width));
- float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
- int nThumbWidth = (int)fThumbWidth;
- if (nThumbWidth > nTrackWidth)
- {
- nThumbWidth = nTrackWidth;
- fThumbWidth = nTrackWidth;
- }
- if (nThumbWidth < 56)
- {
- nThumbWidth = 56;
- fThumbWidth = 56;
- }
- //figure out value
- int nPixelRange = nTrackWidth - nThumbWidth;
- int nRealRange = (Maximum - Minimum) - LargeChange;
- float fPerc = 0.0f;
- if (nRealRange != 0)
- {
- fPerc = (float)moValue / (float)nRealRange;
- }
- float fTop = fPerc * nPixelRange;
- moThumbTop = (int)fTop;
- Invalidate();
- }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Channel Color")]
- public Color ChannelColor
- {
- get { return moChannelColor; }
- set { moChannelColor = value; }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
- public Image UpArrowImage
- {
- get { return moUpArrowImage; }
- set { moUpArrowImage = value; }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
- public Image DownArrowImage
- {
- get { return moDownArrowImage; }
- set { moDownArrowImage = value; }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
- public Image ThumbTopImage
- {
- get { return moThumbTopImage; }
- set { moThumbTopImage = value; }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
- public Image ThumbTopSpanImage
- {
- get { return moThumbTopSpanImage; }
- set { moThumbTopSpanImage = value; }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
- public Image ThumbBottomImage
- {
- get { return moThumbBottomImage; }
- set { moThumbBottomImage = value; }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
- public Image ThumbBottomSpanImage
- {
- get { return moThumbBottomSpanImage; }
- set { moThumbBottomSpanImage = value; }
- }
- [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("组件扩展属性"), Description("Up Arrow Graphic")]
- public Image ThumbMiddleImage
- {
- get { return moThumbMiddleImage; }
- set { moThumbMiddleImage = value; }
- }
-
- }
- //internal class ScrollbarControlDesigner : System.Windows.Forms.Design.ControlDesigner
- //{
- // public override SelectionRules SelectionRules
- // {
- // get
- // {
- // SelectionRules selectionRules = base.SelectionRules;
- // PropertyDescriptor propDescriptor = TypeDescriptor.GetProperties(this.Component)["AutoSize"];
- // if (propDescriptor != null)
- // {
- // bool autoSize = (bool)propDescriptor.GetValue(this.Component);
- // if (autoSize)
- // {
- // selectionRules = SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.BottomSizeable | SelectionRules.TopSizeable;
- // }
- // else
- // {
- // selectionRules = SelectionRules.Visible | SelectionRules.AllSizeable | SelectionRules.Moveable;
- // }
- // }
- // return selectionRules;
- // }
- // }
- //}
- }
|