using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsApplication1 { // Windowsフォームのクラス public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; // コンストラクタ(初期化処理などを呼び出す) public Form1() { // 初期化処理 InitializeComponent(); // コントロールボックスの[閉じる]ボタンの無効化 IntPtr hMenu = GetSystemMenu(this.Handle, 0); RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND); } // Win32 APIのインポート [ DllImport("USER32.DLL") ] private static extern IntPtr GetSystemMenu(IntPtr hWnd, UInt32 bRevert); [ DllImport("USER32.DLL") ] private static extern UInt32 RemoveMenu(IntPtr hMenu, UInt32 nPosition, UInt32 wFlags); // [閉じる]ボタンを無効化するための値 private const UInt32 SC_CLOSE = 0x0000F060; private const UInt32 MF_BYCOMMAND = 0x00000000; // 後処理(使用されているリソースの解放など) protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } // 初期化処理(コントロール属性の設定など) private void InitializeComponent() { // [終了]ボタンの追加 this.button1 = new System.Windows.Forms.Button(); // フォームとコントロールの属性を調整するため、 // フォームのレイアウト処理を一時停止する this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(104, 136); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "終了"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 12); this.ClientSize = new System.Drawing.Size(292, 182); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); // フォームとコントロールの初期化が終わったので、 // レイアウト処理の一時停止を解除する this.ResumeLayout(false); } // アプリケーションのメイン・エントリ・ポイント [STAThread] static void Main() { Application.Run(new Form1()); } // [終了]ボタンが押されたかどうかの終了フラグ private bool bFinished = false; // [終了]ボタンを押されたとき、Windowsフォームを閉じる private void button1_Click(object sender, System.EventArgs e) { bFinished = true; // 終了のためのフラグを立てる this.Close(); // Windowsフォームを閉じる } // Windowsフォームが閉じられる直前に呼ばれるイベント・ハンドラ private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // 終了フラグを立ってなければ、フォームが閉じられるのを阻止 if (bFinished == true) { e.Cancel = false; } else { e.Cancel = true; } } } }