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()で文字列化する。 } //これで完成。
【まとめ】
・各クラスと、プロパティをリファレンスから見てちょっとずつ試す。