kenlistian

勤学多思

  IT博客 :: 首页 :: 新随笔 ::  :: 聚合  :: 管理 ::
  412 随笔 :: 0 文章 :: 23 评论 :: 0 Trackbacks


看代码,学程序
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()





posted on 2008-06-03 11:30 kenlistian 阅读(1001) 评论(0)  编辑 收藏 引用 所属分类: Ruby
只有注册用户登录后才能发表评论。