c# 自定义 滑块TrackBar

news2024/9/27 15:33:47

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

效果图如下:

具体操作:

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);
            else
                base.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.coloradmin.cn/o/1396825.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

1434. 数池塘(四方向)-深度优先搜索-DFS

代码&#xff1a; #include<iostream> using namespace std; char a[200][200]; int fx[4]{0,1,0,-1}; int fy[4]{1,0,-1,0}; int k0; int n,m; void dfs(int x,int y){a[x][y].;int tx,ty;for(int i0;i<4;i){txxfx[i];tyyfy[i];if(tx>1&&tx<n&&am…

python24.1.19引入模块

引入方法 可引入第三方模块 安装模块&#xff1a;在终端输入pip install模块名字 eg&#xff1a;

kali下-MSF-ftp_login模块破解FTP账号及密码

一、环境准备 两台设备在同一个网络内 一台kali系统&#xff1a;192.168.10.128 一台winserver2016&#xff1a;192.168.10.132 二、MSF介绍 metasploit 全称是The Metasploit Framework&#xff0c;又称MSF&#xff0c;是Kali 内置的一款渗透测试框架&#xff0c;也是全球…

Docker(二)安装指南:主要介绍在 Linux 、Windows 10 和 macOS 上的安装

作者主页&#xff1a; 正函数的个人主页 文章收录专栏&#xff1a; Docker 欢迎大家点赞 &#x1f44d; 收藏 ⭐ 加关注哦&#xff01; 安装 Docker Docker 分为 stable test 和 nightly 三个更新频道。 官方网站上有各种环境下的 安装指南&#xff0c;这里主要介绍 Docker 在…

【网络技术】【traceroute】【Linux虚拟机(Ubuntu)】无法traceroute至谷歌【及解决方法】

一、问题描述 问题描述如下&#xff1a; Ubuntu虚拟机可以ping通谷歌&#xff08;www.google.com&#xff09;&#xff0c;但是却无法通过traceroute命令找到路由路线&#xff0c;如下图所示&#xff1a; 二、解决方法 从traceroute命令的返回内容可以看出&#xff0c;路由寻…

使用IEEE754标准转换过程

IEEE 754标准是一种用于浮点数表示和计算的标准。对于给定的浮点数&#xff0c;IEEE 754标准定义了如何表示它以及如何执行基本的算术运算。 例如&#xff0c;现在要将十进制浮点数3.25f转换为IEEE 754标准的二进制表示&#xff0c;可以按照以下步骤进行&#xff1a; 将3.25f…

网卡:初始化,frame组装,网卡处理电信号过程

ip模块中存储的是一堆数字信号&#xff0c;网卡内部会把数字信号转换成电信号或者光信号在网线中传输。 网卡只是一个硬件&#xff0c;需要驱动程序去操作他&#xff0c;计算机中已经把主流网卡的驱动程序&#xff08;不只是网卡的&#xff0c;还有鼠标&#xff0c;键盘这些硬件…

性能测试知多少---性能测试流程

看到好多新手&#xff0c;在性能需求模糊的情况下&#xff0c;随便找一个性能测试工具&#xff0c;然后就开始进行性能测试了&#xff0c;在这种情况下得到的性能测试结果很难体现系统真实的能力&#xff0c;或者可能与系统真实的性能相距甚远。 与功能测试相比&#xff0c;性能…

R.swift SwiftGen 资源使用指南

R.swift 和 SwiftGen 资源转换使用指南 R.swift &#xff08;原始代码会打包到项目&#xff1f;&#xff09; Pod platform :ios, 12.0 target LBtest do# Comment the next line if you dont want to use dynamic frameworksuse_frameworks!pod R.swift # pod SwiftGen, ~&g…

虚幻UE 特效-Niagara特效实战-雨天

回顾Niagara特效基础知识&#xff1a;虚幻UE 特效-Niagara特效初识 其他两篇实战&#xff1a;虚幻UE 特效-Niagara特效实战-火焰、烛火、虚幻UE 特效-Niagara特效实战-烟雾、喷泉 本篇笔记我们再来实战雨天&#xff0c;雨天主要用到了特效中的事件。 文章目录 一、雨天1、创建雨…

智汇云舟创始人兼总裁周舟:视频孪生赋能智慧城市多元场景建设

1月18日&#xff0c;由知名科技媒体和产业智库泰伯网主办的WIF2023创新先行者论坛暨企业家会员年会于北京成功举办。共有百余位科技公司创始人、管理者、投资人齐聚&#xff0c;研判产业经济趋势&#xff0c;寻找新形势下企业未来发展的新方向、新机会。智汇云舟创始人兼总裁周…

毅速ESU:金属3D打印技术助力模具开发降本增效

模具是工业之母&#xff0c;在高效批产方面极具优势&#xff0c;但随着企业对模具精度、结构复杂性、生产周期和成本的要求日益严格&#xff0c;传统模具制造已经不能完全满足市场需求。在众多的模具加工工艺新技术中&#xff0c;3D打印技术把增材制造理念引入模具行业&#xf…

精品量化公式——“筹码动态”,筹码动态改进版,增加了三个买点信号标识

不多说&#xff0c;直接上效果如图&#xff1a; ► 日线表现 代码评估 技术指标代码评估&#xff1a; 散筹估算: 使用EMA&#xff08;指数移动平均&#xff09;方法计算(WINNER(C*1.1)-WINNER(C*0.9))*70的3日均线&#xff0c;用黄色粗线表示。这是用于估算市场中散户投资者的…

在Linux中安装和配置Node.js与Express.js创建HTTP服务器

在Linux环境中&#xff0c;安装和配置Node.js与Express.js来创建一个HTTP服务器需要一系列的步骤。下面是一个详细的指南&#xff0c;帮助你在Linux上设置这个环境。 步骤1&#xff1a;安装Node.js 首先&#xff0c;你需要确保你的Linux系统已经安装了Node.js。你可以通过以下…

Vulnhub靶机:driftingblues 7

一、介绍 运行环境&#xff1a;Virtualbox 攻击机&#xff1a;kali&#xff08;10.0.2.15&#xff09; 靶机&#xff1a;driftingblues7&#xff08;10.0.2.23&#xff09; 目标&#xff1a;获取靶机root权限和flag 靶机下载地址&#xff1a;https://www.vulnhub.com/entr…

限时免费参加游戏开发训练营!!!

限时免费 本周有效 游戏行业作为当下最火热的行业之一&#xff0c;受到了很多人的喜爱&#xff0c;也有很多同学想要进入游戏开发行业。 基于大家的需求&#xff0c;我们为大家提供了不同类型的游戏开发训练营&#xff0c;让你既能通过这些游戏Demo熟练开发技能&#xff0c;也…

python222网站实战(SpringBoot+SpringSecurity+MybatisPlus+thymeleaf+layui)-热门帖子推荐显示实现

锋哥原创的SpringbootLayui python222网站实战&#xff1a; python222网站实战课程视频教程&#xff08;SpringBootPython爬虫实战&#xff09; ( 火爆连载更新中... )_哔哩哔哩_bilibilipython222网站实战课程视频教程&#xff08;SpringBootPython爬虫实战&#xff09; ( 火…

【android】 android 里写jni

目录 &#xff08;1&#xff09; 环境准备 (2) 关联c文件到gradle文件 &#xff08;3&#xff09; 生成了 (4) 书写 &#xff08;5&#xff09; 使用 &#xff08;6&#xff09;业务调用 参考文档 &#xff08;1&#xff09; 环境准备 ndk, cmake (2) 关联c文件到gr…

算法练习-A+B/财务管理/实现四舍五入/牛牛的菱形字符(题目链接+题解打卡)

难度参考 难度&#xff1a;简单 分类&#xff1a;熟悉OJ与IDE的操作 难度与分类由我所参与的培训课程提供&#xff0c;但需要注意的是&#xff0c;难度与分类仅供参考。以下内容均为个人笔记&#xff0c;旨在督促自己认真学习。 题目 A B1. A B - AcWing题库财务管理1004:财…