c# 自定义 滑块TrackBar

2024-01-19 22:44

本文主要是介绍c# 自定义 滑块TrackBar,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

辛苦半天做出来的,如果觉得好用,记得点赞

效果图如下:

具体操作:

1 、添加代码(代码在下面),重新生成下整个工程,在工具栏中就出现控件,将控件拖到窗体中

2、只需要调整这些参数就行

3. 常用事件

4. 下面是代码 ,直接复制,将顶部 namespace 名改成你的工程名称就能用了 。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 视频下载和播放
{public class LTrackBar : Control{public LTrackBar(){SetStyle(ControlStyles.AllPaintingInWmPaint, true);SetStyle(ControlStyles.OptimizedDoubleBuffer, true);CreateControl();}[Category("DemoUI"), Description("背景条颜色")]/// <summary>/// 未滑按钮/// </summary>private Color _BarButtonColor = Color.FromArgb(0, 0, 200); // 浅绿色public Color L_BarButtonColor{get { return _BarButtonColor; }set{_BarButtonColor = value;Invalidate();}}/// <summary>/// 未滑过的区域颜色/// </summary>private Color _BarColor = Color.FromArgb(128, 255, 128); // 浅绿色public Color L_BarColor{get { return _BarColor; }set{_BarColor = value;Invalidate();}}/// <summary>/// 已滑过的区域颜色/// </summary>private Color _SliderColor = Color.FromArgb(0, 200, 0); // 浅绿色public Color L_SliderColor{get { return _SliderColor; }set{_SliderColor = value;Invalidate();}}/// <summary>/// 圆角/// </summary>private bool _IsRound = true;public bool L_IsRound {get { return _IsRound; }set {_IsRound = value;Invalidate();}}/// <summary>/// 最小值/// </summary>private int _Minimum = 0;public int L_Minimum {get { return _Minimum; }set {_Minimum = Convert.ToInt32(value);if (_Minimum >= _Maximum) { _Minimum = _Maximum - 1; }if (_Minimum < 0) { _Minimum = 0; }if (_Value < _Minimum) { _Value = _Minimum; }Invalidate();}}/// <summary>/// 最大值/// </summary>private int _Maximum = 100;public int L_Maximum{get { return _Maximum; }set{_Maximum = Convert.ToInt32(value);if (_Minimum >= _Maximum) { _Maximum = _Minimum + 1; }if (_Value > _Minimum) { _Value = _Minimum; }Invalidate();}}/// <summary>/// 添加 滑块值改变 委托事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>public delegate void LValueChangedEventHandler(object sender, LEventArgs e);public event LValueChangedEventHandler LValueChanged;/// <summary>/// 滑块的当前值/// </summary>private int _Value = 0;public int L_Value {get { return _Value; }set {_Value = value;if (_Value > _Maximum) { _Value = _Maximum; }if (_Value < _Minimum) { _Value = _Minimum; }Invalidate();LValueChanged?.Invoke(this, new LEventArgs(_Value));}}/// <summary>/// 滑块的方向/// </summary>private Orientation _Orientation = Orientation.Horizontal_LR;public Orientation L_Orientation{get { return _Orientation; }set {Orientation old = _Orientation;_Orientation = value;if (old != _Orientation){Size = new Size(Size.Height, Size.Width);}}            }/// <summary>/// 滑块的高度/// </summary>private int _BarSize = 10;public int L_BarSize{get { return _BarSize; }set{_BarSize = value;if (_BarSize < 3) _BarSize = 3;if (_Orientation == Orientation.Horizontal_LR){Size = new Size(Width , _BarSize);}else{ Size = new Size(_BarSize,Height);}}}/// <summary>/// 实现只能调整宽度/高度,需要重写SetBoundsCore方法/// </summary>protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified){if (_Orientation == Orientation.Horizontal_LR)base.SetBoundsCore(x, y, width, _BarSize, specified);elsebase.SetBoundsCore(x, y, _BarSize, height, specified);}MouseStatus mouseStatus;private PointF mousePoint;/// <summary>/// 尺寸变化是刷新/// </summary>/// <param name="e"></param>protected override void OnSizeChanged(EventArgs e){base.OnSizeChanged(e);Invalidate();}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);pValueToPoint();e.Graphics.SmoothingMode = SmoothingMode.HighQuality;float barSizeRatio = 0.7f;if (_BarSize < 15)barSizeRatio = 0.5f;Pen penBarBack = new Pen(_BarColor, _BarSize * barSizeRatio);Pen penBarFore = new Pen(_SliderColor, _BarSize * barSizeRatio);float fCapWidth = _BarSize;float fCapHalfWidth = _BarSize / 2.0f;if (_IsRound){      penBarBack.StartCap = LineCap.Round;penBarBack.EndCap = LineCap.Round;penBarFore.StartCap = LineCap.Round;penBarFore.EndCap = LineCap.Round;}float fPointValue = 0;if (_Orientation == Orientation.Horizontal_LR){e.Graphics.DrawLine(penBarBack, fCapHalfWidth, Height / 2f, Width - fCapHalfWidth, Height / 2f);fPointValue = mousePoint.X;if (fPointValue < fCapHalfWidth) fPointValue = fCapHalfWidth;if (fPointValue > Width - fCapHalfWidth) fPointValue = Width - fCapHalfWidth;}else{e.Graphics.DrawLine(penBarBack, Width / 2f, fCapHalfWidth, Width / 2f, Height - fCapHalfWidth);fPointValue = mousePoint.Y;if (fPointValue < fCapHalfWidth) fPointValue = fCapHalfWidth;if (fPointValue > Height - fCapHalfWidth) fPointValue = Height - fCapHalfWidth;}Brush brush = new SolidBrush(_BarButtonColor);if (_Orientation == Orientation.Horizontal_LR){e.Graphics.DrawLine(penBarFore, fCapHalfWidth, Height / 2f, fPointValue, Height / 2f);e.Graphics.FillEllipse(brush, fPointValue - fCapHalfWidth, Height / 2f - fCapHalfWidth, fCapWidth-1, fCapWidth-1);}else{e.Graphics.DrawLine(penBarFore, Width / 2f, fPointValue, Width / 2f, Height - fCapHalfWidth);e.Graphics.FillEllipse(brush, Width / 2f - fCapHalfWidth, fPointValue - fCapHalfWidth, fCapWidth-1, fCapWidth - 1);}}private void pValueToPoint(){float fCapWidth = _BarSize;float fCapHalfWidth = _BarSize / 2.0f;float fRatio = Convert.ToSingle(_Value - _Minimum) / (_Maximum - _Minimum);if (_Orientation == Orientation.Horizontal_LR){float fPointValue = fRatio * (Width - fCapWidth) + fCapHalfWidth;mousePoint = new PointF(fPointValue, fCapHalfWidth);}else{float fPointValue = Height - fCapHalfWidth - fRatio * (Height - fCapWidth);mousePoint = new PointF(fCapHalfWidth, fPointValue);}}protected override void OnMouseDown(MouseEventArgs e){mouseStatus = MouseStatus.Down;mousePoint = e.Location;pPointToValue();Invalidate();base.OnMouseDown(e);     }protected override void OnMouseUp(MouseEventArgs e){mouseStatus = MouseStatus.Up;base.OnMouseUp(e); }protected override void OnMouseMove(MouseEventArgs e){if (mouseStatus == MouseStatus.Down){mousePoint = e.Location;pPointToValue();Invalidate();}base.OnMouseMove(e);}protected override void OnMouseEnter(EventArgs e){mouseStatus = MouseStatus.Enter;base.OnMouseEnter(e);    }protected override void OnMouseLeave(EventArgs e){           mouseStatus = MouseStatus.Leave;base.OnMouseLeave(e);}/// <summary>/// 计算滑块位置/// </summary>private void pPointToValue(){float fCapHalfWidth = 0;float fCapWidth = 0;if (_IsRound){fCapWidth = _BarSize;fCapHalfWidth = _BarSize * 0.5f;}// 计算滑块的位置if (_Orientation == Orientation.Horizontal_LR){float fRatio = Convert.ToSingle(mousePoint.X - fCapHalfWidth) / (Width - fCapWidth);_Value = Convert.ToInt32(fRatio * (_Maximum - _Minimum) + _Minimum);}else{float fRatio = Convert.ToSingle(Height - mousePoint.Y - fCapHalfWidth) / (Height - fCapWidth);_Value = Convert.ToInt32(fRatio * (_Maximum - _Minimum) + _Minimum);}if (_Value < _Minimum)_Value = _Minimum;else if (_Value > _Maximum)_Value = _Maximum;LValueChanged?.Invoke(this, new LEventArgs(_Value));}}public class LEventArgs : EventArgs{public LEventArgs(object value){Value = value;}public object Value { get; set; }}/// <summary>/// 控件方向/// </summary>public enum Orientation{ /// <summary>/// 水平方向 (从左到右)/// </summary>Horizontal_LR,/ <summary>/ 水平方向 (从右到左)/ </summary>//Horizontal_RL,/// <summary>/// 垂直方向 (从下到上)/// </summary>Vertical_BT,/ <summary>/  垂直方向 (从上到下)/ </summary>//Vertical_TB,}/// <summary>/// 鼠标状态/// </summary>public enum MouseStatus{ /// <summary>/// 鼠标进入/// </summary>Enter,/// <summary>/// 鼠标离开/// </summary>Leave,/// <summary>/// 鼠标按下/// </summary>Down,/// <summary>/// 鼠标放开/// </summary>Up}}

这篇关于c# 自定义 滑块TrackBar的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/623936

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla

C# dateTimePicker 显示年月日,时分秒

dateTimePicker默认只显示日期,如果需要显示年月日,时分秒,只需要以下两步: 1.dateTimePicker1.Format = DateTimePickerFormat.Time 2.dateTimePicker1.CustomFormat = yyyy-MM-dd HH:mm:ss Tips:  a. dateTimePicker1.ShowUpDown = t

C#关闭指定时间段的Excel进程的方法

private DateTime beforeTime;            //Excel启动之前时间          private DateTime afterTime;               //Excel启动之后时间          //举例          beforeTime = DateTime.Now;          Excel.Applicat

C# 防止按钮botton重复“点击”的方法

在使用C#的按钮控件的时候,经常我们想如果出现了多次点击的时候只让其在执行的时候只响应一次。这个时候很多人可能会想到使用Enable=false, 但是实际情况是还是会被多次触发,因为C#采用的是消息队列机制,这个时候我们只需要在Enable = true 之前加一句 Application.DoEvents();就能达到防止重复点击的问题。 private void btnGenerateSh