当然ですが。
これ以降の内容は全部 pre alpha 1 の環境が前提になっていますので、ご注意くだされ。
とりあえず結構きついのがCLR上の列挙体が今のところ正しく取り扱えないので、WindowsFormのDockプロパティなどが設定できないってこと。
その辺が問題なければさくっと書けそうなところが厳しいですね。
あと、イベント記述方法に問題があるのか、同一クラスのオブジェクトが複数ある場合、先に宣言したイベントのみが有効になっているっぽいです。回避方法がないので、どうしようもない...
ともあれ、イロイロ試してみたブラウザプログラムのソースを以下に放置しておきます(^^;
]]> require 'System.Windows.Forms' require 'System.Drawing' # CLRエイリアス Form = System::Windows::Forms::Form Panel = System::Windows::Forms::Panel Label = System::Windows::Forms::Label TextBox = System::Windows::Forms::TextBox Button = System::Windows::Forms::Button MessageBox = System::Windows::Forms::MessageBox MainMenu = System::Windows::Forms::MainMenu MenuItem = System::Windows::Forms::MenuItem Point = System::Drawing::Point # 土台となるフォーム browserForm = Form.new() [browserForm].each do |f| f.Text = 'IronRubyでブラウザを動かすにょろん' f.Width = 800 f.Height = 600 end # ヘッダパネル headerPanel = Panel.new() [headerPanel].each do |c| # 列挙体は動かない orz # c.Dock = System::Windows::Forms::DockStyle.Top c.Location = Point.new(0, 0) c.Height = 24 c.Width = 780 end # ボディパネル bodyPanel = Panel.new() [bodyPanel].each do |c| # 列挙体は動かない orz # c.Dock = System::Windows::Forms::DockStyle.Fill c.Location = Point.new(0, 24) c.Height = 560 c.Width = 780 end # アドレス入力欄 addressLabel = Label.new() [addressLabel].each do |c| c.Text = 'アドレス' c.Location = Point.new(0, 0) c.Height = 24 c.Width = 48 end addressText = TextBox.new() [addressText].each do |c| c.Text = 'http://www.yahoo.co.jp/' c.Location = Point.new(48, 0) c.Width = 544 end # 移動ボタン moveButton = Button.new() [moveButton].each do |c| c.Text = '移動' c.Location = Point.new(592, 0) c.Width = 48 end # ブラウザ browserCtrl = System::Windows::Forms::WebBrowser.new() [browserCtrl].each do |c| # 列挙体は動かない orz # c.Dock = System::Windows::Forms::DockStyle.Fill c.Location = Point.new(0, 0) c.Height = 456 c.Width = 640 end # Event記述 moveButton.Click do|sender, args| browserCtrl.Navigate(addressText.Text) end # Event記述 browserCtrl.Navigated do|sender, args| addressText.Text = sender.Url.ToString browserForm.Text = sender.DocumentTitle end # 列挙体問題でDockとかつかえないから # VB2時代の悪しき伝統を泣く泣く活用 browserForm.Resize do|sender, args| headerPanel.Width = sender.Width bodyPanel.Width = sender.Width bodyPanel.Height = sender.Height - headerPanel.Height browserCtrl.Width = sender.Width - 24 browserCtrl.Height = sender.Height - (64 + headerPanel.Height) end headerPanel.Resize do|sender, args| addressText.Width = sender.Width - (addressLabel.Width + moveButton.Width) moveButton.Location = Point.new(sender.Width - moveButton.Width, 0) end # とりあえずメニューもやってみる mainMenu = MainMenu.new() menuFile = MenuItem.new() menuFile.Name = 'menuFile' menuFile.Text = 'ファイル(&F)' menuQuit = MenuItem.new() menuQuit.Name = 'menuQuit' menuQuit.Text = '終了(&Q)' menuFile.MenuItems.Add menuQuit menuHelp = MenuItem.new() menuHelp.Name = 'menuHelp' menuHelp.Text = 'ヘルプ(&H)' menuAbout = MenuItem.new() menuAbout.Name = 'menuAbout' menuAbout.Text = 'このプログラムについて(&A)' menuHelp.MenuItems.Add menuAbout menuAbout.Click do|sender, args| MessageBox.Show("IronRuby Sample Script\n(C)DonaDona(KITAO Masato)") end # なぜか動かない!! menuQuit.Click do|sender, args| browserForm.Close end mainMenu.MenuItems.Add menuFile mainMenu.MenuItems.Add menuHelp browserForm.Menu = mainMenu # コントロールを追加 cs = headerPanel.Controls cs.Add addressLabel cs.Add addressText cs.Add moveButton # コントロールを追加 bodyPanel.Controls.Add browserCtrl # フォームにコントロールの追加 browserForm.Controls.Add headerPanel browserForm.Controls.Add bodyPanel browserForm.ShowDialog]]>
とりあえず以下の手順でビルドしてみました。
Microsoft Visual Studio 2005→
Visual Studio Tools→
Visual Studio 2005 コマンドプロンプトを開く。
生成されたrbx.exeを実行してみると次のような画面になりますよ。
(この実行は普通のコマンドプロンプトでOK)
D:\IronRuby-Pre-Alpha1\Bin\Release>rbx IronRuby Pre-Alpha (1.0.0.0) on .NET 2.0.50727.832 Copyright (c) Microsoft Corporation. All rights reserved. >>> puts 'Hello, World' => nil >>>
実は、最初間違ってput 'Hello, World'とかやったんですが、irbのようにNoMethodErrorが出るのではなくて、.NET CLRのSystem.MissingMethodExceptionと共に大量のスタックトレースが吐き出されました(^^;
せっかくなのでWindowsFormを操作してみます。
(荒井省三さんのblogを参考に試してみました。)
>>> require 'System.Windows.Forms' => true >>> f = System::Windows::Forms::Form.new() => #<Form:0000002c> >>> f.ShowDialog() => #<DialogResult:0000002d> >>>
ボタンを配置して、イベントを発生させてみましょう。
IronRuby Pre-Alpha (1.0.0.0) on .NET 2.0.50727.832 Copyright (c) Microsoft Corporation. All rights reserved. >>> require 'System.Windows.Forms' => true >>> require 'System.Drawing' => true >>> f = System::Windows::Forms::Form.new() => #<Form:0000002b> >>> b = System::Windows::Forms::Button.new() => #<Button:0000002c> >>> b.Location = System::Drawing::Point.new(50,100) => nil >>> b.Text = '日本語' => nil >>> # ref. http://www.hanselman.com/blog/WPFSampleInIronRubyTalkingViaCToWesabe.aspx => nil >>> b.click do|sender, args| ... System::Windows::Forms::MessageBox.Show(sender.Text) ... end => nil >>> f.Controls.Add b => nil >>> f.ShowDialog => #<DialogResult:0000002d> >>>
もちろん上記コードをソースファイルにして読み込ませることもできます。
っつーか出来なかったら終わってますが(^^;
require 'System.Windows.Forms' require 'System.Drawing' f = System::Windows::Forms::Form.new() b = System::Windows::Forms::Button.new() b.Location = System::Drawing::Point.new(50,100) b.Text = '日本語' b.click do|sender, args| System::Windows::Forms::MessageBox.Show(sender.Text) end f.Controls.Add b f.ShowDialog
D:\IronRuby-Pre-Alpha1\Bin\Release>rbx test.rb
ちなみにコード中、律儀にSystem::Windows::Forms::MessageBox.Show
とかやっていますが、別に几帳面なのではなくこうしないとエラーになるから仕方なくやっているだけです(^^;
まぁ、まだプレアルファ版ということでこの辺の面倒くさいあたりが改善されていくことを祈るばかりですね。
更新内容は以下の通りです。(http://hsj.jp/gme/#Ver.1.06)
ダウンロードについては http://hsj.jp/gme/#Download からどうぞ。
]]>