回 帖 发 新 帖 刷新版面

主题:[讨论]请高手帮忙

C# 多线程应用程序编成
 
如何用进度条显示应用程序窗口的加载进程
 
希望高手帮忙!
Email : dingzhp@163.com

回复列表 (共3个回复)

沙发

小例子,VS03的
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace testProgressBarApp
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ProgressBar MyProgressBar;
        private System.Windows.Forms.TrackBar trackBarSpeed;
        private System.Windows.Forms.Button btnStartStop;
        private System.Windows.Forms.Label lbFinishedPercent;
        private System.ComponentModel.IContainer components;

        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.MyProgressBar = new System.Windows.Forms.ProgressBar();
            this.trackBarSpeed = new System.Windows.Forms.TrackBar();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.btnStartStop = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.lbFinishedPercent = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.trackBarSpeed)).BeginInit();
            this.SuspendLayout();
            // 
            // MyProgressBar
            // 
            this.MyProgressBar.Location = new System.Drawing.Point(16, 24);
            this.MyProgressBar.Name = "MyProgressBar";
            this.MyProgressBar.Size = new System.Drawing.Size(296, 23);
            this.MyProgressBar.Step = 1;
            this.MyProgressBar.TabIndex = 0;
            this.MyProgressBar.Click += new System.EventHandler(this.progressBar1_Click);
            // 
            // trackBarSpeed
            // 
            this.trackBarSpeed.Location = new System.Drawing.Point(0, 152);
            this.trackBarSpeed.Minimum = 1;
            this.trackBarSpeed.Name = "trackBarSpeed";

板凳

            this.trackBarSpeed.Size = new System.Drawing.Size(104, 45);
            this.trackBarSpeed.TabIndex = 1;
            this.trackBarSpeed.Value = 1;
            this.trackBarSpeed.Scroll += new System.EventHandler(this.trackBarSpeed_Scroll);
            // 
            // timer1
            // 
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // btnStartStop
            // 
            this.btnStartStop.Location = new

3 楼

System.Drawing.Point(240, 144);
            this.btnStartStop.Name = "btnStartStop";
            this.btnStartStop.TabIndex = 2;
            this.btnStartStop.Text = "开始";
            this.btnStartStop.Click += new System.EventHandler(this.btnStartStop_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 120);
            this.label1.Name = "label1";
            this.label1.TabIndex = 3;
            this.label1.Text = "设置速度";
            this.label1.Click += new System.EventHandler(this.label1_Click);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(80, 64);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(56, 23);
            this.label2.TabIndex = 4;
            this.label2.Text = "已完成:";
            // 
            // lbFinishedPercent
            // 
            this.lbFinishedPercent.Location = new System.Drawing.Point(152, 64);
            this.lbFinishedPercent.Name = "lbFinishedPercent";
            this.lbFinishedPercent.TabIndex = 5;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(336, 197);
            this.Controls.Add(this.lbFinishedPercent);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnStartStop);
            this.Controls.Add(this.trackBarSpeed);
            this.Controls.Add(this.MyProgressBar);
            this.Name = "Form1";
            this.Text = "testProgressBar";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.trackBarSpeed)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void progressBar1_Click(object sender, System.EventArgs e)
        {
        
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            if ( MyProgressBar.Value == MyProgressBar.Maximum) 
            {
                MyProgressBar.Value = MyProgressBar.Minimum ;
            }
            else
            {
                MyProgressBar.PerformStep ();
            }
            int FinishedPercent;
            FinishedPercent = 100 * ( MyProgressBar.Value - MyProgressBar.Minimum )/(MyProgressBar.Maximum - MyProgressBar.Minimum );
            lbFinishedPercent.Text = Convert.ToInt16 (FinishedPercent).ToString() + "%";

        }

        private void trackBarSpeed_Scroll(object sender, System.EventArgs e)
        {
            timer1.Interval = Convert.ToInt16 (1000 / trackBarSpeed.Value) ;
        }

        private void btnStartStop_Click(object sender, System.EventArgs e)
        { 
            if (timer1.Enabled==true)
            {
                timer1.Enabled = false;
                btnStartStop.Text = "开始";
            }
            else 
            {
                timer1.Enabled = true;
                btnStartStop.Text = "停止";
            }
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
        
        }

        private void label1_Click(object sender, System.EventArgs e)
        {
        
        }
    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册