#!ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2016/01/16 06:05:35 +0900> # # 画像を読み込んで表示してみるテスト require_relative 'lib/dxrubyws' require_relative 'lib/standardgui' module WS # 画像選択ウインドウクラス class ImageSelectWindow < WSWindow # コンストラクタ。初期化処理 def initialize(*args) super # ボタンを生成して登録 x, y, w, h = 4, 4, 120, 24 @btn = WSButton.new(x, y, w, h, "画像読み込み") client.add_control(@btn) @btn.add_handler(:click, self.method(:on_click)) # タイトルバー上の閉じるボタンを無効化 self.window_title.close_button = false # ESCキーで閉じないようにする @key_handler.delete(K_ESCAPE) # 次のウインドウ発生位置 @x_pos = self.x @y_pos = self.y + self.height end # ボタンを押した時の処理 def on_click(obj, tx, ty) # ファイル選択ダイアログを開いてみる filters = [ ["画像ファイル(*.png;*.jpg;*.bmp)", "*.png;*.jpg;*.bmp"], ["すべてのファイル(*.*)", "*.*"] ] filepath = Window.openFilename(filters, "画像ファイルを選択") create_image_window(filepath) if filepath end # 画像を表示するウインドウを生成して画面に追加 def create_image_window(filepath) img = Image.load(filepath) w, h = img.width, img.height bsname = File.basename(filepath) wdw = WS::ImageWindow.new(@x_pos, @y_pos, w + 48, h + 48, bsname) WS.desktop.add_control(wdw) wdw.image = img @x_pos += 32 @y_pos += 32 end end # 画像を表示するだけのウインドウクラス class ImageWindow < WSWindow attr_accessor :image def initialize(*args) super @image = nil end def draw x = (client.image.width - @image.width) / 2 y = (client.image.height - @image.height) / 2 client.image.draw(x, y, @image) if @image super end end end Window.width, Window.height = 1024, 600 wdw = WS::ImageSelectWindow.new(8, 8, 200, 56, "ImageSelectWindow") WS.desktop.add_control(wdw) Window.loop do WS.update end