WindowsGUIツールのベース。ボタンクリックにより処理を実行し、処理状況をプログレスバーに表示。途中キャンセルを可能とする。
namespace ToolBaseType1 { 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.statusStrip = new System.Windows.Forms.StatusStrip(); this.tspbShintyokuritsu = new System.Windows.Forms.ToolStripProgressBar(); this.tsslShinktyokuritsu = new System.Windows.Forms.ToolStripStatusLabel(); this.btnCancel = new System.Windows.Forms.Button(); this.tsslDummy = new System.Windows.Forms.ToolStripStatusLabel(); this.statusStrip.SuspendLayout(); 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(213, 203); 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(15, 13); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(354, 184); this.listBox1.TabIndex = 4; // // statusStrip // this.statusStrip.AutoSize = false; this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.tsslDummy, this.tsslShinktyokuritsu, this.tspbShintyokuritsu}); this.statusStrip.Location = new System.Drawing.Point(0, 229); this.statusStrip.Name = "statusStrip"; this.statusStrip.Size = new System.Drawing.Size(381, 22); this.statusStrip.TabIndex = 5; this.statusStrip.Text = "statusStrip1"; // // tspbShintyokuritsu // this.tspbShintyokuritsu.AutoSize = false; this.tspbShintyokuritsu.Name = "tspbShintyokuritsu"; this.tspbShintyokuritsu.Size = new System.Drawing.Size(120, 16); // // tsslShinktyokuritsu // this.tsslShinktyokuritsu.AutoSize = false; this.tsslShinktyokuritsu.Name = "tsslShinktyokuritsu"; this.tsslShinktyokuritsu.Size = new System.Drawing.Size(60, 17); this.tsslShinktyokuritsu.Text = " 0% 完了"; this.tsslShinktyokuritsu.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // btnCancel // this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnCancel.Location = new System.Drawing.Point(294, 203); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(75, 23); this.btnCancel.TabIndex = 6; this.btnCancel.Text = "キャンセル"; this.btnCancel.UseVisualStyleBackColor = true; this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); // // tsslDummy // this.tsslDummy.Name = "tsslDummy"; this.tsslDummy.Size = new System.Drawing.Size(153, 17); this.tsslDummy.Spring = true; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(381, 251); this.Controls.Add(this.btnCancel); this.Controls.Add(this.statusStrip); this.Controls.Add(this.listBox1); this.Controls.Add(this.btnExecute); this.Name = "MainForm"; this.Text = "ToolName"; this.Load += new System.EventHandler(this.MainForm_Load); this.statusStrip.ResumeLayout(false); this.statusStrip.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button btnExecute; private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.StatusStrip statusStrip; private System.Windows.Forms.ToolStripStatusLabel tsslShinktyokuritsu; private System.Windows.Forms.ToolStripProgressBar tspbShintyokuritsu; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.ToolStripStatusLabel tsslDummy; } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace ToolBaseType1 { public partial class MainForm : Form { BackgroundWorker bw = null; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { this.btnCancel.Enabled = false; } private void btnExecute_Click(object sender, EventArgs e) { this.listBox1.Items.Add("開始"); this.btnExecute.Enabled = false; this.btnCancel.Enabled = true; this.bw = new BackgroundWorker(); this.bw.WorkerReportsProgress = true; this.bw.WorkerSupportsCancellation = true; this.bw.DoWork += new DoWorkEventHandler(bw_DoWork); this.bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); this.bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); 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("キャンセル"); this.btnExecute.Enabled = true; } else { this.listBox1.Items.Add("完了"); this.btnExecute.Enabled = true; this.btnCancel.Enabled = false; } } void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.tsslShinktyokuritsu.Text = string.Format("{0}% 完了", e.ProgressPercentage); this.tspbShintyokuritsu.Value = e.ProgressPercentage; } private void btnCancel_Click(object sender, EventArgs e) { if (!this.btnExecute.Enabled) { this.btnCancel.Enabled = false; this.bw.CancelAsync(); } } } }