win32screenshot 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/lib/win32screenshot.rb +51 -8
- data/lib/win32screenshot/version.rb +1 -1
- data/test/win32screenshot_test.rb +26 -6
- metadata +2 -2
data/History.txt
CHANGED
data/lib/win32screenshot.rb
CHANGED
@@ -9,6 +9,11 @@ module Win32
|
|
9
9
|
|
10
10
|
dlload "kernel32.dll","user32.dll","gdi32.dll"
|
11
11
|
|
12
|
+
USER32 = DL.dlopen("user32")
|
13
|
+
EnumWindows = USER32['EnumWindows', 'IPL']
|
14
|
+
GetWindowTextLength = USER32['GetWindowTextLengthA' ,'LI' ]
|
15
|
+
GetWindowText = USER32['GetWindowTextA', 'iLsL' ]
|
16
|
+
|
12
17
|
SRCCOPY = 0xCC0020
|
13
18
|
GMEM_FIXED = 0
|
14
19
|
DIB_RGB_COLORS = 0
|
@@ -17,6 +22,7 @@ module Win32
|
|
17
22
|
typealias "LPRECT","unsigned int*"
|
18
23
|
|
19
24
|
extern "HWND GetForegroundWindow()"
|
25
|
+
extern "HWND GetDesktopWindow()"
|
20
26
|
extern "BOOL GetWindowRect(HWND, LPRECT)"
|
21
27
|
extern "BOOL GetClientRect(HWND, LPRECT)"
|
22
28
|
extern "HDC GetDC(HWND)"
|
@@ -34,15 +40,10 @@ module Win32
|
|
34
40
|
extern "long DeleteObject(unsigned long)"
|
35
41
|
extern "long DeleteDC(HDC)"
|
36
42
|
extern "long ReleaseDC(long, HDC)"
|
43
|
+
extern "BOOL SetForegroundWindow(HWND)"
|
37
44
|
|
38
45
|
module_function
|
39
|
-
def capture(
|
40
|
-
hScreenDC = getDC(hwnd)
|
41
|
-
|
42
|
-
# Find the dimensions of the window
|
43
|
-
rect = DL.malloc(DL.sizeof('LLLL'))
|
44
|
-
getClientRect(hwnd, rect)
|
45
|
-
x1, y1, x2, y2 = rect.to_a('LLLL')
|
46
|
+
def capture(hScreenDC, x1, y1, x2, y2)
|
46
47
|
w = x2-x1
|
47
48
|
h = y2-y1
|
48
49
|
|
@@ -78,10 +79,52 @@ module Win32
|
|
78
79
|
|
79
80
|
return [w, h, data]
|
80
81
|
end
|
82
|
+
|
83
|
+
def capture_hwnd(hwnd)
|
84
|
+
hScreenDC = getDC(hwnd)
|
85
|
+
|
86
|
+
# Find the dimensions of the window
|
87
|
+
rect = DL.malloc(DL.sizeof('LLLL'))
|
88
|
+
getClientRect(hwnd, rect)
|
89
|
+
x1, y1, x2, y2 = rect.to_a('LLLL')
|
90
|
+
|
91
|
+
capture(hScreenDC, x1, y1, x2, y2)
|
92
|
+
end
|
81
93
|
|
82
94
|
module_function
|
83
95
|
def foreground
|
84
|
-
|
96
|
+
hwnd = getForegroundWindow
|
97
|
+
capture_hwnd(hwnd)
|
98
|
+
end
|
99
|
+
|
100
|
+
module_function
|
101
|
+
def desktop
|
102
|
+
hwnd = getDesktopWindow
|
103
|
+
capture_hwnd(hwnd)
|
104
|
+
end
|
105
|
+
|
106
|
+
module_function
|
107
|
+
def window(title_query, delay=0.1)
|
108
|
+
hwnd = nil
|
109
|
+
|
110
|
+
proc = DL.callback('ILL') do |curr_hwnd, lparam|
|
111
|
+
textLength, a = GetWindowTextLength.call(curr_hwnd)
|
112
|
+
captionBuffer = " " * (textLength+1)
|
113
|
+
t, textCaption = GetWindowText.call(curr_hwnd, captionBuffer, textLength+1)
|
114
|
+
text = textCaption[1].to_s
|
115
|
+
if text =~ title_query
|
116
|
+
hwnd = curr_hwnd
|
117
|
+
0
|
118
|
+
else
|
119
|
+
1
|
120
|
+
end
|
121
|
+
end
|
122
|
+
EnumWindows.call(proc, 0)
|
123
|
+
|
124
|
+
raise "Couldn't find window with title matching #{title_query}" if hwnd.nil?
|
125
|
+
setForegroundWindow(hwnd)
|
126
|
+
sleep(delay)
|
127
|
+
capture_hwnd(hwnd)
|
85
128
|
end
|
86
129
|
end
|
87
130
|
end
|
@@ -1,23 +1,43 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'RMagick'
|
2
4
|
|
5
|
+
# Prereqs for this test:
|
6
|
+
# * Must be run from a command window with dimensions 1600x1200 pixels (160x60 characters, default font)
|
7
|
+
# * Screen res must be 1600x1200
|
8
|
+
# * Must have a maximised Firefox open
|
9
|
+
#
|
3
10
|
class Win32screenshotTest < Test::Unit::TestCase
|
4
11
|
|
5
|
-
def
|
12
|
+
def test_should_capture_foreground
|
6
13
|
width, height, bmp = Win32::Screenshot.foreground
|
7
14
|
assert_equal 1280, width
|
8
15
|
assert_equal 720, height
|
9
|
-
|
16
|
+
assert_image(bmp)
|
10
17
|
end
|
11
18
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
19
|
+
def test_should_capture_desktop
|
20
|
+
width, height, bmp = Win32::Screenshot.desktop
|
21
|
+
assert_equal 1600, width
|
22
|
+
assert_equal 1200, height
|
23
|
+
assert_image(bmp)
|
24
|
+
end
|
15
25
|
|
16
|
-
|
26
|
+
def test_should_capture_window_by_title
|
27
|
+
width, height, bmp = Win32::Screenshot.window(/Firefox/)
|
28
|
+
assert_equal 1600, width
|
29
|
+
assert_equal 1147, height
|
30
|
+
assert_image(bmp, "ff.png")
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def assert_image(bmp, file=nil)
|
35
|
+
assert_equal 'BM', bmp[0..1]
|
17
36
|
img = Magick::Image.from_blob(bmp)
|
18
37
|
png = img[0].to_blob do
|
19
38
|
self.format = 'PNG'
|
20
39
|
end
|
21
40
|
assert_equal "\211PNG", png[0..3]
|
41
|
+
File.open(file, "wb") {|io| io.write(png)} unless file.nil?
|
22
42
|
end
|
23
43
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: win32screenshot
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2006-
|
6
|
+
version: 0.0.2
|
7
|
+
date: 2006-12-02 00:00:00 +01:00
|
8
8
|
summary: Capture Screenshots on Windows with Ruby
|
9
9
|
require_paths:
|
10
10
|
- lib
|