using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.VisualStyles;
namespace System.Windows.Forms
{
public class RibbonTrackBar : RibbonItem
{
protected const int BordHeight = 3;
private const int spacing = 2;
internal bool _imageVisible;
internal Rectangle _imageBounds;
internal int _TrackBarWidth;
internal Rectangle _TrackBarBounds;
internal Rectangle moveRect;
public RibbonTrackBar()
{
this.MinValue = 0;
this.MaxValue = 10;
this.Value = 0;
_TrackBarWidth = 150;
moveRect = new Rectangle(0, 2, 38, 60);
}
private int minValue, maxValue, currValue;
/// <summary>
/// Gets or sets the width
/// </summary>
[Description("长度"), Category("外观")]
public int Width
{
get { return _TrackBarWidth; }
set { _TrackBarWidth = value; NotifyOwnerRegionsChanged(); }
}
[Description("最小值"), Category("值")]
public int MinValue
{
get { return minValue; }
set
{
if (value > MaxValue) return;
minValue = value;
//this.Refresh();
}
}
[Description("最大值"), Category("值")]
public int MaxValue
{
get { return maxValue; }
set
{
if (value < MinValue) return;
maxValue = value;
//this.Refresh();
}
}
[Description("当前值"), Category("值")]
public new int Value
{
get { return currValue; }
set
{
int preValue = currValue;
if (value > MaxValue) currValue = MaxValue;
else if (value < MinValue) currValue = MinValue;
else currValue = value;
//this.Refresh();
if (preValue != currValue) OnScroll();
}
}
private Color fillColor = Color.White;
[Description("有值部分颜色"), Category("外观")]
public Color FillColor
{
get { return fillColor; }
set { fillColor = value; }
}
private Color emptyColor = Color.FromArgb(135, 124, 124);
[Description("无值部分颜色"), Category("外观")]
public Color EmptyColor
{
get { return emptyColor; }
set { emptyColor = value; }
}
[Description("滑块形状"), Category("外观")]
public TrackShape Shape { get; set; }
protected float ValueX
{
get
{
return (Value - MinValue) / (float)(MaxValue - MinValue) * (BorderLength - 1);
}
}
protected int BorderLength
{
get
{
return this.Width - BordHeight * 17;
}
}
protected void DrawCircleTrack(Graphics g, SolidBrush brush)
{
using (Pen emptyPen = new Pen(EmptyColor))
{
g.FillEllipse(brush, ValueX + 41, 26, 10, 10);
g.DrawEllipse(emptyPen, ValueX + 41, 26, 10, 10);
}
}
protected void DrawRectangles(Graphics g, SolidBrush brush)
{
using (Pen emptyPen = new Pen(EmptyColor))
{
g.FillRectangle(brush, ValueX + BordHeight / 2 + 44, 26, 6, 10);
g.DrawRectangle(emptyPen, ValueX + BordHeight / 2 + 44, 26, 6, 10);
}
}
/// <summary>
/// 通过鼠标当前位置计算出进度值
/// </summary>
/// <param name="x">鼠标当前位置</param>
/// <returns></returns>
public int LocationX2Value(int x)
{
return (int)(((MaxValue - MinValue) / (float)BorderLength * (x - _TrackBarBounds.Location.X - 44) + MinValue));
}
[Description("当值变化后触发的事件"), Category("值")]
public event EventHandler Scroll = null;
public virtual void OnScroll()
{
if (Scroll != null)
{
Scroll(this, new EventArgs());
}
}
public enum TrackShape
{
Circle,
Rectanles
}
/// <summary>
/// Measures the suposed height of the bar
/// </summary>
/// <returns></returns>
public virtual int MeasureHeight()
{
return 60 + Owner.ItemMargin.Vertical;
}
bool x = false;
public override void OnPaint(object sender, RibbonElementPaintEventArgs e)
{
//Shape = TrackShape.Rectanles;
using (Bitmap bit = new Bitmap(this.Width, MeasureHeight()))
{
using (Graphics g = Graphics.FromImage(bit))
{
if (this.Image != null)
{
g.DrawImage(this.Image, 2, 10, 32, 26);
}
using (SolidBrush fontBrush = new SolidBrush(Color.Black))
{
g.DrawString(this.Text, new Font("宋体", 9), fontBrush, new PointF(0, 44));
}
using (SolidBrush emptyBrush = new SolidBrush(EmptyColor))
{
g.FillRectangle(emptyBrush, BordHeight + 42, 2 + BordHeight / 2f + 26, this.Width, BordHeight);
}
using (SolidBrush valueBrush = new SolidBrush(FillColor))
{
g.FillRectangle(valueBrush, BordHeight + 1f + 42, 2 + BordHeight / 2f + 1f + 26, ValueX - 2, BordHeight - 2);
switch (Shape)
{
case TrackShape.Circle: DrawCircleTrack(g, valueBrush); break;
case TrackShape.Rectanles: DrawRectangles(g, valueBrush); break;
}
}
}
e.Graphics.DrawImage(bit, _TrackBarBounds.Location.X, _TrackBarBounds.Location.Y);
}
}
public override void SetBounds(System.Drawing.Rectangle bounds)
{
base.SetBounds(bounds);
_TrackBarBounds = Rectangle.FromLTRB(
bounds.Right - _TrackBarWidth,
bounds.Top,
bounds.Right,
bounds.Bottom);
if (Image != null)
_imageBounds = new Rectangle(
bounds.Left + Owner.ItemMargin.Left,
bounds.Top + Owner.ItemMargin.Top, Image.Width, Image.Height);
else
_imageBounds = new Rectangle(ContentBounds.Location, Size.Empty);
if (SizeMode == RibbonElementSizeMode.Large)
{
_imageVisible = true;
}
else if (SizeMode == RibbonElementSizeMode.Medium)
{
_imageVisible = true;
}
else if (SizeMode == RibbonElementSizeMode.Compact)
{
_imageBounds = Rectangle.Empty;
_imageVisible = false;
}
}
public override Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e)
{
if (!Visible && !Owner.IsDesignMode())
{
SetLastMeasuredSize(new Size(0, 0));
return LastMeasuredSize;
}
Size size = Size.Empty;
int w = 0;
//int iwidth = Image != null ? Image.Width + spacing : 0;
int iwidth = 0;
int twidth = Width;
w += Width;
switch (e.SizeMode)
{
case RibbonElementSizeMode.Large:
w += iwidth + spacing;
break;
case RibbonElementSizeMode.Medium:
w += iwidth;
break;
}
SetLastMeasuredSize(new Size(w, MeasureHeight()));
return LastMeasuredSize;
}
public override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
}
public override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
}
public override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
}
public override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button != System.Windows.Forms.MouseButtons.Left) return;
int tempValue = LocationX2Value(e.X);
if (tempValue > MaxValue) Value = MaxValue;
else if (tempValue < MinValue) Value = MinValue;
else Value = tempValue;
}
public override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
}
}
}