官网

http://www.hzhcontrols.com

前提

入行已经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自定义控件-滑块的更多相关文章

  1. (二十五)c#Winform自定义控件-有确定取消的窗体(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. (三十五)c#Winform自定义控件-下拉框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  3. (四十五)c#Winform自定义控件-水波图表

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  4. (十五)c#Winform自定义控件-键盘(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  5. (三十五)c#Winform自定义控件-Tab页

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (五十五)c#Winform自定义控件-管道

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  7. (六十五)c#Winform自定义控件-思维导图/组织架构图(工业)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  8. (六十五)c#Winform自定义控件-图标字体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  9. (七十五)c#Winform自定义控件-控件水印组件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. 问题解决:Maven execution terminated abnormally (exit code 1)

    Maven execution terminated abnormally (exit code 1) 修改setting.xml中的镜像位置 如下就可以了 <mirror> <id ...

  2. xutils3 上传文件操作——个人小计

    上传文件注意: 使用KeyValue对象进行添加文件操作 int uid = 2; //普通字段的存储 requestParams.addBodyParameter("uid", ...

  3. Y服务-你真的懂 Yaml 吗

    目录 一.什么是 Yaml 二.Yaml 的语法 三.操作 Yaml A. 引入框架 B. 代码片段 C. 完整案例 参考文档 在Java 的世界里,配置的事情都交给了 Properties,要追溯起 ...

  4. MySql 命令(命令行)

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  5. ThinkPHP 5.0 控制器-》请求-》数据库

    ThinkPHP 5.0 控制器->请求->数据库 控制器总结 无需继承其他的类(若继承了Think/Controller,可直接调用view函数渲染模板),位置处于application ...

  6. Vue双向绑定原理及其实现

    在之前面试的时候被面试官问到是否了解Vue双向绑定的原理,其实自己之前看过双向绑定的原理,但也就是粗略的了解,但是没有深入.面试官当时让我手写一个原理,但是就蒙了

  7. Pivotal:15分钟部署你的应用

    “ 本篇文章介绍的是PaaS平台Pivotal Cloud Foundry(以下简称PCF)的初步使用,相比于传统的IaaS平台(比如阿里云),PCF可实现快速迭代开发与部署,让您专注于业务开发.” ...

  8. context创建过程解析(二)之deployWARs

    HostConfig.deployApps() //在监听到start事件类型,也就是StandardHost调用startInternal protected void deployApps() { ...

  9. EF Core的Code First 基础

    一.创建实体类与映射类 通过NuGet引用Microsoft.EntityFrameworkCore 1.创建实体类 Code First可以通过为实体类字段添加相应特性,来创建对应的字段类型等,举例 ...

  10. Git 从master拉取代码创建新分支

    从master拉取新分支并push到远端 开发过程中经常用到从master分支copy一个开发分支: 1.切换到被copy的分支(master),并且从远端拉取最新版本 $git checkout m ...