gpt4 book ai didi

ruby - 如何在 wxRuby 应用程序中恢复窗口大小和位置?

转载 作者:数据小太阳 更新时间:2023-10-29 08:56:06 25 4
gpt4 key购买 nike

有谁知道在 wxRuby 中恢复窗口位置和大小的示例代码?

最佳答案

YAML 是序列化 prefs 的更好选择,但这里我只是在 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

关于ruby - 如何在 wxRuby 应用程序中恢复窗口大小和位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/173581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com