## 指定したウインドウクラスから最後にアクティブだったウインドウのタイトルを取得する ## ## active_title = get_activewin_title( "OpWindow" ) ## active_title.gsub!( / - Opera$/, "" ) #!/usr/bin/env ruby require 'dl/win32' User32 = DL.dlopen("user32") def get_activewin_title( win_class ) # http://homepage2.nifty.com/igus/starthp/ruby_memo.html def get_window_text( hwnd ) buf = "\0" * 500 gWT = Win32API.new( 'user32', 'GetWindowTextA', 'LPI' ,'I' ) gWT.call( hwnd,buf, buf.size ) return buf.unpack( "A*" )[0] end def get_class_name( hwnd ) buf = "\0" * 500 gCN = Win32API.new( 'user32', 'GetClassName', 'LPI', 'I' ) gCN.Call( hwnd,buf, buf.size ) return buf.unpack( "A*" )[0] end hw = Win32API.new( "user32", "GetDesktopWindow", [], "L" ).call ec = User32['EnumChildWindows', '0LPL'] activewin_title = "" enum_child_proc = DL.callback( 'IL' ) do |hwnd2| if activewin_title == "" and get_class_name( hwnd2 ) == win_class winText = get_window_text( hwnd2 ) activewin_title = winText if winText != "" end -1 end ec.call( hw, enum_child_proc, 0 ) return activewin_title end