Skip to content

Instantly share code, notes, and snippets.

@smison
Last active November 26, 2015 11:08
Show Gist options
  • Save smison/19815dff9470c61867df to your computer and use it in GitHub Desktop.
Save smison/19815dff9470c61867df to your computer and use it in GitHub Desktop.
windowsでアクティブなウィンドウのタイトルを取得する
$ gem install win32-api
※参考 http://stackoverflow.com/questions/14220386/ruby-win32-api-getfocus-and-getwindowtext
require 'win32/api'
include Win32
while true do
hwnd = GetActiveWindow = API.new('GetForegroundWindow', 'V', 'L', 'user32').call
GetWindowText = API.new('GetWindowText', 'LPI', 'I', 'user32')
GetWindowTextLength = API.new('GetWindowTextLength', 'L', 'I', 'user32')
buf_len = GetWindowTextLength.call(hwnd)
str = ' ' * (buf_len + 1)
# Retreive the text.
result = GetWindowText.call(hwnd, str, str.length)
puts str.strip
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment