自己制作的一个进度条

本文介绍了一个使用C#编写的简单Windows进度条实现方法。通过自定义绘图和线程延时来模拟任务进度更新的过程,并实时显示进度百分比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己写的一个简易win下的进度条:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace GraphicsTest
{
    public delegate double HandlerColor(double x,double y);

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            DrawGo();

            base.OnPaint(e);
        }

        private void DrawGo()
        {
            Graphics dc = this.CreateGraphics();

            Brush RedBrush = new SolidBrush(Color.Red);
           
            Pen BluePen = new Pen(Color.Blue, 1);

            Rectangle rec = new Rectangle(20, 80, 250, 30);
            dc.DrawRectangle(BluePen, rec);

            HandlerColor gg = new HandlerColor(GetPercent);

            int i;
            for (i = 0; i <= rec.Width; i = i+10)
            {
                double res = Handlering(gg, (double)i, rec.Width);
                this.Text = "进度为" + res*100+"%";

                dc.FillRectangle(RedBrush,rec.X,rec.Y,rec.Width*((float)res),rec.Height);
                Thread.Sleep(500);

            }
        }

        private static double Handlering(HandlerColor action, double x, double y)
        {
            double result = action(x, y);

            return result;
        }

        private double GetPercent(double x,double y)
        {
            double value;
            value = x / y;
            return value;
        }
    }
}

相关界面:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值