Win32\'s FindWindow()
can find a window having the title of \"Untitled - Notepad\", but what if I just want to find a Notepad window but don\'t know whether it
I would follow Eric's advice to use EnumWindows. You can provide Ruby callbacks to Windows API functions through win32-api. Here's an example that was trivially modified from the sample in the win32-api README:
require 'win32/api'
include Win32
# Callback example - Enumerate windows
EnumWindows = API.new('EnumWindows', 'KP', 'L', 'user32')
GetWindowText = API.new('GetWindowText', 'LPI', 'I', 'user32')
EnumWindowsProc = API::Callback.new('LP', 'I'){ |handle, param|
buf = "\0" * 200
GetWindowText.call(handle, buf, 200);
if (!buf.index(param).nil?)
puts "window was found: handle #{handle}"
0 # stop looking after we find it
else
1
end
}
EnumWindows.call(EnumWindowsProc, 'Firefox')
The 1st argument of FindWindow
searches by class name, if you use "Notepad"
(Notepad's main window class name) for this and a null title you would get the 1st matching handle irrespective of its caption.
You almost certainly want to use the EnumWindows function; this function will call you back with a window handle, and then you can use GetWindowText to inspect the window title and find the one you want.
Now, I have no idea how to write a callback function in Ruby, so you'll need some help there.