当前位置:网站首页>C# Winform 自定义进度条ProgressBar
C# Winform 自定义进度条ProgressBar
2022-06-23 21:56:00 【熊思宇】
效果:

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

我本来只是用来显示一下内存和硬盘到大小,你看,进度条中间一直有个光影在移动,给人到印象特别奇怪,搞不懂你这是在表达什么,好像要下载又不是下载。
二、自定义进度条
于是我在网上找了一些资料,有到效果有,但不是特别漂亮,比如下面这个
另外,我参考了下面到帖子
Winform自定义控件-进度条/图片图标进度条_思无心的博客-CSDN博客_winform进度条控件
1.添加用户控件
添加一个用户控件,取名为 ProgressBarControl

添加完成后,界面如下

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

2.添加代码
代码:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 自定义控件
{
public partial class ProgressBarControl : UserControl
{
private int val;//进度值
private Color PBackgroundColor = Color.FromArgb(217, 218, 219);//初始化颜色
private Color PForegroundColor = Color.Green;
public ProgressBarControl()
{
InitializeComponent();
}
/// <summary>
/// 背景色
/// </summary>
public Color pBackgroundColor
{
get => PBackgroundColor;
set
{
PBackgroundColor = value;
this.BackColor = PBackgroundColor;
}
}
/// <summary>
/// 前景色
/// </summary>
public Color pForegroundColor
{
get => PForegroundColor;
set => PForegroundColor = value;
}
/// <summary>
/// 当前值
/// </summary>
public int Val
{
get => val;
set
{
val = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush brush = new SolidBrush(PForegroundColor);
float percent = val / 100f;
Rectangle rect = this.ClientRectangle;
rect.Width = (int)((float)rect.Width * percent);
rect.Height = this.Height;
g.FillRectangle(brush, rect);
brush.Dispose();
g.Dispose();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Name = "ProgressBarControl";
this.Size = new System.Drawing.Size(250, 30);
this.ResumeLayout(false);
}
}
}
添加代码完成后,就完成了基本的操作了,下面开始使用
三、使用方法
先点击Form1 界面,在工具箱就可以看到 命名空间对应的自定义组件了

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

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

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

改变进度条到进度,使用下面代码即可
progressBarControl1.Val = 50;效果:

本案例源码:点击下载
结束
如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢
end
边栏推荐
- How to set ulimit value for SYSTEMd service in easycvr?
- What is the API gateway architecture? What are the common gateway types?
- Source code analysis of jmeter5.1 core class testcompiler
- Analysis and application of ThreadLocal source code
- The latest February activity # 1 core 2G first year: 38 yuan / year! 2-core 4G light weight RMB 74 / year! Mysql database 19.9 yuan / year!!
- How to use data warehouse to create time series
- Website construction is not set to inherit the superior column. How to find a website construction company
- Heat transfer oil electric heater
- Notes to nodejs (II)
- 2021-12-10: which can represent a 64 bit floating point number or a 64 bit signed integer
猜你喜欢

Phpmailer sends mail PHP

Face and lining of fresh food pre storage

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

混沌工程,了解一下

Ambire 指南:Arbitrum 奥德赛活动开始!第一周——跨链桥

蚂蚁集团自研TEE技术通过国家级金融科技产品认证
Mysql中的触发器定义及语法介绍
Trigger definition and syntax introduction in MySQL

Section 29 basic configuration case of Tianrongxin topgate firewall

什么是免疫组织化学实验? 免疫组织化学实验
随机推荐
[js] remove the extra zero after the decimal point
Remember a compose version of Huarong Road, you deserve it!
百万消息量IM系统技术要点分享
How to judge the video frame type in h265 in golang development
蚂蚁获FinQA竞赛冠军,在长文本数值推理AI技术上取得突破
什么是免疫组织化学实验? 免疫组织化学实验
SQL语句中EXISTS的详细用法大全
Isolement des transactions MySQL
MySQL transaction isolation
MySQL事務隔離
C language picture transcoding for performance testing
CS1.6 service startup tutorial
What are the processes, levels, stages and key points of requirements analysis in software development
Phpmailer sends mail PHP
[JS reverse hundred examples] the first question of the anti crawling practice platform for netizens: JS confusion encryption and anti hook operation
Website construction column setting form which website construction company is better
VNC multi gear resolution adjustment, 2008R2 setting 1280 × 1024 resolution
Aicon2021 | AI technology helps content security and promotes the healthy development of Internet Environment
Operation and maintenance failure experience sharing
How to choose the server for website construction, including which servers and how much to rent