[Implement] Progress Bar

[Website] 判斷是否為數字(轉)


{
    /// <summary>
    /// 應用程式的主要進入點。
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

        Application.EnableVisualStyles(); //此行會顯示走馬燈效果
    }
}

程式進入點


{
    System.Threading.Thread progBarThread = null;
    StartProgressBar spb = new StartProgressBar(0);
    spb.Show(out progBarThread);
    try
    {
        while (true)
        { }
    }
    catch (Exception ex)
    {
        //...
    }
    finally
    {
        try
        {
            if (spb != null)
            {
                spb.CloseForm();
            }
            if (progBarThread != null && progBarThread.IsAlive)
            {
                progBarThread.Abort();
            }
        }
        catch (System.Threading.ThreadAbortException ex2) {/*執行緒已中止*/}
        catch (Exception ex3) {/*Form已關閉*/}
    }
}

啟動 ProgressBar Class


    {
        int type = 0;
        public ProgressBarDialog pb;

        public StartProgressBar(int _type)
        {
            type = _type;
        }

        public void Show(out System.Threading.Thread _thread)
        {
            _thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
            _thread.IsBackground = true;
            _thread.Start();
        }
        void DoWork()
        {
            pb = new ProgressBarDialog(type);
            pb.ShowDialog();
        }

        public void CloseForm()
        {
            try
            {
                pb.Visible = false;
                pb.Close();
                pb.Dispose();
            }
            catch (Exception ex)
            {
                /*Form已關閉*/
            }
        }
    }

ProgressBar Dialog


{
    public ProgressBarDialog(int type)
    {
        System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
        System.Windows.Forms.ProgressBar progressBar1 = new System.Windows.Forms.ProgressBar();
        System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox();
        
        // 
        // label1
        // 
        label1.AutoSize = true;
        label1.Location = new System.Drawing.Point(126, 192);
        label1.Name = "label1";
        label1.Size = new System.Drawing.Size(33, 12);
        label1.TabIndex = 0;
        label1.Text = "label1";
        // 
        // progressBar1
        // 
        progressBar1.Location = new System.Drawing.Point(48, 155);
        progressBar1.Name = "progressBar1";
        progressBar1.Size = new System.Drawing.Size(193, 23);
        progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Marquee; //走馬燈效果
        progressBar1.TabIndex = 1;
        // 
        // pictureBox1 (放入 Gif 檔 http://preloaders.net/)
        // 
        pictureBox1.Image = global::ProgressBar.Properties.Resources._325;
        pictureBox1.Location = new System.Drawing.Point(80, 18);
        pictureBox1.Name = "pictureBox1";
        pictureBox1.Size = new System.Drawing.Size(131, 131);
        pictureBox1.TabIndex = 2;
        pictureBox1.TabStop = false;
        // 
        // ProgressBarDialog
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(pictureBox1);
        this.Controls.Add(progressBar1);
        this.Controls.Add(label1);
        this.Name = "ProgressBarDialog";
        this.Text = "ProgressBarDialog";

        Form.CheckForIllegalCrossThreadCalls = false; // 讓 Thread 之間可互相溝通

        string[] name = new string[] { "Loading...", "Processing..." };

        this.Text = name[type];
        label1.Text = name[type];

        this.Cursor = Cursors.WaitCursor;
    }
}