2008-10-06 2 views

답변

3

YAML이 환경 설정을 직렬화하기에 더 좋은 선택이 될 것이지만 여기에서는 쉼표로 구분 된 문자열을 prefs.txt 파일에 저장합니다.

require "rubygems" 
require "wx" 
include Wx 

class MyApp < App 
    def on_init 
    left, top, width, height = *prefs 
    position = Point.new(left, top) 
    size = Size.new(width, height) 
    main = Frame.new(nil, -1, "Title", position, size) 

    #when the window closes, save the location 
    main.evt_close do |event| 
     save_window_location(event.get_event_object) 
     event.skip 
    end 
    main.show() 
    end 

    private 

    def save_window_location(frame) 
    self.prefs = [ 
     frame.position.x, 
     frame.position.y, 
     frame.size.width, 
     frame.size.height 
    ] 
    end 

    #load the prefs and return them as an array 
    def prefs 
    location = [] 
    begin 
     location = File.read(prefs_filename).split(',').map{|s| s.to_i} 
    rescue Exception 
     #file didn't exist, or read failed 
    end 
    location = [100, 100, 300, 300] if location.size != 4 
    return location 
    end 

    #save the prefs as a comma-delimited string 
    def prefs=(prefs_array) 
    File.open(prefs_filename, 'w') do |prefs_file| 
     prefs_file << prefs_array.join(",") 
    end 
    end 

    def prefs_filename 
    'prefs.txt' 
    end 
end 

MyApp.new.main_loop 
+0

감사합니다. –

관련 문제