看代码,学程序
1.hello 例子require "wx" include Wx=begin
第一个参数nil,表示要构造的Frame是没有父窗口的,如果有的话,应该在这儿传送。 第二个参数 -1,标识 第三个参数 标题。"The Bare Minimum", 第四个参数 定义了Frame在桌面上出现的位置. 第五个参数 定义Frame的初始大小 第六个参数,窗体类型。=endclass MinimalApp < App def on_init myFrame = Frame.new(nil, -1, "test") #创建frame StaticText.new(myFrame,-1,"Hello World") myFrame.show() #显示 end endMinimalApp.new.main_loop
然后通过exerb生成exe。单独可以运行。2.创建一个确认对话框
require 'wx'include Wxclass MyApp < Wx::App
MENU_DIALOGS = 1000 def on_init() frame = Wx::Frame.new(nil, -1, 'test123(ue)')#创建菜单子项
menu_dialogs = Wx::Menu.new() menu_dialogs.append(MENU_DIALOGS, "&Dialog...\t", 'Dialog')#创建菜单条
menu_bar = Wx::MenuBar.new() menu_bar.append(menu_dialogs, "&mytest") frame.set_menu_bar( menu_bar )frame.set_client_size( Wx::Size.new(200,200))
frame.evt_menu(MENU_DIALOGS) { | e | on_dialog(e) }panel = Wx::Panel.new(frame)
frame.show() end DIALOG_OPTIONS = Wx::NO_DEFAULT|Wx::OK|Wx::CANCEL|Wx::ICON_EXCLAMATION def on_dialog(event) confirm = Wx::MessageDialog.new(nil, "Are you really sure?", "Just checking", DIALOG_OPTIONS ) case confirm.show_modal() when Wx::ID_OK puts "OK" when Wx::ID_CANCEL puts "NOT OK" end endendMyApp.new.main_loop()
3.窗口框架中例子
require 'wx'
include Wx#搜寻框
class SearchDialog < Wx::Dialog def initialize(parent) super( parent, -1, 'Search')sizer = Wx::FlexGridSizer.new(2,4)
#创建lable
sizer.add(Wx::StaticText.new(self, -1, 'Search for'), 0, Wx::ALIGN_CENTRE_VERTICAL|Wx::ALIGN_RIGHT, 4) #创建edit,其中edit为@term @term = Wx::TextCtrl.new(self, -1, 'text') sizer.add(@term, 2, Wx::GROW|Wx::ALL, 4)#创建lable
sizer.add(Wx::StaticText.new(self, -1, 'Expand search'),0, Wx::ALIGN_CENTRE_VERTICAL|Wx::ALIGN_RIGHT, 4)#spin控件
@expand = Wx::SpinCtrl.new( self, -1, "") @expand.set_range(0,250) @expand.set_value(50) sizer.add(@expand, 0, Wx::ALL, 4)#
sizer.add(Wx::StaticText.new(self, -1, ''), 0, Wx::ALIGN_CENTRE, 4) @regx = Wx::CheckBox.new(self, -1, "Use regular expressions") sizer.add(@regx, 0, Wx::ALL, 4)#search button和他响应事件
button = Wx::Button.new(self, -1, 'Search') button.evt_button(button.get_id) { | e | on_do_search() } sizer.add(button, 0, Wx::ALL, 4)#cancel button
button = Wx::Button.new(self, -1, 'Cancel') button.evt_button(button.get_id) { | e | hide() } sizer.add(button, 0, Wx::ALL|Wx::ALIGN_RIGHT, 4) #size 置入dialog框体中 self.set_sizer(sizer) sizer.set_size_hints(self) sizer.fit(self) enddef get_term
@term.get_value enddef get_regexp
@regx.get_value enddef get_expand
@expand.get_value enddef on_do_search
puts "Search term: %s; regexps: %s; expand: %i" % [ get_term(), get_regexp(), get_expand() ] endend #主框架处理class MyApp < Wx::App MENU_DIALOGS = 1000 MENU_EXIT = 1001 def on_init() @frame = Wx::Frame.new(nil, -1, 'Dialog') menu_dialogs = Wx::Menu.new() menu_dialogs.append(MENU_DIALOGS, "&Dialog...\t", 'Dialog') menu_dialogs.append(MENU_EXIT, "&Exit...\t", 'Exit') menu_bar = Wx::MenuBar.new() menu_bar.append(menu_dialogs, "&Dialogs") @frame.set_menu_bar( menu_bar )@frame.set_client_size( Wx::Size.new(400,400) )
@frame.evt_menu(MENU_DIALOGS) { | e | on_dialog(e) } panel = Wx::Panel.new(@frame) @frame.show()end
def on_dialog(event)
@search ||= SearchDialog.new(@frame) @search.show(true) end endMyApp.new.main_loop()