wxruby框架例子1

看代码,学程序
1.hello 例子
require "wx" 
include Wx  

=begin
 第一个参数nil,表示要构造的Frame是没有父窗口的,如果有的话,应该在这儿传送。
    第二个参数 -1,标识
    第三个参数 标题。"The Bare Minimum", 
    第四个参数 定义了Frame在桌面上出现的位置.
    第五个参数 定义Frame的初始大小
    第六个参数,窗体类型。
=end
class MinimalApp < App  
   def on_init  
        myFrame = Frame.new(nil, -1, "test")    #创建frame
        StaticText.new(myFrame,-1,"Hello World")  
        myFrame.show()     #显示
  end  
end 

MinimalApp.new.main_loop  

然后通过exerb生成exe。单独可以运行。

2.创建一个确认对话框
require 'wx'
include Wx

class 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
  end
end

MyApp.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)
  end

  def get_term
    @term.get_value
  end

  def get_regexp
    @regx.get_value
  end

  def get_expand
    @expand.get_value
  end

  def on_do_search
    puts "Search term: %s; regexps: %s; expand: %i" % 
      [ get_term(), get_regexp(), get_expand() ]
  end
end


#主框架处理
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


end

MyApp.new.main_loop()

转载于:https://www.cnblogs.com/zhouwenwu/archive/2012/02/15/2352344.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值