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}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/414518.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

分享 GitHub 上的敏感词汇工具类:sensitive-word

&#x1f604; 19年之后由于某些原因断更了三年&#xff0c;23年重新扬帆起航&#xff0c;推出更多优质博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志 &#x1f390; 个人CSND主页——Mi…

6.3.5编辑视频

6.3.5编辑视频 除了上面的功能外&#xff0c;Camtasia4还能进行简单的视频编辑工作&#xff0c;如媒体的剪辑、连接、画中画等。 下面我们就利用Camtasia4的强大功能来实现一个画中画效果&#xff0c;在具体操作之前&#xff0c;需要准备好两个视频文件&#xff0c;一个作为主…

Python使用HTTP代理实现网络请求的调试和日志记录

在Python中&#xff0c;使用HTTP代理可以实现网络请求的调试和日志记录。通过HTTP代理&#xff0c;我们可以拦截、修改或记录网络请求和响应的数据&#xff0c;以便更好地了解和调试网络请求。 下面是一个使用Python和httplib2库实现HTTP代理的示例&#xff0c;同时对请求和响…

费斯托工具如何借助CRM为业务赋能?

对于“以客户为中心”的企业来说&#xff0c;要提升企业业绩&#xff0c;就要确保与客户在营销、销售、服务每一个环节上的交互都顺利、高效&#xff0c;而CRM系统正是承载这些功能的数字化工具。从图特斯工具系统技术&#xff08;上海&#xff09;有限公司&#xff08;简称“费…

计数排序归并排序(递归版本非递归版本)

1.计数排序 计数排序是一种非比较排序算法&#xff0c;其核心思想是通过统计每个元素出现的次数&#xff0c;然后根据统计结果将元素按照顺序放置在输出数组中。 以下是计数排序的逻辑思想&#xff08;C语言版&#xff09;&#xff1a; 1. 首先&#xff0c;遍历待排序的数组&am…

[足式机器人]Part2 Dr. CAN学习笔记- Kalman Filter卡尔曼滤波器Ch05-3+4

本文仅供学习使用 本文参考&#xff1a; B站&#xff1a;DR_CAN Dr. CAN学习笔记 - Kalman Filter卡尔曼滤波器 Ch05-34 3. Step by step : Deriation of Kalmen Gain 卡尔曼增益/因数 详细推导4. Priori/Posterrori error Covariance Martix 误差协方差矩阵 3. Step by step :…

Spring Boot - 利用Resilience4j-Circuitbreaker实现断路器模式_防止级联故障

文章目录 PreResilience4j概述Resilience4j官方地址Resilience4j-Circuitbreaker应用场景微服务演示Address servicePOMModelRepositoryServiceControllerData InitProperties测试 Order serviceModelRepositoryServiceSet UpProperties测试 探究断路器调用order-service API 2…

Java网络编程——UDP通信原理

一、TCP和UDP概述 传输层通常以TCP和UDP协议来控制端点与端点的通信 TCPUDP协议名称传输控制协议用户数据包协议是否连接面向连接的协议。数据必须要建立连接无连接的协议&#xff0c;每个数据报中都给出完整的地址信息&#xff0c;因此不需要事先建立发送方和接受方的连接是…

leetcode 013二维区域和检索---矩阵不可变

给定一个二维矩阵 matrix&#xff0c;以下类型的多个请求&#xff1a; 计算其子矩形范围内元素的总和&#xff0c;该子矩阵的左上角为 (row1, col1) &#xff0c;右下角为 (row2, col2) 。 实现 NumMatrix 类&#xff1a; NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进…

jrebel IDEA 热部署

1 下载 2022.4.1 JRebel and XRebel - IntelliJ IDEs Plugin | Marketplace 2 选择下载好的zip 离线安装IDEA 插件 重启IDEA 3 打开 [Preference -> JRebel & XRebel] 菜单&#xff0c;输入 GUID address 为 https://jrebel.qekang.com/1e67ec1b-122f-4708-87d…

买家福音:亚马逊鲲鹏系统全自动操作助你轻松搞定一切

我一直以来都是亚马逊的忠实用户&#xff0c;但是最近我发现了一款真正令人惊叹的工具&#xff0c;改变了我在平台上的经验。我想分享一下我的感受&#xff0c;最近&#xff0c;我得知并尝试了亚马逊鲲鹏系统&#xff0c;简直是为买家账号管理量身定制的利器。在我账号过多时&a…

旅游行业@2023/24:复苏、繁荣与重构

Mark作为一名热爱旅游的大学生&#xff0c;在2023年&#xff0c;去过很多地方&#xff0c;有因烧烤爆火的淄博&#xff1b;也有因特种兵式旅游再次出名的泰山&#xff1b;还和同学一起自驾去了川西&#xff1b;最近&#xff0c;他就正在计划春节要在哪里度过。 “我可太喜欢旅…