サブウィンドウの作成
VisualuRubyの勉強 その7
メインウィンドウ上のテキストエリアの文字をサブウィンドウ上に表示。サブウィンドウ上のcloseボタンでサブウィンドウ上のテキストエリアの文字をメインウィンドウ上のテキストエリアに表示。
サブウィンドウはVRModalDialogを継承したクラスで作成(モードレスもあるけど多分使用しないと思う)してVRLocalScreen.modalformで呼ぶと表示される。サブ側の値はcloseメソッドの引数としてメイン側に渡せるのがリファレンスでわかったけど、問題はメイン側のオブジェクトをどうやってサブ側に渡すかが不明。とりあえずvrdialog.rbの中をみてVRLocalScreen.modalformの第5引数にハッシュを使って渡せばよさそうだったので実行したら上手くいったが、これで良いのかがわからない。サブ側ではハッシュoptionsとしてアクセスする。
あとで気がついたけれどもリファレンスにもそれらしきことが書いてありました。
require 'vr/vruby' require 'vr/vrcontrol' require 'vr/vrdialog' class Dialog < VRModalDialog def construct self.caption = 'dialog' self.move(200,150,343,218) addControl(VRButton,'button2',"close",32,144,264,32) addControl(VRText,'text2',"",32,16,252,108,WStyle::WS_VSCROLL) options.each do |k,v| @text2.text += "#{k} : #{v}\n" end end def button2_clicked self.close(@text2.text) end end class MyForm < VRForm def construct self.caption = 'form1' self.move(137,124,343,218) addControl(VRButton,'button1',"button1",32,144,264,32) addControl(VRText,'text1',"Hello",32,16,252,108,WStyle::WS_VSCROLL) end def button1_clicked opt = {:text => @text1.text} # サブウィンドウに渡すオブジェクトのハッシュ ret = VRLocalScreen.modalform(nil,nil,Dialog,nil,opt) @text1.text = ret if ret end end VRLocalScreen.start(MyForm)