C#でGUIプログラミング

GUIアプリケーション C#より参考



using System;
using System.Drawing;
using System.Windows.Forms;

class Program{
       public static void Main(){
            Application.Run(new Form());
              //Formクラスのインスタンス化して実行
             }

}

csc /target:winexe Program.cs
と、入力しないとウィンドウを出現するたびにコマンドプロンプトが表示されてしまいます。

300×300のウィンドウが出現したと思います。ただ、ここで、フォームの大きさやタイトルテキストなどの細かい設定をするプロパティというものを上記のコードに記述していきたいと思います。


using System;
using System.Drawing;
using System.Windows.Forms;

class Program{
          public static void Main(){
               Form f = new Form();//インスタンス化
        f.Width = 200;
               f.Height = 200;
               f.Text = "C#でGUIプログラミング";
               Application.Run(f);
            }
}

----------------------------------------------------------------
//ただし、オブジェクト指向らしくないので書き直したいと思います。


using System;
using System.Drawing;
using System.Windows.Forms;

class Program{
        public static void Main(){
           Application.Run(new Form1());
                 }
}


class Form1 : Form{
       public Form1(){//おそらくコンストラクタだろう
              this.Width = 200;
              this.Height = 200;
              this.Text = "C#でGUIプログラミング";
              }
}

----------------------------------------------------------------
【ボタンを挿入する】

/*■ボタンを挿入するならば、
・Form1クラス内に、*/
Button button1 = new Button();とButtonクラスの
/*生成をする。

・Form()コンストラクタ内に、*/

	      this.button1.Location = new Point(10, 10);
	      this.button1.Size = new Size(170, 30);
	      this.button1.Text = "ここを押して";
//ボタンのプロパティを入力
	
	      this.Controls.Add(this.button1);
//ボタンをフォームに合体
----------------------------------------------------------------
【クリックするとカウントするプログラム】
//Form1クラスに
   int count = 0;
//Form1コンストラクタに
  this.button1.Click += new EventHandler(this.Button1_Click);
//Form1コンストラクタ外にButton1_Clickメソッドを作り、
void Button1_Click(object sender, EventArgs e)
  {
    this.count++;//イベントを受け付けるとcountをインクリメント
    this.button1.Text = this.count.ToString();//ボタンのテキスト部分にカウントした数字を表示させる。ToString()で文字列化する。
  }
//これで完成。


Application.Run() (Google検索結果)
.NET Framework クラス ライブラリ Application.Run メソッド
.NET Frameworkクラスライブラリの1つのようで。
Application.Run(Form)
で、現在のスレッドで標準のアプリケーションメッセージループの実行を
開始し、指定したフォームを表示する。.NET Frameworkによってサポートされている。


Controls.Add() (Google検索結果)


c# ToString() (Google検索結果)
文字列を数字に変換する、数字を文字列に変換する
数値.ToString()
は、数値を文字列に変換するメソッドだ。


c# EventHandler (Google検索結果)
EventHandler (Google検索結果)


【まとめ】
・各クラスと、プロパティをリファレンスから見てちょっとずつ試す。