前回告知した通り、WIndows 11に標準でついてくるMSBuild.exeを使うスタンドアローンのウィンドウプログラムを作ります。
【Disclaimer】
尚、前回も書きましたが、このプログラムの機能は「短いバッチ処理プログラム」と変わらず、余り大したことをしないので期待しないでください...(汗;)
先ず(これはBCCForm and BCCDSkeltonからの私のプログラミング作法なんですが)、
(1)完成形をイメージして、
(2)必要なリソースを作成し、
(3)ドンガラ(スケルトン)を作ってから、
(4)必要な実体的機能を実装します。
今回は(1)~(3)までをやりましょう。
先ずは「完成形」ですが、こんな感じ。単なるツールバー、ステータスバー付きのSDI(SDI - Single Document Interface)ウィンドウです。クライアントエリアにはエディットボックス(TextBoxクラスコントロール)を貼りつけておきます。
この完成形で使うC#のリソースはツールバービットマップと(システム)アイコンであり、前者はBCCForm and BCCSkeltonパッケージのサンプルビットマップとTBEditorツールを使い、
↑ようにし、後者は自作C#アイコンにしました。(アイコンであればなんでも結構です。)
そしてドンガラ(SDIスケルトン)のコーディングです。いつものようにコメントと解説:で説明します。
【MSBuilder.cs】
///////////////////////////////
// MSBuilder.cs
// that calls and uses MSBuild
// Copyright (c) 2025 by ysama
///////////////////////////////
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection; //Assemblyを使う為
using System.Resources; //リソース関係クラス等の使用の為
using System.IO; //File.ReadAllTextとFileStream使用の為
using System.Text; //Encoding.GetEncoding使用の為
using System.Text.RegularExpressions; //Regex.Splitを使用の為
using System.Diagnostics; //Process使用の為
namespace MSBuilder
{
public class MSB : Form //解説:Formクラスから派生させる
{
//クラスメンバー変数
string MSBPath = @"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\"; //MSBuild.exeへのパス既定値
string TargetFile = ""; //ビルドするcsprojまたはslnファイル名(初期値は空文、以下同じ)
string MSBSwitch = ""; //MSBuildのスイッチ文
string MSBDataFile = ""; //MSBuilderのデータファイル(*.msb)
//クラスコントロール変数
ToolStrip toolStrip; //ツールバー
ToolStripButton[] toolStripButton; //ツールバーボタン
StatusStrip statusStrip; //ステータスバー
ToolStripStatusLabel[] tssl; //ステータスバーラベル
TextBox txtBox; //出力用テキストボックス
[STAThread] //解説:忘れずに...
public static void Main() //エントリーポイント
{
Application.Run(new MSB());
}
public MSB() //コンストラクター
{
Assembly myOwn = Assembly.GetEntryAssembly();
this.Icon = Icon.ExtractAssociatedIcon(myOwn.Location); //プログラムアイコンをフォームにつける
this.Size = new Size(640, 480);
this.MinimumSize = new Size(320, 190);
this.Text = "MSBuilder";
this.Load += MSB_Load;
}
private void MSB_Load(object sender, EventArgs e)
{
//フォームのレイアウトを一時停止
this.SuspendLayout();
//メインメニュー作成
MainMenu menu = new MainMenu();
Menu = menu;
//メインメニューアイテム付加
MenuItem miFile = new MenuItem(); //「ファイル」メニュー
miFile.Text = "ファイル(&F)";
miFile.Index = 0;
menu.MenuItems.Add(miFile);
MenuItem miBuild = new MenuItem(); //「ビルド」メニュー
miBuild.Text = "ビルド(&B)";
miBuild.Index = 1;
menu.MenuItems.Add(miBuild);
MenuItem miHelp = new MenuItem(); //「ヘルプ」メニュー
miHelp.Text = "ヘルプ(&H)";
miHelp.Index = 2;
menu.MenuItems.Add(miHelp);
//サブメニューアイテム付加
MenuItem miOpen = new MenuItem(); //「開く」メニューアイテム
miOpen.Text = "開く(&O)";
miOpen.Index = 0;
miOpen.Click += OnOpen_Click;
miOpen.Shortcut = Shortcut.CtrlO;
miFile.MenuItems.Add(miOpen);
MenuItem miSave = new MenuItem(); //「保存」メニューアイテム
miSave.Text = "保存(&S)";
miSave.Index = 1;
miSave.Click += OnSave_Click;
miSave.Shortcut = Shortcut.CtrlS;
miFile.MenuItems.Add(miSave);
miFile.MenuItems.Add("-"); //セパレーター
MenuItem miSetUp = new MenuItem(); //「設定」メニューアイテム
miSetUp.Text = "設定(&U)";
miSetUp.Index = 2;
miSetUp.Click += OnSetUp_Click;
miSetUp.Shortcut = Shortcut.CtrlU;
miFile.MenuItems.Add(miSetUp);
miFile.MenuItems.Add("-"); //セパレーター
MenuItem miExit = new MenuItem(); //「終了」メニューアイテム
miExit.Text = "終了(&X)";
miExit.Index = 3;
miExit.Click += OnExit_Click;
miExit.Shortcut = Shortcut.CtrlX;
miFile.MenuItems.Add(miExit);
MenuItem miSwitch = new MenuItem(); //「スイッチ」メニューアイテム
miSwitch.Text = "スイッチ(&W)";
miSwitch.Index = 0;
miSwitch.Click += OnSwitch_Click;
miSwitch.Shortcut = Shortcut.CtrlW;
miBuild.MenuItems.Add(miSwitch);
MenuItem miExec = new MenuItem(); //「実行」メニューアイテム
miExec.Text = "実行(&E)";
miExec.Index = 1;
miExec.Click += OnExec_Click;
miExec.Shortcut = Shortcut.CtrlE;
miBuild.MenuItems.Add(miExec);
MenuItem miHowtoUse = new MenuItem(); //「使い方」メニューアイテム
miHowtoUse.Text = "使い方(&U)";
miHowtoUse.Index = 0;
miHowtoUse.Click += OnHowtoUse_Click;
miHelp.MenuItems.Add(miHowtoUse);
MenuItem miVer = new MenuItem(); //「バージョン」メニューアイテム
miVer.Text = "バージョン(&N)";
miVer.Index = 1;
miVer.Click += OnVersion_Click;
miVer.Shortcut = Shortcut.CtrlN;
miHelp.MenuItems.Add(miVer);
//ツールバーとステータスバーの設定
//ToolStripクラスインスタンスの生成
this.toolStrip = new ToolStrip();
//ツールバーのレイアウトを一時停止
this.toolStrip.SuspendLayout();
//ToolStripButton配列を作成
this.toolStripButton = new ToolStripButton[6];
//本プログラムの埋め込みリソースのリソースマネージャーを作成
Assembly asm = Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager("MSBuilder", asm);
//ツールバービットマップの読み込み
ImageList imgList = new ImageList();
imgList.ImageSize = new Size(16, 15);
imgList.Images.AddStrip((Bitmap)rm.GetObject("ToolBar"));
imgList.TransparentColor = Color.White;
//ToolStripButton[0]を作成
this.toolStripButton[0] = new ToolStripButton();
this.toolStripButton[0].Text = "開く(&O)"; //テキスト設定
this.toolStripButton[0].Image = (Bitmap)imgList.Images[0]; //画像設定
this.toolStripButton[0].DisplayStyle = ToolStripItemDisplayStyle.Image; //画像表示のみ
this.toolStripButton[0].Click += OnOpen_Click; //Clickイベントハンドラ追加
this.toolStrip.Items.Add(this.toolStripButton[0]); //ボタンを追加
//ToolStripButton[1]を作成
this.toolStripButton[1] = new ToolStripButton();
this.toolStripButton[1].Text = "保存(&S)"; //テキスト設定
this.toolStripButton[1].Image = (Bitmap)imgList.Images[1]; //画像設定
this.toolStripButton[1].DisplayStyle = ToolStripItemDisplayStyle.Image; //画像表示のみ
this.toolStripButton[1].Click += OnSave_Click; //Clickイベントハンドラ追加
this.toolStrip.Items.Add(this.toolStripButton[1]); //ボタンを追加
//セパレーターを挿入
this.toolStrip.Items.Add(new ToolStripSeparator());
//ToolStripButton[2]を作成
this.toolStripButton[2] = new ToolStripButton();
this.toolStripButton[2].Text = "終了(&X)"; //テキスト設定
this.toolStripButton[2].Image = (Bitmap)imgList.Images[2]; //画像設定
this.toolStripButton[2].DisplayStyle = ToolStripItemDisplayStyle.Image; //画像表示のみ
this.toolStripButton[2].Click += OnExit_Click; //Clickイベントハンドラ追加
this.toolStrip.Items.Add(this.toolStripButton[2]); //ボタンを追加
//セパレーターを挿入
this.toolStrip.Items.Add(new ToolStripSeparator());
//ToolStripButton[3]を作成
this.toolStripButton[3] = new ToolStripButton();
this.toolStripButton[3].Text = "実行(&E)"; //テキスト設定
this.toolStripButton[3].Image = (Bitmap)imgList.Images[3]; //画像設定
this.toolStripButton[3].DisplayStyle = ToolStripItemDisplayStyle.Image; //画像表示のみ
this.toolStripButton[3].Click += OnExec_Click; //Clickイベントハンドラ追加
this.toolStrip.Items.Add(this.toolStripButton[3]); //ボタンを追加
//セパレーターを挿入
this.toolStrip.Items.Add(new ToolStripSeparator());
//ToolStripButton[4]を作成
this.toolStripButton[4] = new ToolStripButton();
this.toolStripButton[4].Text = "使い方(&U)"; //テキスト設定
this.toolStripButton[4].Image = (Bitmap)imgList.Images[4]; //画像設定
this.toolStripButton[4].DisplayStyle = ToolStripItemDisplayStyle.Image; //画像表示のみ
this.toolStripButton[4].Click += OnHowtoUse_Click; //Clickイベントハンドラ追加
this.toolStrip.Items.Add(this.toolStripButton[4]); //ボタンを追加
//ToolStripButton[5]を作成
this.toolStripButton[5] = new ToolStripButton();
this.toolStripButton[5].Text = "バージョン情報(&V)"; //テキスト設定
this.toolStripButton[5].Image = (Bitmap)imgList.Images[5]; //画像設定
this.toolStripButton[5].DisplayStyle = ToolStripItemDisplayStyle.Image; //画像表示のみ
this.toolStripButton[5].Click += OnVersion_Click; //Clickイベントハンドラ追加
this.toolStrip.Items.Add(this.toolStripButton[5]); //ボタンを追加
//ツールバーの設定
this.Controls.Add(this.toolStrip);
//ツールバーのレイアウトを再開
this.toolStrip.ResumeLayout(false);
this.toolStrip.PerformLayout();
//StatusStripクラスインスタンスの生成
this.statusStrip = new StatusStrip();
//ステータスバーにパネルとテキストを追加
tssl = new ToolStripStatusLabel[2];
tssl[0] = new ToolStripStatusLabel();
tssl[0].BorderSides = ToolStripStatusLabelBorderSides.All;
tssl[0].BorderStyle = Border3DStyle.SunkenInner;
tssl[0].BackColor = SystemColors.Control;
tssl[0].Text = "MSBuilder Ver. 1.0";
tssl[0].AutoSize = true;
tssl[0].TextAlign = ContentAlignment.MiddleLeft;
tssl[1] = new ToolStripStatusLabel();
tssl[1].BorderSides = ToolStripStatusLabelBorderSides.All;
tssl[1].BorderStyle = Border3DStyle.SunkenInner;
tssl[1].BackColor = SystemColors.Control;
tssl[1].TextAlign = ContentAlignment.MiddleLeft;
tssl[1].Text = "(ビルドするファイル名)";
tssl[1].ToolTipText = "(ビルドするファイル名)"; //ToolTip設定
tssl[1].Spring = true;
statusStrip.Items.AddRange(tssl);
statusStrip.ShowItemToolTips = true; //ToolTip表示
this.Controls.Add(this.statusStrip); //StatusStrip(ステータスバー)を追加
//出力用テキストボックス
txtBox = new TextBox();
//位置(解説:ツールバーの高さだけ調整する)
txtBox.Location = new Point(0, this.toolStrip.Height);
//サイズ(解説:ツールバー、ステータスバーの高さだけ調整する)
txtBox.Width = ClientSize.Width;
txtBox.Height = ClientSize.Height - this.toolStrip.Height - this.statusStrip.Height;
//アンカー(解説:左上と右下で全体がウィンドウサイズの変化につれて変化する)
txtBox.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right);
//その他プロパティ(解説:複数行表示で、折り返しなくスクロールバー付きにする)
txtBox.AcceptsReturn = true;
txtBox.AcceptsTab = true;
txtBox.Multiline = true;
txtBox.ScrollBars = ScrollBars.Both;
txtBox.WordWrap = false;
//FormにTextBoxを追加
this.Controls.Add(txtBox);
//メインフォームのレイアウトを再開
this.ResumeLayout(false);
this.PerformLayout();
}
//終了処理
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e); //解説:標準処理をそのまま残す
DialogResult dr = MessageBox.Show("終了しますか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(dr == DialogResult.No)
{
e.Cancel = true;
}
}
//「ファイルを開く」処理
private void OnOpen_Click(object sender, EventArgs e)
{
MessageBox.Show("ゴメンナサイ。まだできていません。", "工事中", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
//「保存」処理
private void OnSave_Click(object sender, EventArgs e)
{
MessageBox.Show("ゴメンナサイ。まだできていません。", "工事中", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
//「設定」処理
private void OnSetUp_Click(object sender, EventArgs e)
{
MessageBox.Show("ゴメンナサイ。まだできていません。", "工事中", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
//「終了」処理
private void OnExit_Click(object sender, EventArgs e)
{
Close(); //修了確認はOnClosingメソッドで行う
}
//「スイッチ」処理
private void OnSwitch_Click(object sender, EventArgs e)
{
MessageBox.Show("ゴメンナサイ。まだできていません。", "工事中", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
//「実行」処理
private void OnExec_Click(object sender, EventArgs e)
{
MessageBox.Show("ゴメンナサイ。まだできていません。", "工事中", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
//「使い方」処理
private void OnHowtoUse_Click(object sender, EventArgs e)
{
MessageBox.Show("ゴメンナサイ。まだできていません。", "工事中", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
//「バージョン」処理
private void OnVersion_Click(object sender, EventArgs e)
{
VersionDlg verDlg = new VersionDlg(this.Icon); //Versionダイアログ表示
verDlg.ShowDialog();
verDlg.Dispose();
}
}
/////////////////////
//Version ダイアログ
/////////////////////
class VersionDlg : Form
{
public VersionDlg(Icon ico)
{
//ダイアログの属性設定
this.Text = "バーション情報";
this.ClientSize = new Size(320, 100);
this.MaximizeBox = false; // 最大化ボタン
this.MinimizeBox = false; // 最小化ボタン
this.ShowInTaskbar = false; //タスクバー上表示
this.FormBorderStyle = FormBorderStyle.FixedDialog; // 境界のスタイル
this.StartPosition = FormStartPosition.CenterParent; // 親フォームの中央に配置
//コントロールの属性設定
Button btnOK = new Button();
btnOK.Size = new Size(40, 28);
btnOK.Location = new Point(ClientSize.Width - btnOK.Width - 10, (ClientSize.Height - btnOK.Height) / 2);
btnOK.Font = new Font("Times New Roman", 10, FontStyle.Bold);
btnOK.Text = "OK";
btnOK.Click += new EventHandler(OnOK_Click);
Label imglabel = new Label();
imglabel.Size = new Size(40, 40);
imglabel.Location = new Point(10, (ClientSize.Height - imglabel.Height) / 2);
imglabel.BorderStyle = BorderStyle.Fixed3D;
imglabel.Image = ico.ToBitmap(); //親のシステムアイコン
Label label = new Label();
label.Size = new Size(ClientSize.Width - imglabel.Width - btnOK.Width - 40, ClientSize.Height- 20);
label.Location = new Point(imglabel.Width + 20, (ClientSize.Height - label.Height) / 2);
label.BorderStyle = BorderStyle.Fixed3D;
label.Text = "MSBuilder Version 1.0\r\nCopyright (c) 2025 by Ysama\r\n(written in Microsoft C#)";
label.TextAlign = ContentAlignment.MiddleCenter;
label.Font = new Font("Times New Roman", 10, FontStyle.Bold);
this.Controls.Add(btnOK);
this.Controls.Add(imglabel);
this.Controls.Add(label);
}
private void OnOK_Click(object sender, EventArgs e)
{
Close();
}
}
}
この「ドンガラ」は"SDISkelton.cs"とでもして持っていると、次からSDIウィンドウのスケルトンをコーディングする必要がなくなりますね。(その発想がBCCForm and BCCSkeltonの存在意義だったのですが...因みに、この「ドンガラ」状態でもウィンドウにテキストを書いて、表示させることが出来ます。)
では、次回にこのプログラムに、
(1)ビルド用ファイル(*.csprojや*.sln)を読み込ませ、ウィンドウに表示させる。
(2)MSBuildのオプション(スイッチ文)を与えてビルド(MSBuild.exeを実行)する。
(3)上記(1)、(2)のファイルパスとスイッチ文を保存、読み込みできるようにする。
(4)スイッチ文入力用にダイアログを作成する。
(5)使い方のヘルプを表示する。
というようなメイン、サブの機能を追加してゆきます。
それでは、また。
So long.
再見。
또 봐요(トボァヨ)。






