Skip to content

Commit

Permalink
Merge remote branch 'jarmo/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rdp committed Jul 7, 2010
2 parents fac970c + 4013e4e commit 1c713cb
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 20 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ coverage
rdoc
pkg
.idea
*.bmp
*.png
spec/tmp
8 changes: 7 additions & 1 deletion History.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
= 0.0.5 2010-XX-XX
= 0.0.5 2010-07-07
* Added method window_area for capturing specified area of the window instead of full window (Jarmo Pertman)
Usage: Win32::Screenshot.window_area(title, x1, y1, x2, y2) {|width, height, bmp|}
* Added method foreground_area for capturing area of the foreground (Jarmo Pertman)
Usage: Win32::Screenshot.foreground_area(x1, y1, x2, y2) {|width, height, bmp|}
* Added method desktop_area for capturing area of the visible view (Jarmo Pertman)
Usage: Win32::Screenshot.desktop_area(x1, y1, x2, y2) {|width, height, bmp|}
* Added method hwnd_area for capturing area of the window with specified handle (Jarmo Pertman)
Usage: Win32::Screenshot.hwnd_area(hwnd, x1, y1, x2, y2) {|width, height, bmp|}

== Internal changes
* Removed usage of ShowWindow with parameter SW_SHOW when trying to bring window to front due it's behaviour of resizing window if it was maximized (Jarmo Pertman)
Expand Down
33 changes: 26 additions & 7 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,50 @@ other formats like png.
Win32::Screenshot.foreground do |width, height, bmp|
File.open("picture1.bmp", "wb") {|file| file.puts bmp}
end


# take a screenshot of the area of the foreground where upper left x and y
# coordinates are 0 and 10, width is 100 and height is 200
Win32::Screenshot.foreground_area(0, 10, 100, 200) do |width, height, bmp|
File.open("picture2.bmp", "wb") {|file| file.puts bmp}
end

# take a screenshot of the screen
Win32::Screenshot.desktop do |width, height, bmp|
File.open("picture2.bmp", "wb") {|file| file.puts bmp}
File.open("picture3.bmp", "wb") {|file| file.puts bmp}
end

# take a screenshot of the area of the desktop where upper left x and y
# coordinates are 0 and 10, width is 100 and height is 200
Win32::Screenshot.desktop_area(0, 10, 100, 200) do |width, height, bmp|
File.open("picture4.bmp", "wb") {|file| file.puts bmp}
end

# take a screenshot of the window, which has a text part of it's title
Win32::Screenshot.window("Internet Explorer") do |width, height, bmp|
File.open("picture3.bmp", "wb") {|file| file.puts bmp}
File.open("picture5.bmp", "wb") {|file| file.puts bmp}
end

# take a screenshot of the window, which matches regexp against it's title
Win32::Screenshot.window(/Internet Explorer/) do |width, height, bmp|
File.open("picture4.bmp", "wb") {|file| file.puts bmp}
File.open("picture6.bmp", "wb") {|file| file.puts bmp}
end

# take a screenshot of the area of the window where upper left x and y
# coordinates are 0 and 10, width is 100 and height is 200
Win32::Screenshot.window_area("Internet Explorer", 0, 10, 100, 200) do |width, height, bmp|
File.open("picture5.bmp", "wb") {|file| file.puts bmp}
File.open("picture7.bmp", "wb") {|file| file.puts bmp}
end

# take a screenshot of the window with specified window handle
Win32::Screenshot.hwnd(window_handle) do |width, height, bmp|
File.open("picture6.bmp", "wb") {|file| file.puts bmp}
File.open("picture8.bmp", "wb") {|file| file.puts bmp}
end

# take a screenshot of the area of the window with a window handle
# where upper left x and y
# coordinates are 0 and 10, width is 100 and height is 200
Win32::Screenshot.hwnd_area(window_handle, 0, 10, 100, 200) do |width, height, bmp|
File.open("picture9.bmp", "wb") {|file| file.puts bmp}
end

# convert a screenshot to the png format with RMagick
Expand All @@ -49,7 +68,7 @@ other formats like png.
Win32::Screenshot.hwnd(window_handle) do |width, height, bmp|
img = Magick::Image.from_blob(bmp)
png = img[0].to_blob {self.format = 'PNG'}
File.open("picture7.png", "wb") {|file| file.puts png}
File.open("picture10.png", "wb") {|file| file.puts png}
end

== Copyright
Expand Down
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ Rake::RDocTask.new do |rdoc|
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end

desc "Remove all temporary files"
task :clobber => [:clobber_rdoc, :clobber_rcov] do
rm_r "spec/tmp"
end
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.4
0.0.5
39 changes: 35 additions & 4 deletions lib/win32/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,36 @@ def foreground(&proc)
BitmapMaker.capture_all(hwnd, &proc)
end

# captures desktop
# captures area of the foreground
# where *x1* and *y1* are 0 in the upper left corner and
# *x2* specifies the width and *y2* the height of the area to be captured
def foreground_area(x1, y1, x2, y2, &proc)
hwnd = BitmapMaker.foreground_window
validate_coordinates(hwnd, x1, y1, x2, y2)
BitmapMaker.capture_area(hwnd, x1, y1, x2, y2, &proc)
end

# captures visible view of the screen
#
# to make screenshot of the real desktop, all
# windows must be minimized before
def desktop(&proc)
hwnd = BitmapMaker.desktop_window
BitmapMaker.capture_all(hwnd, &proc)
end

# captures area of the visible view of the screen
# where *x1* and *y1* are 0 in the upper left corner and
# *x2* specifies the width and *y2* the height of the area to be captured
#
# to make screenshot of the real desktop, all
# windows must be minimized before
def desktop_area(x1, y1, x2, y2, &proc)
hwnd = BitmapMaker.desktop_window
validate_coordinates(hwnd, x1, y1, x2, y2)
BitmapMaker.capture_area(hwnd, x1, y1, x2, y2, &proc)
end

# captures window with a *title_query* and waits *pause* (by default is 0.5)
# seconds after trying to set window to the foreground
def window(title_query, pause=0.5, &proc)
Expand All @@ -29,9 +53,7 @@ def window(title_query, pause=0.5, &proc)
# *x2* specifies the width and *y2* the height of the area to be captured
def window_area(title_query, x1, y1, x2, y2, pause=0.5, &proc)
hwnd = window_hwnd(title_query)
validate_coordinates(hwnd, x1, y1, x2, y2)
BitmapMaker.prepare_window(hwnd, pause)
BitmapMaker.capture_area(hwnd, x1, y1, x2, y2, &proc)
hwnd_area(hwnd, x1, y1, x2, y2, pause, &proc)
end

# captures by window handle
Expand All @@ -40,6 +62,15 @@ def hwnd(hwnd, pause=0.5, &proc)
BitmapMaker.capture_all(hwnd, &proc)
end

# captures area of the window with a handle of *hwnd*
# where *x1* and *y1* are 0 in the upper left corner and
# *x2* specifies the width and *y2* the height of the area to be captured
def hwnd_area(hwnd, x1, y1, x2, y2, pause=0.5, &proc)
validate_coordinates(hwnd, x1, y1, x2, y2)
BitmapMaker.prepare_window(hwnd, pause)
BitmapMaker.capture_area(hwnd, x1, y1, x2, y2, &proc)
end

private

def window_hwnd(title_query)
Expand Down
7 changes: 5 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'spec'
require 'spec/autorun'
require 'RMagick'
require 'fileutils'

module SpecHelper
SW_MAXIMIZE = 3
Expand All @@ -24,12 +25,14 @@ module SpecHelper
[:long, :long, :int, :int, :int, :int, :int], :bool

def check_image(bmp, file=nil)
File.open("#{file}.bmp", "wb") {|io| io.write(bmp)} unless file.nil?
temp_dir = File.join(File.dirname(__FILE__), 'tmp')
FileUtils.mkdir temp_dir unless File.exists?(temp_dir)
File.open(File.join(temp_dir, "#{file}.bmp"), "wb") {|io| io.write(bmp)} unless file.nil?
bmp[0..1].should == 'BM'
img = Magick::Image.from_blob(bmp)
png = img[0].to_blob {self.format = 'PNG'}
png[0..3].should == "\211PNG"
File.open("#{file}.png", "wb") {|io| io.puts(png)} unless file.nil?
File.open(File.join(temp_dir, "#{file}.png"), "wb") {|io| io.puts(png)} unless file.nil?
end

def wait_for_programs_to_open
Expand Down
47 changes: 44 additions & 3 deletions spec/win32_screenshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ def cleanup
end
end

it "captures area of the foreground" do
Win32::Screenshot.foreground_area(30, 30, 100, 150) do |width, height, bmp|
check_image(bmp, 'foreground_area')
width.should == 70
height.should == 120
end
end

it "doesn't allow to capture area of the foreground with invalid coordinates" do
lambda {Win32::Screenshot.foreground_area(0, 0, -1, 100) {|width, height, bmp| check_image('foreground2')}}.
should raise_exception("specified coordinates (0, 0, -1, 100) are invalid!")
end

it "captures desktop" do
Win32::Screenshot.desktop do |width, height, bmp|
check_image(bmp, 'desktop')
Expand All @@ -42,6 +55,19 @@ def cleanup
end
end

it "captures area of the desktop" do
Win32::Screenshot.desktop_area(30, 30, 100, 150) do |width, height, bmp|
check_image(bmp, 'desktop_area')
width.should == 70
height.should == 120
end
end

it "doesn't allow to capture area of the desktop with invalid coordinates" do
lambda {Win32::Screenshot.desktop_area(0, 0, -1, 100) {|width, height, bmp| check_image('desktop2')}}.
should raise_exception("specified coordinates (0, 0, -1, 100) are invalid!")
end

it "captures maximized window by window title" do
title = "Internet Explorer"
maximize(title)
Expand Down Expand Up @@ -84,7 +110,7 @@ def cleanup
it "captures area of the window" do
title = /calculator/i
Win32::Screenshot.window_area(title, 30, 30, 100, 150) do |width, height, bmp|
check_image(bmp, 'calc_part')
check_image(bmp, 'calc_area')
width.should == 70
height.should == 120
end
Expand All @@ -95,7 +121,7 @@ def cleanup
hwnd = Win32::Screenshot::BitmapMaker.hwnd(title)
dimensions = Win32::Screenshot::BitmapMaker.dimensions_for(hwnd)
Win32::Screenshot.window_area(title, 0, 0, dimensions[2], dimensions[3]) do |width, height, bmp|
check_image(bmp, 'calc_part_full_window')
check_image(bmp, 'calc_area_full_window')
width.should == dimensions[2]
height.should == dimensions[3]
end
Expand Down Expand Up @@ -125,7 +151,7 @@ def cleanup
should raise_exception("specified coordinates (0, 0, 10, 10000) are invalid!")
end

it "captures by hwnd" do
it "captures by window with handle" do
title = /calculator/i
hwnd = Win32::Screenshot::BitmapMaker.hwnd(title)
Win32::Screenshot.hwnd(hwnd) do |width, height, bmp|
Expand All @@ -136,6 +162,21 @@ def cleanup
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|
check_image(bmp, 'calc_hwnd_area')
width.should == 70
height.should == 120
end
end

it "doesn't allow to capture area of the window with handle with invalid coordinates" do
hwnd = Win32::Screenshot::BitmapMaker.hwnd(/calculator/i)
lambda {Win32::Screenshot.hwnd_area(hwnd, 0, 0, -1, 100) {|width, height, bmp| check_image('desktop2')}}.
should raise_exception("specified coordinates (0, 0, -1, 100) are invalid!")
end

after :all do
Process.kill 9, @notepad
Process.kill 9, @iexplore
Expand Down

0 comments on commit 1c713cb

Please sign in to comment.