📄 xtrackbar.cs
字号:
namespace Imps.Client.Pc.Controls
{
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
[DefaultProperty("BarInnerColor"), DefaultEvent("Scroll"), ToolboxBitmap(typeof(TrackBar))]
public class XTrackBar : Control
{
private Color[,] aColorSchema;
private Rectangle barHalfRect;
private Color barInnerColor;
private int barMaximum;
private int barMinimum;
private System.Windows.Forms.Orientation barOrientation;
private Color barOuterColor;
private Rectangle barRect;
private int barSize;
private ColorSchemas colorSchema;
private IContainer components;
private bool drawFocusRectangle;
private bool drawSemitransparentThumb;
private uint largeChange;
private bool mouseEffects;
private bool mouseInRegion;
private bool mouseInThumbRegion;
private int mouseWheelBarPartitions;
private uint smallChange;
private GraphicsPath thumbCustomShape;
private Rectangle thumbHalfRect;
private Color thumbInnerColor;
private Color thumbOuterColor;
private Color thumbPenColor;
private Rectangle thumbRect;
private int thumbSize;
private int trackerValue;
[Category("Behavior"), Description("Event fires when the Slider position is changed")]
public event ScrollEventHandler Scroll;
[Category("Action"), Description("Event fires when the Value property changes")]
public event EventHandler ValueChanged;
public XTrackBar() : this(0, 100, 50)
{
}
public XTrackBar(int min, int max, int value)
{
this.thumbSize = 15;
this.barSize = 3;
this.trackerValue = 50;
this.barMaximum = 100;
this.smallChange = 1;
this.largeChange = 5;
this.drawFocusRectangle = true;
this.drawSemitransparentThumb = true;
this.mouseEffects = true;
this.mouseWheelBarPartitions = 10;
this.thumbOuterColor = Color.White;
this.thumbInnerColor = Color.Gainsboro;
this.thumbPenColor = Color.Silver;
this.barOuterColor = Color.SkyBlue;
this.barInnerColor = Color.DarkSlateBlue;
Color[,] colorArray = new Color[5, 5];
*(colorArray[0, 0]) = Color.White;
*(colorArray[0, 1]) = Color.FromArgb(0xaf, 0xaf, 0xaf);
*(colorArray[0, 2]) = Color.FromArgb(0x44, 0x44, 0x44);
*(colorArray[0, 3]) = Color.FromArgb(0x13, 0x13, 0x13);
*(colorArray[0, 4]) = Color.FromArgb(0x13, 0x13, 0x13);
*(colorArray[1, 0]) = Color.White;
*(colorArray[1, 1]) = Color.Gainsboro;
*(colorArray[1, 2]) = Color.Silver;
*(colorArray[1, 3]) = Color.SkyBlue;
*(colorArray[1, 4]) = Color.DarkSlateBlue;
*(colorArray[2, 0]) = Color.White;
*(colorArray[2, 1]) = Color.Gainsboro;
*(colorArray[2, 2]) = Color.Silver;
*(colorArray[2, 3]) = Color.Red;
*(colorArray[2, 4]) = Color.DarkRed;
*(colorArray[3, 0]) = Color.White;
*(colorArray[3, 1]) = Color.Gainsboro;
*(colorArray[3, 2]) = Color.Silver;
*(colorArray[3, 3]) = Color.GreenYellow;
*(colorArray[3, 4]) = Color.Yellow;
*(colorArray[4, 0]) = Color.White;
*(colorArray[4, 1]) = Color.Gainsboro;
*(colorArray[4, 2]) = Color.Silver;
*(colorArray[4, 3]) = Color.Red;
*(colorArray[4, 4]) = Color.Crimson;
this.aColorSchema = colorArray;
this.colorSchema = ColorSchemas.PerlBlueGreen;
this.InitializeComponent();
base.SetStyle(0x22e12, true);
this.BackColor = Color.Transparent;
this.Minimum = min;
this.Maximum = max;
this.Value = value;
}
public static GraphicsPath CreateRoundRectPath(Rectangle rect, Size size)
{
GraphicsPath path = new GraphicsPath();
path.AddLine(rect.Left + (size.Width / 2), rect.Top, rect.Right - (size.Width / 2), rect.Top);
path.AddArc(rect.Right - size.Width, rect.Top, size.Width, size.Height, 270f, 90f);
path.AddLine(rect.Right, rect.Top + (size.Height / 2), rect.Right, rect.Bottom - (size.Width / 2));
path.AddArc(rect.Right - size.Width, rect.Bottom - size.Height, size.Width, size.Height, 0f, 90f);
path.AddLine(rect.Right - (size.Width / 2), rect.Bottom, rect.Left + (size.Width / 2), rect.Bottom);
path.AddArc(rect.Left, rect.Bottom - size.Height, size.Width, size.Height, 90f, 90f);
path.AddLine(rect.Left, rect.Bottom - (size.Height / 2), rect.Left, rect.Top + (size.Height / 2));
path.AddArc(rect.Left, rect.Top, size.Width, size.Height, 180f, 90f);
return path;
}
public static Color[] DesaturateColors(params Color[] colorsToDesaturate)
{
Color[] colorArray = new Color[colorsToDesaturate.Length];
for (int i = 0; i < colorsToDesaturate.Length; i++)
{
int num2 = (int) (((colorsToDesaturate[i].R * 0.3) + (colorsToDesaturate[i].G * 0.6)) + (colorsToDesaturate[i].B * 0.1));
colorArray[i] = Color.FromArgb((-65793 * (0xff - num2)) - 1);
}
return colorArray;
}
protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
}
private void DrawColorSlider(PaintEventArgs e, Color thumbOuterColorPaint, Color thumbInnerColorPaint, Color thumbPenColorPaint, Color barOuterColorPaint, Color barInnerColorPaint)
{
try
{
LinearGradientMode linearGradientMode;
GraphicsPath thumbCustomShape;
this.barRect = this.ClientRectangle;
if (this.barOrientation == System.Windows.Forms.Orientation.Horizontal)
{
int num = ((this.trackerValue - this.barMinimum) * (this.barRect.Width - this.thumbSize)) / (this.barMaximum - this.barMinimum);
this.thumbRect = new Rectangle(this.barRect.Left + num, this.barRect.Top + 1, this.thumbSize - 1, this.barRect.Height - 3);
}
else
{
int num2 = ((this.trackerValue - this.barMinimum) * (this.ClientRectangle.Height - this.thumbSize)) / (this.barMaximum - this.barMinimum);
this.thumbRect = new Rectangle(this.barRect.Left + 1, this.barRect.Top + num2, this.barRect.Width - 3, this.thumbSize - 1);
}
this.thumbHalfRect = this.thumbRect;
if (this.barOrientation == System.Windows.Forms.Orientation.Horizontal)
{
this.barRect = new Rectangle(this.barRect.Left + 1, ((this.barRect.Top + this.barRect.Bottom) - this.barSize) / 2, this.barRect.Width - 2, this.barSize);
this.barHalfRect = this.barRect;
this.barHalfRect.Height /= 2;
linearGradientMode = LinearGradientMode.Vertical;
this.thumbHalfRect.Height /= 2;
}
else
{
this.barRect = new Rectangle(((this.barRect.Left + this.barRect.Right) - this.barSize) / 2, this.barRect.Top + 1, this.barSize, this.barRect.Height - 2);
this.barHalfRect = this.barRect;
this.barHalfRect.Width /= 2;
linearGradientMode = LinearGradientMode.Horizontal;
this.thumbHalfRect.Width /= 2;
}
if (this.thumbCustomShape == null)
{
new GraphicsPath().AddRectangle(this.thumbRect);
}
else
{
thumbCustomShape = this.thumbCustomShape;
Matrix matrix = new Matrix();
matrix.Translate(this.thumbRect.Left - thumbCustomShape.GetBounds().Left, this.thumbRect.Top - thumbCustomShape.GetBounds().Top);
thumbCustomShape.Transform(matrix);
}
using (LinearGradientBrush brush = new LinearGradientBrush(this.barHalfRect, barOuterColorPaint, barInnerColorPaint, linearGradientMode))
{
brush.WrapMode = WrapMode.TileFlipXY;
e.Graphics.FillRectangle(brush, this.barRect);
}
Color color = thumbOuterColorPaint;
Color color2 = thumbInnerColorPaint;
if (base.Capture && this.drawSemitransparentThumb)
{
color = Color.FromArgb(0xaf, thumbOuterColorPaint);
color2 = Color.FromArgb(0xaf, thumbInnerColorPaint);
}
using (LinearGradientBrush brush2 = new LinearGradientBrush(this.thumbHalfRect, color, color2, linearGradientMode))
{
brush2.WrapMode = WrapMode.TileFlipXY;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillPath(brush2, thumbCustomShape);
using (Pen pen = new Pen(thumbPenColorPaint))
{
e.Graphics.DrawPath(pen, thumbCustomShape);
}
}
}
catch (Exception exception)
{
Console.WriteLine("DrawBackGround Error in " + base.Name + ":" + exception.Message);
}
}
private void InitializeComponent()
{
base.SuspendLayout();
base.Size = new Size(200, 30);
base.ResumeLayout(false);
}
private static bool IsPointInRect(Point pt, Rectangle rect)
{
return ((((pt.X > rect.Left) & (pt.X < rect.Right)) & (pt.Y > rect.Top)) & (pt.Y < rect.Bottom));
}
public static Color[] LightenColors(params Color[] colorsToLighten)
{
Color[] colorArray = new Color[colorsToLighten.Length];
for (int i = 0; i < colorsToLighten.Length; i++)
{
colorArray[i] = ControlPaint.Light(colorsToLighten[i]);
}
return colorArray;
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
base.Invalidate();
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
base.Invalidate();
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
switch (e.KeyCode)
{
case Keys.Prior:
this.SetProperValue(this.Value + ((int) this.largeChange));
if (this.Scroll != null)
{
this.Scroll(this, new ScrollEventArgs(ScrollEventType.LargeIncrement, this.Value));
}
break;
case Keys.Next:
this.SetProperValue(this.Value - ((int) this.largeChange));
if (this.Scroll != null)
{
this.Scroll(this, new ScrollEventArgs(ScrollEventType.LargeDecrement, this.Value));
}
break;
case Keys.End:
this.Value = this.barMaximum;
break;
case Keys.Home:
this.Value = this.barMinimum;
break;
case Keys.Left:
case Keys.Down:
this.SetProperValue(this.Value - ((int) this.smallChange));
if (this.Scroll != null)
{
this.Scroll(this, new ScrollEventArgs(ScrollEventType.SmallDecrement, this.Value));
}
break;
case Keys.Up:
case Keys.Right:
this.SetProperValue(this.Value + ((int) this.smallChange));
if (this.Scroll != null)
{
this.Scroll(this, new ScrollEventArgs(ScrollEventType.SmallIncrement, this.Value));
}
break;
}
if ((this.Scroll != null) && (this.Value == this.barMinimum))
{
this.Scroll(this, new ScrollEventArgs(ScrollEventType.First, this.Value));
}
if ((this.Scroll != null) && (this.Value == this.barMaximum))
{
this.Scroll(this, new ScrollEventArgs(ScrollEventType.Last, this.Value));
}
Point point = base.PointToClient(Cursor.Position);
this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0));
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
base.Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
base.Capture = true;
this.OnMouseMove(e);
}
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
this.mouseInRegion = true;
base.Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.mouseInRegion = false;
this.mouseInThumbRegion = false;
base.Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
this.mouseInThumbRegion = IsPointInRect(e.get_Location(), this.thumbRect);
if (base.Capture & (e.Button == MouseButtons.Left))
{
ScrollEventType thumbPosition = ScrollEventType.ThumbPosition;
Point point = e.get_Location();
int num = (this.barOrientation == System.Windows.Forms.Orientation.Horizontal) ? point.X : point.Y;
int num2 = this.thumbSize >> 1;
num -= num2;
float num3 = ((float) (this.barMaximum - this.barMinimum)) / ((float) (((this.barOrientation == System.Windows.Forms.Orientation.Horizontal) ? base.ClientSize.Width : base.ClientSize.Height) - (2 * num2)));
this.trackerValue = ((int) Math.Round((double) (num * num3), 0)) + this.barMinimum;
if (this.trackerValue <= this.barMinimum)
{
this.trackerValue = this.barMinimum;
thumbPosition = ScrollEventType.First;
}
else if (this.trackerValue >= this.barMaximum)
{
this.trackerValue = this.barMaximum;
thumbPosition = ScrollEventType.Last;
}
if (this.Scroll != null)
{
this.Scroll(this, new ScrollEventArgs(thumbPosition, this.trackerValue));
}
if (this.ValueChanged != null)
{
this.ValueChanged(this, new EventArgs());
}
}
base.Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
base.Capture = false;
this.mouseInThumbRegion = IsPointInRect(e.get_Location(), this.thumbRect);
if (this.Scroll != null)
{
this.Scroll(this, new ScrollEventArgs(ScrollEventType.EndScroll, this.trackerValue));
}
if (this.ValueChanged != null)
{
this.ValueChanged(this, new EventArgs());
}
base.Invalidate();
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
int num = ((e.Delta / 120) * (this.barMaximum - this.barMinimum)) / this.mouseWheelBarPartitions;
this.SetProperValue(this.Value + num);
}
protected override void OnPaint(PaintEventArgs e)
{
if (!base.Enabled)
{
Color[] colorsToDesaturate = new Color[] { this.thumbOuterColor, this.thumbInnerColor, this.thumbPenColor, this.barOuterColor, this.barInnerColor };
Color[] colorArray = DesaturateColors(colorsToDesaturate);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -