WindowsGuiToolBase2

WindowsGUIツールのベース。ボタンクリックにより処理を実行し、処理中待ちフォームを表示する。そのフォームのプログレスバーに処理状況を表示。途中キャンセルを可能とする。



namespace ToolBaseType2
{
    partial class MainForm
    {
        /// <summary>
        /// 必要なデザイナ変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナで生成されたコード

        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnExecute = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // btnExecute
            // 
            this.btnExecute.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnExecute.Location = new System.Drawing.Point(205, 126);
            this.btnExecute.Name = "btnExecute";
            this.btnExecute.Size = new System.Drawing.Size(75, 23);
            this.btnExecute.TabIndex = 0;
            this.btnExecute.Text = "実行";
            this.btnExecute.UseVisualStyleBackColor = true;
            this.btnExecute.Click += new System.EventHandler(this.btnExecute_Click);
            // 
            // listBox1
            // 
            this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.listBox1.FormattingEnabled = true;
            this.listBox1.ItemHeight = 12;
            this.listBox1.Location = new System.Drawing.Point(13, 13);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(267, 100);
            this.listBox1.TabIndex = 1;
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 161);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.btnExecute);
            this.Name = "MainForm";
            this.Text = "ToolName";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button btnExecute;
        private System.Windows.Forms.ListBox listBox1;
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ToolBaseType2
{
    public partial class MainForm : Form
    {
        WaitForm waitForm = null;

        BackgroundWorker bw = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Add("開始");

            this.waitForm = new WaitForm();
            this.waitForm.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.waitForm.Shown += new EventHandler(waitForm_Shown);
            this.waitForm.FormClosed += new FormClosedEventHandler(waitForm_FormClosed);
            this.waitForm.ShowDialog(this);
        }

        void waitForm_Shown(object sender, EventArgs e)
        {
            this.bw = new BackgroundWorker();
            this.bw.WorkerReportsProgress = true;
            this.bw.WorkerSupportsCancellation = true;
            this.bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            this.bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            this.bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            this.bw.RunWorkerAsync(100);
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            int bgWorkerArg = (int)e.Argument;
            BackgroundWorker worker = (BackgroundWorker)sender;

            for (int i = 0; i < bgWorkerArg; i++)
            {
                System.Threading.Thread.Sleep(100);

                int percentage = i * 100 / bgWorkerArg;
                worker.ReportProgress(percentage);

                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }

            worker.ReportProgress(100);

            e.Result = "完了";
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                this.listBox1.Items.Add("キャンセル");
            }
            else
            {
                this.listBox1.Items.Add("完了");
            }

            this.btnExecute.Enabled = true;

            Application.DoEvents();
            System.Threading.Thread.Sleep(100);

            this.waitForm.Close();
        }

        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.waitForm.lblShintyokuritsu.Text = string.Format("{0}% 完了", e.ProgressPercentage);
            this.waitForm.pbShintyoku.Value = e.ProgressPercentage;
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            this.bw.CancelAsync();
        }

        void waitForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.bw.CancelAsync();
        }
    }
}
namespace ToolBaseType2
{
    partial class WaitForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pbShintyoku = new System.Windows.Forms.ProgressBar();
            this.lblShintyokuritsu = new System.Windows.Forms.Label();
            this.btnCancel = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // pbShintyoku
            // 
            this.pbShintyoku.Location = new System.Drawing.Point(12, 9);
            this.pbShintyoku.Name = "pbShintyoku";
            this.pbShintyoku.Size = new System.Drawing.Size(160, 20);
            this.pbShintyoku.TabIndex = 0;
            // 
            // lblShintyokuritsu
            // 
            this.lblShintyokuritsu.Location = new System.Drawing.Point(10, 32);
            this.lblShintyokuritsu.Name = "lblShintyokuritsu";
            this.lblShintyokuritsu.Size = new System.Drawing.Size(58, 23);
            this.lblShintyokuritsu.TabIndex = 1;
            this.lblShintyokuritsu.Text = "0% 完了";
            this.lblShintyokuritsu.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(97, 32);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 2;
            this.btnCancel.Text = "キャンセル";
            this.btnCancel.UseVisualStyleBackColor = true;
            // 
            // WaitForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(182, 64);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.lblShintyokuritsu);
            this.Controls.Add(this.pbShintyoku);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "WaitForm";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "WaitForm";
            this.ResumeLayout(false);

        }

        #endregion

        public System.Windows.Forms.ProgressBar pbShintyoku;
        public System.Windows.Forms.Label lblShintyokuritsu;
        public System.Windows.Forms.Button btnCancel;
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ToolBaseType2
{
    public partial class WaitForm : Form
    {
        public WaitForm()
        {
            InitializeComponent();
        }
    }
}