• C# Winform 自定义进度条ProgressBar


    效果:

    一、前言

    Winfrom各种老毛病真的不适合做大型项目,甚至中型项目都不适合,一些小功能都能把你折腾半死,比如,我想在界面上显示一个进度条,用来显示现在硬盘和内存已经使用了多少,使用了 ProgressBar 控件你看看效果:

    进度条中间一直有个白色光影在晃来晃去的,是不是想让别人感慨:“哇!好强的光芒,我的眼睛快睁不开了...”。而且背景颜色无法改变,这个动画也无法关掉,为了解决这两个问题,我找了很久,终于找到了下面的解决方法。

    二、自定义进度条

    于是我在网上找了一些资料,有到效果有,但不是特别漂亮,比如下面这个

    C# WinForm 自定义进度条控件_科技_品阅网

    另外,我参考了下面到帖子

    Winform自定义控件-进度条/图片图标进度条_思无心的博客-CSDN博客_winform进度条控件

    1.添加用户控件

    添加一个用户控件,取名为 ProgressBarControl

    添加完成后,界面如下

    我们将界面调整一下,让其看上去像个进度条,比如宽度 250,高度 30

    2.添加代码

    代码:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Drawing;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Threading.Tasks;
    7. using System.Windows.Forms;
    8. namespace 自定义控件
    9. {
    10. public partial class ProgressBarControl : UserControl
    11. {
    12. private int val;//进度值
    13. private Color PBackgroundColor = Color.FromArgb(217, 218, 219);//初始化颜色
    14. private Color PForegroundColor = Color.Green;
    15. public ProgressBarControl()
    16. {
    17. InitializeComponent();
    18. }
    19. /// <summary>
    20. /// 背景色
    21. /// </summary>
    22. public Color pBackgroundColor
    23. {
    24. get => PBackgroundColor;
    25. set
    26. {
    27. PBackgroundColor = value;
    28. this.BackColor = PBackgroundColor;
    29. }
    30. }
    31. /// <summary>
    32. /// 前景色
    33. /// </summary>
    34. public Color pForegroundColor
    35. {
    36. get => PForegroundColor;
    37. set => PForegroundColor = value;
    38. }
    39. /// <summary>
    40. /// 当前值
    41. /// </summary>
    42. public int Val
    43. {
    44. get => val;
    45. set
    46. {
    47. val = value;
    48. this.Invalidate();
    49. }
    50. }
    51. protected override void OnPaint(PaintEventArgs e)
    52. {
    53. Graphics g = e.Graphics;
    54. SolidBrush brush = new SolidBrush(PForegroundColor);
    55. float percent = val / 100f;
    56. Rectangle rect = this.ClientRectangle;
    57. rect.Width = (int)((float)rect.Width * percent);
    58. rect.Height = this.Height;
    59. g.FillRectangle(brush, rect);
    60. brush.Dispose();
    61. g.Dispose();
    62. }
    63. private void InitializeComponent()
    64. {
    65. this.SuspendLayout();
    66. this.Name = "ProgressBarControl";
    67. this.Size = new System.Drawing.Size(250, 30);
    68. this.ResumeLayout(false);
    69. }
    70. }
    71. }

     添加代码完成后,就完成了基本的操作了,下面开始使用

     三、使用方法

    先点击Form1 界面,在工具箱就可以看到 命名空间对应的自定义组件了

    将 ProgressBarControl 控件直接拖入到Form1中即可,如下

    然后在属性窗体就可以看到比Winform的ProgressBar多出来一些功能,这就是你在代码中添加的

    我们可以将背景颜色改为其他颜色

    改变进度条到进度,使用下面代码即可

    progressBarControl1.Val = 50;

     效果:

     本案例源码:点击下载

    结束

    如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢

    end

  • 相关阅读:
    MySQL 1 数据库概述知识
    移动零 ----双指针
    面试准备-软件工程
    学术 | [LaTex]超详细Texlive2022+Tex Studio下载安装配置
    【数据结构(邓俊辉)学习笔记】向量01——接口与实现
    springboot如何忽略url大小写呢?
    记一次逆向分析解密还原Class文件
    【教程】Derby数据库安装与使用
    平衡树 Treap & Splay [学习笔记]
    [技术调研]数据不平衡解决方法调研
  • 原文地址:https://blog.csdn.net/qq_38693757/article/details/125424168