📄 gscrollbar.cs
字号:
using System;
using System.Drawing;
using System.Windows.Forms;
using gowk.utility.Diagnostics;
using gowk.utility;
namespace gowk.controls
{
/// <summary>
/// GScrollBar 的摘要说明。
/// </summary>
[System.Serializable]
public class GScrollBar:GPanel
{
private int largeChange=10;
private int maximum=100;
private int minimum=0;
private int smallChange=1;
private int value=0;
private bool draging;
private Point start_drag_point;
private System.Timers.Timer timer;
private readonly int ArrowBoxX=16;
Rectangle ra;//the rectangle of the uparrow or the leftarrow;
Rectangle rb;//............ .of bottom or the rightarrow
Rectangle rbox;//the scroll box;
Rectangle rx;
Orientation orientation=Orientation.Vertical;
public event EventHandler ValueChanged;
// public event ScrollEventHandler Scroll;
public GScrollBar()
{
this.SetStyle(ControlStyles.DoubleBuffer|ControlStyles.UserPaint|ControlStyles.AllPaintingInWmPaint|ControlStyles.UserMouse,true);
this.timer=new System.Timers.Timer(50);
this.timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
this.BackColor=System.Drawing.SystemColors.Control;
this.ForeColor=Color.Gold;
this.increaseArrowImage=new GImage();
this.decreaseArrowImage=new GImage();
this.thumbImage=new GImage();
}
protected override void OnResize(EventArgs e)
{
base.OnResize (e);
this.SettingAll();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave (e);
this.Invalidate();
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel (e);
this.Value-=(int)(this.LargeChange*e.Delta/120);
}
/* protected override void OnClick(EventArgs e)
{
base.OnClick (e);
}*/
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter (e);
this.Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown (e);
Point p=new Point(e.X,e.Y);
if(this.rbox.Contains(p))
{
this.draging=true;
this.start_drag_point=p;
this.Invalidate();
}
else
{
this.timer.Enabled=true;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp (e);
this.timer.Enabled=false;
this.draging=false;
this.Invalidate();
}
/* protected void OnScroll(ScrollEventArgs e)
{
}*/
/* private void InvokeScroll(ScrollEventType t)
{
ScrollEventArgs e=new ScrollEventArgs(t,this.value);
this.OnScroll(e);
if(this.Scroll!=null)this.Scroll(this,e);
}*/
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove (e);
if(this.draging)
{
if(this.orientation==Orientation.Vertical && e.Y<this.rx.Bottom && e.Y>this.rx.Y)
{
int offset=e.Y-this.start_drag_point.Y;
this.rbox.Y+=offset;
if(this.rbox.Bottom>this.rx.Bottom)
{
this.rbox.Y=this.rx.Bottom-this.rbox.Height;
}
else if(this.rbox.Top<this.rx.Top)
{
this.rbox.Y=this.rx.Top;
}
int xx=this.rx.Height-this.rbox.Height;
if(xx>0)
{
int newValue=(int)((this.rbox.Bottom-this.rx.Top-this.rbox.Height)*(this.Maximum-this.Minimum)/xx+this.Minimum);
if(this.value!=newValue)
{
this.value=newValue;
this.InvokeValueChange();
}
this.start_drag_point=new Point(e.X,e.Y);
}
}
else if(e.X<this.rx.Right && e.X>this.rx.X)
{
int offset=e.X-this.start_drag_point.X;
this.rbox.X+=offset;
if(this.rbox.Right>this.rx.Right)
{
this.rbox.X=this.rx.Right-this.rbox.Width;
}
else if(this.rbox.Left<this.rx.Left)
{
this.rbox.X=this.rx.Left;
}
int xx=this.rx.Width-this.rbox.Width;
if(xx>0)
{
int newValue=(int)((this.rbox.Right-this.rx.Left-this.rbox.Width)*(this.Maximum-this.Minimum)/xx+this.Minimum);
if(this.value!=newValue)
{
this.value=newValue;
this.InvokeValueChange();
}
this.start_drag_point=new Point(e.X,e.Y);
}
}
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics g=e.Graphics;
this.DrawBackGround(g);
this.DrawUpArrow(g);
this.DrawDownArrow(g);
this.DrawScrollBox(g);
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Point p=this.GetMossePosition();
if(this.ra.Contains(p))
{
this.Value-=this.SmallChange;
}
else if(this.rb.Contains(p))
{
this.Value+=this.SmallChange;
}
else if(!this.rbox.Contains(p) && this.rx.Contains(p))
{
if(this.orientation==Orientation.Vertical)
{
if(p.Y<this.rbox.Top)
this.Value-=this.LargeChange;
else
this.Value+=this.LargeChange;
}
else
{
if(p.X<this.rbox.Left)
this.Value-=this.LargeChange;
else
this.Value+=this.LargeChange;
}
}
else
{
this.Invalidate();
}
}
protected virtual void OnValueChanged(EventArgs e)
{
}
private Point GetMossePosition()
{
return this.PointToClient(Control.MousePosition);
}
private void ShiftScrollBox()
{
if(this.orientation==Orientation.Vertical)
{
this.rbox.Y=this.rx.Y+(int)((this.rx.Height-this.rbox.Height)*this.value/(this.Maximum-this.Minimum));
}
else
{
this.rbox.X=this.rx.X+(int)((this.rx.Width-this.rbox.Width)*this.value/(this.Maximum-this.Minimum));
}
/*
System.Diagnostics.Trace.WriteLine("rbox:"+this.rbox.ToString());;
System.Diagnostics.Trace.WriteLine("rx:"+this.rx.ToString());;
System.Diagnostics.Trace.WriteLine("this.rbox.width:"+this.rbox.Width.ToString());*/
}
private void InvokeValueChange()
{
EventArgs e=new EventArgs();
this.OnValueChanged(e);
if(this.ValueChanged!=null)this.ValueChanged(this,e);
this.Invalidate();
// System.Diagnostics.Trace.WriteLine("invokeValueChange:"+this.value.ToString());
}
private void SettingAll()
{
if(this.Maximum==this.Minimum)
{
this.Visible=false;
return;
}
if(this.orientation==Orientation.Vertical)
{
if(this.Height-2*ArrowBoxX>0)
{
this.ra=new Rectangle(0,0,this.Width,ArrowBoxX);
this.rx=new Rectangle(0,ArrowBoxX,this.Width,this.Height-2*ArrowBoxX);
this.rbox.Width=this.Width;
this.rbox.Height=(int)(this.rx.Height*this.largeChange/(this.Maximum-this.Minimum));
this.rb=new Rectangle(0,this.Height-ArrowBoxX,this.Width,ArrowBoxX);
this.ShiftScrollBox();
}
else
{
int h=(int)(this.Height/2);
this.ra=new Rectangle(0,0,this.Width,h);
this.rx=Rectangle.Empty;
this.rb=new Rectangle(0,this.Height-h,this.Width,ArrowBoxX);
this.rbox=Rectangle.Empty;
}
}
else
{
if(this.Width-2*ArrowBoxX>0)
{
this.ra=new Rectangle(0,0,ArrowBoxX,this.Height);
this.rx=new Rectangle(ArrowBoxX,0,this.Width-2*ArrowBoxX,this.Height);
this.rbox.Height=this.Height;
this.rbox.Width=(int)(this.rx.Width*this.LargeChange/(this.Maximum-this.Minimum));
this.rb=new Rectangle(this.Width-ArrowBoxX,0,ArrowBoxX,this.Height);
this.ShiftScrollBox();
}
else
{
int w=(int)(this.Width/2);
this.ra=new Rectangle(0,0,w,this.Height);
this.rx=Rectangle.Empty;
this.rb=new Rectangle(w,0,w,this.Height);
this.rbox=Rectangle.Empty;
}
}
this.Invalidate();
}
#region
public int LargeChange
{
get
{
return Math.Min(this.largeChange, (int) ((this.maximum - this.minimum) + 1));
}
set
{
if (this.largeChange == value)
{
return;
}
if (value < 0)
{
throw new ArgumentException();
}
this.largeChange = value;
this.SettingAll();
}
}
public int Value
{
get
{
return this.value;
}
set
{
if (this.value == value)
{
return;
}
else if(value<this.minimum)
{
this.value=this.minimum;
}
else if(value>this.Maximum)
{
this.value=this.Maximum;
}
else
{
this.value = value;
}
this.ShiftScrollBox();
this.InvokeValueChange();
}
}
public int Maximum
{
get
{
return this.maximum;
}
set
{
if (this.maximum == value)
{
return;
}
if (this.minimum > value)
{
this.minimum = value;
}
if (value < this.value)
{
this.Value = value;
}
this.maximum = value;
this.SettingAll();
}
}
public int Minimum
{
get
{
return this.minimum;
}
set
{
if (this.minimum == value)
{
return;
}
if (this.maximum < value)
{
this.maximum = value;
}
if (value > this.value)
{
this.value = value;
}
this.minimum = value;
this.SettingAll();
}
}
public int SmallChange
{
get
{
return Math.Min(this.smallChange, this.LargeChange);
}
set
{
if (this.smallChange == value)
{
return;
}
if (value < 0)
{
throw new ArgumentException();
}
this.smallChange = value;
this.SettingAll();
}
}
public Orientation Orientation{get{return this.orientation;}set{this.orientation=value;}}
#endregion
private ButtonState GetButtonStateByRectangle(Rectangle rect)
{
if(this.draging)
{
return rect==this.rbox?ButtonState.Pushed:ButtonState.Normal;
}
if(rect.Contains(this.GetMossePosition()))
{
return this.Capture?ButtonState.Pushed:ButtonState.Flat;
}
return ButtonState.Normal;
}
private State GetStateByRectangle(Rectangle rect)
{
if(this.draging)
{
return rect==this.rbox?State.Pressed:State.Normal;
}
if(rect.Contains(this.GetMossePosition()))
{
return this.Capture?State.Pressed:State.Actived;
}
return State.Normal;
}
protected virtual void DrawUpArrow(Graphics g)
{
Image img=this.DecreaseArrowImage.GetImageByState(this.GetStateByRectangle(ra));
if(img!=null)
{
g.DrawImage(img,ra);
}
else
{
ButtonState state=this.GetButtonStateByRectangle(ra);
ScrollButton sb=this.orientation==Orientation.Vertical?ScrollButton.Up:ScrollButton.Left;
System.Windows.Forms.ControlPaint.DrawScrollButton(g,this.ra,sb,state);
}
}
protected virtual void DrawDownArrow(Graphics g)
{
Image img=this.IncreaseArrowImage.GetImageByState(this.GetStateByRectangle(rb));
if(img!=null)
{
g.DrawImage(img,rb);
}
else
{
ButtonState state=GetButtonStateByRectangle(rb);
ScrollButton sb=this.orientation==Orientation.Vertical?ScrollButton.Down:ScrollButton.Right;
System.Windows.Forms.ControlPaint.DrawScrollButton(g,this.rb,sb,state);
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
pevent.Graphics.FillRectangle(new SolidBrush(this.BackColor),this.ClientRectangle);
if(this.BackgroundImage!=null)pevent.Graphics.DrawImage(this.BackgroundImage,this.ClientRectangle);
}
protected virtual void DrawBackGround(Graphics g)
{
/* if(this.BackgroundImage==null)
{
g.FillRectangle(new SolidBrush(this.BackColor),this.ClientRectangle);
}
else
{
g.DrawImage(this.BackgroundImage,this.ClientRectangle);
}*/
}
protected virtual void DrawScrollBox(Graphics g)
{
Image img=this.ThumbImage.GetImageByState(this.GetStateByRectangle(rbox));
if(img!=null)
{
g.DrawImage(img,this.rbox);
}
else
{
ButtonState state=GetButtonStateByRectangle(rbox);
try
{
// System.Diagnostics.Trace.WriteLine(this.rbox.ToString());
if(this.rbox.Width>0 && this.rbox.Height>0)
System.Windows.Forms.ControlPaint.DrawButton(g,this.rbox,state);
}
catch(System.Exception ex)
{
Debug.Write(ex);
// throw;
}
// g.FillRectangle(new SolidBrush(this.ForeColor),this.rbox);
}
}
private GImage increaseArrowImage;
private GImage decreaseArrowImage;
private GImage thumbImage;
public GImage IncreaseArrowImage
{
get
{
return this.increaseArrowImage;
}
set
{
this.increaseArrowImage=value;
}
}
public GImage DecreaseArrowImage
{
get
{
return this.decreaseArrowImage;
}
set
{
this.decreaseArrowImage=value;
}
}
public GImage ThumbImage
{
get
{
return this.thumbImage;
}
set
{
this.thumbImage=value;
}
}
/*
private Image increaseArrowImage;
private Image decreaseArrowImage;
private Image thumbImage;
private Image increaseArrowActiveImage;
private Image decreaseArrowActiveImage;
private Image thumbActiveImage;
private Image increaseArrowInactiveImage;
private Image decreaseArrowInActiveImage;
private Image thumbInActiveImage;
public Image IncreaseArrowImage
{
get
{
return this.increaseArrowImage;
}
set
{
this.increaseArrowImage=value;
}
}
public Image DecreaseArrowImage
{
get
{
return this.decreaseArrowImage;
}
set
{
this.decreaseArrowImage=value;
}
}
public Image ThumbImage
{
get
{
return this.thumbImage;
}
set
{
this.thumbImage=value;
}
}
public Image IncreaseArrowActiveImage
{
get
{
return this.increaseArrowActiveImage;
}
set
{
this.increaseArrowActiveImage=value;
}
}
public Image DecreaseArrowActiveImage
{
get
{
return this.decreaseArrowActiveImage;
}
set
{
this.decreaseArrowActiveImage=value;
}
}
public Image ThumbActiveImage
{
get
{
return this.thumbActiveImage;
}
set
{
this.thumbActiveImage=value;
}
}
public Image IncreaseArrowInactiveImage
{
get
{
return this.increaseArrowInactiveImage;
}
set
{
this.increaseArrowInactiveImage=value;
}
}
public Image DecreaseArrowInActiveImage
{
get
{
return this.decreaseArrowInActiveImage;
}
set
{
this.decreaseArrowInActiveImage=value;
}
}
public Image ThumbInActiveImage
{
get
{
return this.thumbInActiveImage;
}
set
{
this.thumbInActiveImage=value;
}
}*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -