WHITELEAF:Kindle応援サイト

KindleでWEB小説を読もう! Narou.rb 公開中

Ruby/SDLでウィンドウをセンターに

SDL にウィンドウを操作する機能が皆無なので、自分でやる必要があるようです。

# Windows 限定
require "Win32API"

SWP_NOSIZE = 1
SM_CXFULLSCREEN = 0
SM_CYFULLSCREEN = 1

def window_align_center(width, height)
  get_active_window = Win32API.new("user32", "GetActiveWindow", [], 'L')
  set_window_pos = Win32API.new("user32", "SetWindowPos", %w(L L i i i i i), 'i')
  get_system_metrics = Win32API.new("user32", "GetSystemMetrics", %w(i), 'i')
  hwnd = get_active_window.call
  desktop_width = get_system_metrics.call(SM_CXFULLSCREEN)
  desktop_height = get_system_metrics.call(SM_CYFULLSCREEN)
  set_window_pos.call(hwnd, 0, (desktop_width - width) / 2, (desktop_height - height) / 2, 0, 0, SWP_NOSIZE)
end

2年も更新してませんでしたが、再び更新を再開してみます。