(五十)c#Winform自定义控件-滑块
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果


准备工作
依然是GDI+画的,不了解自行百度学习下
第二个有提示文字的用到了(五十一)c#Winform自定义控件-文字提示
开始
添加一个类UCTrackBar,继承Control
添加属性
[Description("值改变事件"), Category("自定义")]
public event EventHandler ValueChanged;
private int dcimalDigits = ;
[Description("值小数精确位数"), Category("自定义")]
public int DcimalDigits
{
get { return dcimalDigits; }
set { dcimalDigits = value; }
}
private float lineWidth = ;
[Description("线宽度"), Category("自定义")]
public float LineWidth
{
get { return lineWidth; }
set { lineWidth = value; }
}
private float minValue = ;
[Description("最小值"), Category("自定义")]
public float MinValue
{
get { return minValue; }
set
{
if (minValue > m_value)
return;
minValue = value;
this.Refresh();
}
}
private float maxValue = ;
[Description("最大值"), Category("自定义")]
public float MaxValue
{
get { return maxValue; }
set
{
if (value < m_value)
return;
maxValue = value;
this.Refresh();
}
}
private float m_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 (value == v)
return;
this.m_value = v;
this.Refresh();
if (ValueChanged != null)
{
ValueChanged(this, null);
}
}
}
private Color m_lineColor = Color.FromArgb(, , );
[Description("线颜色"), Category("自定义")]
public Color LineColor
{
get { return m_lineColor; }
set
{
m_lineColor = value;
this.Refresh();
}
}
RectangleF m_lineRectangle;
RectangleF m_trackRectangle;
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SetGDIHigh();
m_lineRectangle = new RectangleF(lineWidth, (this.Size.Height - lineWidth) / , this.Size.Width - lineWidth * , lineWidth);
GraphicsPath pathLine = ControlHelper.CreateRoundedRectanglePath(m_lineRectangle, );
g.FillPath(new SolidBrush(m_lineColor), pathLine);
m_trackRectangle = new RectangleF(m_lineRectangle.Left - lineWidth + (((float)m_value / (float)(maxValue - minValue)) * (this.Size.Width - lineWidth * )), (this.Size.Height - lineWidth * ) / , lineWidth * , lineWidth * );
g.FillEllipse(new SolidBrush(m_lineColor), m_trackRectangle);
g.FillEllipse(Brushes.White, new RectangleF(m_trackRectangle.X + m_trackRectangle.Width / , m_trackRectangle.Y + m_trackRectangle.Height / , m_trackRectangle.Width / , m_trackRectangle.Height / ));
}
处理拖动
public UCTrackBar()
{
this.Size = new Size(, );
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;
} bool blnDown = false;
void UCTrackBar_MouseDown(object sender, MouseEventArgs e)
{
if (m_lineRectangle.Contains(e.Location) || m_trackRectangle.Contains(e.Location))
{
blnDown = true;
Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
}
}
void UCTrackBar_MouseMove(object sender, MouseEventArgs e)
{
if (blnDown)
{
Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
}
}
void UCTrackBar_MouseUp(object sender, MouseEventArgs e)
{
blnDown = false;
}
完整代码
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
{
public class UCTrackBar : Control
{
[Description("值改变事件"), Category("自定义")]
public event EventHandler ValueChanged; private int dcimalDigits = ; [Description("值小数精确位数"), Category("自定义")]
public int DcimalDigits
{
get { return dcimalDigits; }
set { dcimalDigits = value; }
} private float lineWidth = ; [Description("线宽度"), Category("自定义")]
public float LineWidth
{
get { return lineWidth; }
set { lineWidth = value; }
} private float minValue = ; [Description("最小值"), Category("自定义")]
public float MinValue
{
get { return minValue; }
set
{
if (minValue > m_value)
return;
minValue = value;
this.Refresh();
}
} private float maxValue = ; [Description("最大值"), Category("自定义")]
public float MaxValue
{
get { return maxValue; }
set
{
if (value < m_value)
return;
maxValue = value;
this.Refresh();
}
} private float m_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 (value == v)
return;
this.m_value = v;
this.Refresh();
if (ValueChanged != null)
{
ValueChanged(this, null);
}
}
} private Color m_lineColor = Color.FromArgb(, , ); [Description("线颜色"), Category("自定义")]
public Color LineColor
{
get { return m_lineColor; }
set
{
m_lineColor = value;
this.Refresh();
}
}
RectangleF m_lineRectangle;
RectangleF m_trackRectangle; public UCTrackBar()
{
this.Size = new Size(, );
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;
} bool blnDown = false;
void UCTrackBar_MouseDown(object sender, MouseEventArgs e)
{
if (m_lineRectangle.Contains(e.Location) || m_trackRectangle.Contains(e.Location))
{
blnDown = true;
Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
}
}
void UCTrackBar_MouseMove(object sender, MouseEventArgs e)
{
if (blnDown)
{
Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
}
}
void UCTrackBar_MouseUp(object sender, MouseEventArgs e)
{
blnDown = false;
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SetGDIHigh();
m_lineRectangle = new RectangleF(lineWidth, (this.Size.Height - lineWidth) / , this.Size.Width - lineWidth * , lineWidth);
GraphicsPath pathLine = ControlHelper.CreateRoundedRectanglePath(m_lineRectangle, );
g.FillPath(new SolidBrush(m_lineColor), pathLine);
m_trackRectangle = new RectangleF(m_lineRectangle.Left - lineWidth + (((float)m_value / (float)(maxValue - minValue)) * (this.Size.Width - lineWidth * )), (this.Size.Height - lineWidth * ) / , lineWidth * , lineWidth * );
g.FillEllipse(new SolidBrush(m_lineColor), m_trackRectangle);
g.FillEllipse(Brushes.White, new RectangleF(m_trackRectangle.X + m_trackRectangle.Width / , m_trackRectangle.Y + m_trackRectangle.Height / , m_trackRectangle.Width / , m_trackRectangle.Height / ));
}
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(五十)c#Winform自定义控件-滑块的更多相关文章
- (二十五)c#Winform自定义控件-有确定取消的窗体(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十五)c#Winform自定义控件-下拉框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (四十五)c#Winform自定义控件-水波图表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (十五)c#Winform自定义控件-键盘(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十五)c#Winform自定义控件-Tab页
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (五十五)c#Winform自定义控件-管道
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (六十五)c#Winform自定义控件-思维导图/组织架构图(工业)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (六十五)c#Winform自定义控件-图标字体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (七十五)c#Winform自定义控件-控件水印组件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
随机推荐
- C#2.0新增功能02 泛型
连载目录 [已更新最新开发文章,点击查看详细] C# 语言和公共语言运行时 (CLR) 的 2.0 版本中添加了泛型. 泛型将类型参数的概念引入 .NET Framework,这样就可以设计具有 ...
- [leetcode] 134. Gas Station (medium)
原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...
- git 必看,各种撤销操作
场景概念说明 首先说明一个概念, git是一个分布式的版本控制工具,分布式即 git 管理的项目是有多个大致平等的仓库的.通过一个例子来说明这个东西. 举一个最简单的使用场景: 你在github 建立 ...
- 【Android Studio】常用快捷键
1. 删除一行:Ctrl + X 更新中……
- Git/Github使用方法小记
今天把人间网的桌面客户端renjian-deck正式开源了,之前对javascript的了解其实非常的不够的,所以这一次的代码写的也是乱七八糟重用性及其低下,虽然我无数次的想把代码重新整理一下,不过还 ...
- Codis与RedisCluster的原理详解
背景介绍 我们先来看一下为什么要做集群,如果我们要部署一个单节点Redis,很明显会遇到单点故障的问题. 首先能想到解决单点故障的方法,就是做主从,但是当有海量存储需求时,单一的主从结构就会出问题,说 ...
- 十分钟带你看一遍ES6新特性
let , const关键字 var 看习惯了java, 看js真的是忍不住想笑,比如说这个var,它太自由了,自由到{}根本限制不住它的生命周期 js的var关键字,无论在何处声明,都会被视为声明在 ...
- (14)ASP.NET Core 中的日志记录
1.前言 ASP.NET Core支持适用于各种内置和第三方日志记录提供应用程序的日志记录API.本文介绍了如何将日志记录API与内置提供应用程序一起使用. 2.添加日志提供程序 日志记录提供应用程序 ...
- Oracle jdbc 插入 clob blob
Oracle 使用 clob 与 blob 插入一些比较庞大的文本或者文件,JDBC 插入时 也比较简单 表结构 CREATE TABLE test_info ( user_id int NOT NU ...
- Spring Cloud微服务接口这么多怎么调试
导读 我们知道在微服务架构下,软件系统会被拆分成很多个独立运行的服务,而这些服务间需要交互通信,就需要定义各种各样的服务接口.具体来说,在基于Spring Cloud的微服务模式中,各个微服务会基于S ...