// ***********************************************************************
// Assembly : HZH_Controls
// Created : 08-29-2019
//
// ***********************************************************************
//
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
//
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace HZH_Controls.Controls
{
///
/// Class UCTrackBar.
/// Implements the
///
///
[DefaultEvent("ValueChanged")]
public class UCTrackBar : Control
{
///
/// Occurs when [value changed].
///
[Description("值改变事件"), Category("自定义")]
public event EventHandler ValueChanged;
///
/// The dcimal digits
///
private int dcimalDigits = 0;
///
/// Gets or sets the dcimal digits.
///
/// The dcimal digits.
[Description("值小数精确位数"), Category("自定义")]
public int DcimalDigits
{
get { return dcimalDigits; }
set { dcimalDigits = value; }
}
///
/// The line width
///
private float lineWidth = 10;
///
/// Gets or sets the width of the line.
///
/// The width of the line.
[Description("线宽度"), Category("自定义")]
public float LineWidth
{
get { return lineWidth; }
set { lineWidth = value; }
}
///
/// The minimum value
///
private float minValue = 0;
///
/// Gets or sets the minimum value.
///
/// The minimum value.
[Description("最小值"), Category("自定义")]
public float MinValue
{
get { return minValue; }
set
{
if (minValue > m_value)
return;
minValue = value;
this.Refresh();
}
}
///
/// The maximum value
///
private float maxValue = 100;
///
/// Gets or sets the maximum value.
///
/// The maximum value.
[Description("最大值"), Category("自定义")]
public float MaxValue
{
get { return maxValue; }
set
{
if (value < m_value)
return;
maxValue = value;
this.Refresh();
}
}
///
/// The m value
///
private float m_value = 0;
///
/// Gets or sets the value.
///
/// The value.
[Description("值"), Category("自定义")]
public float Value
{
get { return this.m_value; }
set
{
if (value > maxValue || value < minValue)
return;
var v = (float)Math.Round((double)value, dcimalDigits);
if (m_value == v)
return;
this.m_value = v;
this.Refresh();
if (ValueChanged != null)
{
ValueChanged(this, null);
}
}
}
///
/// The m line color
///
private Color m_lineColor = Color.FromArgb(228, 231, 237);
///
/// Gets or sets the color of the line.
///
/// The color of the line.
[Description("线颜色"), Category("自定义")]
public Color LineColor
{
get { return m_lineColor; }
set
{
m_lineColor = value;
this.Refresh();
}
}
///
/// The m value color
///
private Color m_valueColor = Color.FromArgb(255, 77, 59);
///
/// Gets or sets the color of the value.
///
/// The color of the value.
[Description("值颜色"), Category("自定义")]
public Color ValueColor
{
get { return m_valueColor; }
set
{
m_valueColor = value;
this.Refresh();
}
}
///
/// The is show tips
///
private bool isShowTips = true;
///
/// Gets or sets a value indicating whether this instance is show tips.
///
/// true if this instance is show tips; otherwise, false.
[Description("点击滑动时是否显示数值提示"), Category("自定义")]
public bool IsShowTips
{
get { return isShowTips; }
set { isShowTips = value; }
}
///
/// Gets or sets the tips format.
///
/// The tips format.
[Description("显示数值提示的格式化形式"), Category("自定义")]
public string TipsFormat { get; set; }
///
/// The m line rectangle
///
RectangleF m_lineRectangle;
///
/// The m track rectangle
///
RectangleF m_trackRectangle;
///
/// Initializes a new instance of the class.
///
public UCTrackBar()
{
this.Size = new Size(250, 30);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.MouseDown += UCTrackBar_MouseDown;
this.MouseMove += UCTrackBar_MouseMove;
this.MouseUp += UCTrackBar_MouseUp;
}
///
/// The BLN down
///
bool blnDown = false;
///
/// Handles the MouseDown event of the UCTrackBar control.
///
/// The source of the event.
/// The instance containing the event data.
void UCTrackBar_MouseDown(object sender, MouseEventArgs e)
{
if (m_lineRectangle.Contains(e.Location) || m_trackRectangle.Contains(e.Location))
{
blnDown = true;
Value = minValue+((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
ShowTips();
}
}
///
/// Handles the MouseMove event of the UCTrackBar control.
///
/// The source of the event.
/// The instance containing the event data.
void UCTrackBar_MouseMove(object sender, MouseEventArgs e)
{
if (blnDown)
{
Value = minValue + ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
ShowTips();
}
}
///
/// Handles the MouseUp event of the UCTrackBar control.
///
/// The source of the event.
/// The instance containing the event data.
void UCTrackBar_MouseUp(object sender, MouseEventArgs e)
{
blnDown = false;
if (frmTips != null && !frmTips.IsDisposed)
{
frmTips.Close();
frmTips = null;
}
}
///
/// The FRM tips
///
Forms.FrmAnchorTips frmTips = null;
///
/// Shows the tips.
///
private void ShowTips()
{
if (isShowTips)
{
string strValue = Value.ToString();
if (!string.IsNullOrEmpty(TipsFormat))
{
try
{
strValue = Value.ToString(TipsFormat);
}
catch { }
}
var p = this.PointToScreen(new Point((int)m_trackRectangle.X, (int)m_trackRectangle.Y));
if (frmTips == null || frmTips.IsDisposed || !frmTips.Visible)
{
frmTips = Forms.FrmAnchorTips.ShowTips(new Rectangle(p.X, p.Y, (int)m_trackRectangle.Width, (int)m_trackRectangle.Height), strValue, Forms.AnchorTipsLocation.TOP, ValueColor, autoCloseTime: -1);
}
else
{
frmTips.RectControl = new Rectangle(p.X, p.Y, (int)m_trackRectangle.Width, (int)m_trackRectangle.Height);
frmTips.StrMsg = strValue;
}
}
}
///
/// Handles the event.
///
/// The instance containing the event data.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SetGDIHigh();
m_lineRectangle = new RectangleF(lineWidth, (this.Size.Height - lineWidth) / 2, this.Size.Width - lineWidth * 2, lineWidth);
GraphicsPath pathLine = ControlHelper.CreateRoundedRectanglePath(m_lineRectangle, 5);
g.FillPath(new SolidBrush(m_lineColor), pathLine);
GraphicsPath valueLine = ControlHelper.CreateRoundedRectanglePath(new RectangleF(lineWidth, (this.Size.Height - lineWidth) / 2, ((float)(m_value - minValue) / (float)(maxValue - minValue)) * m_lineRectangle.Width, lineWidth), 5);
g.FillPath(new SolidBrush(m_valueColor), valueLine);
m_trackRectangle = new RectangleF(m_lineRectangle.Left - lineWidth + (((float)(m_value - minValue) / (float)(maxValue - minValue)) * (this.Size.Width - lineWidth * 2)), (this.Size.Height - lineWidth * 2) / 2, lineWidth * 2, lineWidth * 2);
g.FillEllipse(new SolidBrush(m_valueColor), m_trackRectangle);
g.FillEllipse(Brushes.White, new RectangleF(m_trackRectangle.X + m_trackRectangle.Width / 4, m_trackRectangle.Y + m_trackRectangle.Height / 4, m_trackRectangle.Width / 2, m_trackRectangle.Height / 2));
}
}
}