Skip to content

Commit

Permalink
allow it to search by classname as well as title
Browse files Browse the repository at this point in the history
  • Loading branch information
rdp committed Aug 19, 2010
1 parent a0403f2 commit b9b9d3a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/win32/screenshot/bitmap_maker.rb
Expand Up @@ -65,12 +65,14 @@ class << self
attach_function :release_dc, :ReleaseDC,
[:long, :long], :int




EnumWindowCallback = FFI::Function.new(:bool, [ :long, :pointer ], { :convention => :stdcall }) do |hwnd, param|
searched_window = WindowStruct.new param
title = Util.window_title(hwnd)
if(searched_window[:search_class] != 0)
title = Util.window_class(hwnd)
else
title = Util.window_title(hwnd)
end
if title =~ Regexp.new(searched_window[:title].read_string) && window_visible(hwnd)
searched_window[:hwnd] = hwnd
false
Expand All @@ -81,10 +83,11 @@ class << self

class WindowStruct < FFI::Struct
layout :title, :pointer,
:hwnd, :long
:hwnd, :long,
:search_class, :char # boolean
end

def hwnd(window_title)
def hwnd(window_title, search_class = nil)
window = WindowStruct.new
unless window_title.is_a?(Regexp)
window_title = Regexp.escape(window_title.to_s)
Expand All @@ -93,10 +96,11 @@ def hwnd(window_title)
end
window_title = FFI::MemoryPointer.from_string(window_title)
window[:title] = window_title
window[:search_class] = search_class ? 1 : 0
enum_windows(EnumWindowCallback, window.to_ptr)
window[:hwnd] == 0 ? nil : window[:hwnd]
end

def prepare_window(hwnd, pause)
restore(hwnd) if minimized(hwnd)
set_foreground(hwnd)
Expand Down
6 changes: 5 additions & 1 deletion spec/win32_screenshot_spec.rb
Expand Up @@ -72,6 +72,10 @@
[width, height].should == Win32::Screenshot::Util.dimensions_for(hwnd)
end
end

it "can also search by window class name" do
Win32::Screenshot::BitmapMaker.hwnd(/calculator/i).should == Win32::Screenshot::BitmapMaker.hwnd(/calcframe/i, true)
end

it "captures small windows" do
title = /Notepad/
Expand Down Expand Up @@ -140,7 +144,7 @@
[width, height].should == Win32::Screenshot::Util.dimensions_for(hwnd)
end
end

it "captures area of the window with handle" do
hwnd = Win32::Screenshot::BitmapMaker.hwnd(/calculator/i)
Win32::Screenshot.hwnd_area(hwnd, 30, 30, 100, 150) do |width, height, bmp|
Expand Down

0 comments on commit b9b9d3a

Please sign in to comment.