■
すっかりVS.NETを使わなくなったので、GUIプログラミングもWinForms以外でやってみようか、GTK#に手を出してみることにしました。GTK#のチュートリアルみたいなのが見つからなかったので、GTK+ 2.0 チュートリアルを読みながら試行錯誤中。
「GTK 版の Hello World」をGTK#とNemerleで書き直してみる。
using System; using System.Console; using Gtk; using GLib; module M { greeting : string = "Hello, World"; OnDelete (_o : object, e : DeleteEventArgs) : void { def sa = e : SignalArgs; WriteLine ("delete event occurred"); // falseにするとデフォルトのdeleteイベントも実行され、ウィンドウが閉じる // ここではtrue (処理済み)にしている sa.RetVal = true; } OnDestroy (_o : object, _e : EventArgs) : void { // アプリケーションを終了する Application.Quit (); } public Main () : void { Application.Init (); // ウィンドウの生成 def window = Window (WindowType.Toplevel); window.BorderWidth = 10u; window.DefaultSize = Gdk.Size (200, 50); window.Title = greeting; window.DeleteEvent += OnDelete; window.DestroyEvent += OnDestroy; // ボタンの生成 def button = Button (greeting); // ボタンをクリックするとコンソールにHello, Worldを表示して終了する button.Clicked += fun (_o : object, _e : EventArgs) { WriteLine (window.Title); }; button.Clicked += OnDestroy; // ウィンドウにボタンを追加 window.Add (button); // ウィンドウに含まれる全てのウィジットを表示 window.ShowAll (); // メッセージループに入る Application.Run (); } }
これをhelloworld.nとして保存し、
$ ncc -pkg:gtk-sharp-2.0 -o:helloworld.exe helloworld.n $ mono helloworld.exe
で、ボタンが1つ貼り付けられたウィンドウが表示され、ボタンをクリックするとコンソールにHello, Worldと表示し、終了します。