win-gdi32-ruby 0.0.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +6 -2
- data/Rakefile +6 -2
- data/lib/BitmapHelper.dll +0 -0
- data/lib/gdi32/system_const.rb +15 -15
- data/lib/win-gdi32-ruby.rb +133 -81
- metadata +8 -10
- data/lib/ScreenCapture.dll +0 -0
- data/lib/main.rb +0 -26
- data/lib/test.bmp +0 -0
- data/lib/test2.bmp +0 -0
data/README
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
-
|
1
|
+
= WinGdi32Ruby
|
2
2
|
|
3
|
-
|
3
|
+
== v0.1.3
|
4
|
+
* Added OpenBitmap
|
5
|
+
|
6
|
+
== v0.1.1
|
7
|
+
* Fixed bug requiring absolute path to BitmapHelper.dll
|
data/Rakefile
CHANGED
@@ -13,7 +13,7 @@ require 'spec/rake/spectask'
|
|
13
13
|
|
14
14
|
spec = Gem::Specification.new do |s|
|
15
15
|
s.name = 'win-gdi32-ruby'
|
16
|
-
s.version = '0.
|
16
|
+
s.version = '0.1.3'
|
17
17
|
s.has_rdoc = true
|
18
18
|
s.extra_rdoc_files = ['README', 'LICENSE']
|
19
19
|
s.summary = 'Ruby wrapper for the gdi32.dll on Windows.'
|
@@ -21,10 +21,14 @@ spec = Gem::Specification.new do |s|
|
|
21
21
|
s.author = 'Jerry Fernholz'
|
22
22
|
s.email = 'jerry.fernholz@gmail.com'
|
23
23
|
# s.executables = ['your_executable_here']
|
24
|
-
s.files = %w(LICENSE README Rakefile) +
|
24
|
+
s.files = %w(LICENSE README Rakefile) +
|
25
|
+
Dir.glob("{bin,lib,spec}/**/*").reject! { |fn| fn.include?('bmp') || fn.include?('main.rb') }
|
25
26
|
s.require_path = "lib"
|
26
27
|
s.bindir = "bin"
|
27
28
|
s.add_dependency('win32-api', '>= 1.4.2')
|
29
|
+
s.homepage = 'http://rubyforge.org/projects/win-user32-ruby'
|
30
|
+
s.rubyforge_project = 'win-user32-ruby'
|
31
|
+
s.rdoc_options = ['-m', 'README']
|
28
32
|
end
|
29
33
|
|
30
34
|
Rake::GemPackageTask.new(spec) do |p|
|
Binary file
|
data/lib/gdi32/system_const.rb
CHANGED
@@ -4,19 +4,19 @@
|
|
4
4
|
# Constants related to the Win32 API
|
5
5
|
|
6
6
|
module WinGdi32Ruby
|
7
|
-
SRCCOPY = 0x00CC0020
|
8
|
-
SRCPAINT = 0x00EE0086
|
9
|
-
SRCAND = 0x008800C6
|
10
|
-
SRCINVERT = 0x00660046
|
11
|
-
SRCERASE = 0x00440328
|
12
|
-
NOTSRCCOPY = 0x00330008
|
13
|
-
NOTSRCERASE = 0x001100A6
|
14
|
-
MERGECOPY = 0x00C000CA
|
15
|
-
MERGEPAINT = 0x00BB0226
|
16
|
-
PATCOPY = 0x00F00021
|
17
|
-
PATPAINT = 0x00FB0A09
|
18
|
-
PATINVERT = 0x005A0049
|
19
|
-
DSTINVERT = 0x00550009
|
20
|
-
BLACKNESS = 0x00000042
|
21
|
-
WHITENESS = 0x00FF0062
|
7
|
+
SRCCOPY = 0x00CC0020 #:nodoc: dest = source
|
8
|
+
SRCPAINT = 0x00EE0086 #:nodoc: dest = source OR dest
|
9
|
+
SRCAND = 0x008800C6 #:nodoc: dest = source AND dest
|
10
|
+
SRCINVERT = 0x00660046 #:nodoc: dest = source XOR dest
|
11
|
+
SRCERASE = 0x00440328 #:nodoc: dest = source AND (NOT dest )
|
12
|
+
NOTSRCCOPY = 0x00330008 #:nodoc: dest = (NOT source)
|
13
|
+
NOTSRCERASE = 0x001100A6 #:nodoc: dest = (NOT src) AND (NOT dest)
|
14
|
+
MERGECOPY = 0x00C000CA #:nodoc: dest = (source AND pattern)
|
15
|
+
MERGEPAINT = 0x00BB0226 #:nodoc: dest = (NOT source) OR dest
|
16
|
+
PATCOPY = 0x00F00021 #:nodoc: dest = pattern
|
17
|
+
PATPAINT = 0x00FB0A09 #:nodoc: dest = DPSnoo
|
18
|
+
PATINVERT = 0x005A0049 #:nodoc: dest = pattern XOR dest
|
19
|
+
DSTINVERT = 0x00550009 #:nodoc: dest = (NOT dest)
|
20
|
+
BLACKNESS = 0x00000042 #:nodoc: dest = BLACK
|
21
|
+
WHITENESS = 0x00FF0062 #:nodoc: dest = WHITE
|
22
22
|
end
|
data/lib/win-gdi32-ruby.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# Author:: Jerry Fernholz
|
2
2
|
# Date:: 21 May 2009
|
3
3
|
#
|
4
|
+
# Parts of this library where adapted from the CapIt project posted on
|
5
|
+
# codeproject.com by Barretto VN:
|
6
|
+
# see http://www.codeproject.com/KB/GDI/barry_s_screen_capture.aspx for more details
|
7
|
+
|
4
8
|
require 'win32/api'
|
5
9
|
|
6
10
|
require 'gdi32/system_const'
|
7
11
|
|
8
|
-
require 'win-user32-ruby'
|
9
|
-
#require 'gdi32sys'
|
10
12
|
require 'timeout'
|
11
13
|
|
12
14
|
module WinGdi32Ruby
|
@@ -17,122 +19,172 @@ module WinGdi32Ruby
|
|
17
19
|
# +name+:: Name of the Win32 function to be called
|
18
20
|
# +param_type+:: Array of parameter type hints
|
19
21
|
# +return_value+:: Type hint for the return value
|
20
|
-
# Example: <tt>
|
21
|
-
# => calls the <i>FindWindow</i> function
|
22
|
+
# Example: <tt>gdi32 'CreateDC', 'SSSP', 'L'</tt>
|
22
23
|
def gdi32(name, param_type, return_value)
|
23
24
|
API.new name, param_type, return_value, 'gdi32'
|
24
25
|
end
|
25
|
-
|
26
|
+
|
27
|
+
def bitmap_helper(name, param_type, return_value)
|
28
|
+
path = File.expand_path(File.dirname(__FILE__))
|
29
|
+
sb = API.new name, param_type, return_value, path + '/BitmapHelper.dll'
|
30
|
+
end
|
31
|
+
|
26
32
|
class Gdi32
|
27
|
-
|
33
|
+
# Save the specified bitmap to a file
|
34
|
+
# +hwnd+:: Handle to the main window (used to get the system pallete)
|
35
|
+
# +hbitmap+:: Handle to the bitmap to save
|
36
|
+
# +filename+:: Filename for the bitmap
|
37
|
+
# *Returns*:: Void
|
38
|
+
def Gdi32.save_bitmap(hbitmap, filename)
|
39
|
+
# Used to get palette
|
40
|
+
hwnd = hbitmap
|
41
|
+
|
42
|
+
sb = bitmap_helper 'SaveBitmap', 'LLS', 'V'
|
43
|
+
|
44
|
+
sb.call hwnd, hbitmap, filename
|
45
|
+
end
|
46
|
+
|
47
|
+
# Loads a bitmap from a BMP file
|
48
|
+
# +filename+:: name of file to load
|
49
|
+
# *Returns*:: an array containing a handle to the bitmap and a handle to the palette
|
50
|
+
# <tt>[hbitmap, hpalette]</tt>
|
51
|
+
def Gdi32.load_bitmap(filename)
|
52
|
+
sb = bitmap_helper 'OpenBitmap', 'SP', 'I'
|
53
|
+
|
54
|
+
# Prepare array for return values
|
55
|
+
bm_plt = [0, 0].pack('L*')
|
56
|
+
raise "Failed to load '#{filename}'" unless sb.call(filename, bm_plt )
|
57
|
+
hbitmap, hpalette = bm_plt.unpack('L*')
|
58
|
+
|
59
|
+
[hbitmap, hpalette]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Captures the window as a bitmap
|
63
|
+
# +left+, +top+. +right+, +bottom+:: specifies the rectangle to capture
|
64
|
+
# *Returns*:: a bitmap handle
|
65
|
+
def Gdi32.capture(left, top, right, bottom)
|
66
|
+
# create a DC for the screen and a memory DC compatible to screen DC
|
67
|
+
hscr_dc = Gdi32.create_dc
|
68
|
+
hmem_dc = Gdi32.create_compatible_dc(hscr_dc)
|
69
|
+
|
70
|
+
width = right - left
|
71
|
+
height = bottom - top
|
72
|
+
|
73
|
+
# create a bitmap compatible with the screen DC
|
74
|
+
hbitmap = Gdi32.create_compatible_bitmap(hscr_dc, width, height)
|
75
|
+
|
76
|
+
# select new bitmap into memory DC
|
77
|
+
holdbitmap = Gdi32.select_object(hmem_dc, hbitmap)
|
78
|
+
|
79
|
+
# bitblt screen DC to memory DC
|
80
|
+
Gdi32.bit_blt(hscr_dc, left, top, width, height, hmem_dc)
|
81
|
+
|
82
|
+
# select old bitmap back into memory DC and get handle to bitmap of the screen
|
83
|
+
hbitmap = Gdi32.select_object(hmem_dc, holdbitmap);
|
84
|
+
|
85
|
+
# clean up
|
86
|
+
Gdi32.delete_dc(hscr_dc);
|
87
|
+
Gdi32.delete_dc(hmem_dc);
|
88
|
+
|
89
|
+
# return handle to the bitmap
|
90
|
+
hbitmap
|
91
|
+
end
|
92
|
+
protected
|
93
|
+
# The +create_dc+ function creates a device context (DC) for a device using the specified name
|
94
|
+
# +driver+:: Pointer to a null-terminated character string that specifies either 'DISPLAY' or the name of a specific display device or the name of a print provider, which is usually 'WINSPOOL'
|
95
|
+
# +device+:: Pointer to a null-terminated character string that specifies the name of the specific output device being used. The +device+ parameter must be used.
|
96
|
+
# +data+:: Pointer to a _DEVMODE_ structure containing device-specific initialization data for the device driver. The DocumentProperties function retrieves this structure filled in for a specified device. The data parameter must be 0 if the device driver is to use the default initialization (if any) specified by the user. If +driver+ is DISPLAY, then +data+ must be 0. The display device's current DEVMODE is used.
|
97
|
+
# *Returns*:: Non-zero DC handle on success
|
98
|
+
def Gdi32.create_dc(driver = 'DISPLAY', device = '', data = 0)
|
28
99
|
cd = gdi32 'CreateDC', 'SSSP', 'L'
|
29
100
|
|
30
|
-
|
101
|
+
# +output+:: This parameter is ignored and should be set to ''. It is provided only for compatibility with 16-bit Windows
|
102
|
+
output = ''
|
103
|
+
hdc = cd.call driver, device, output, data
|
31
104
|
end
|
32
105
|
|
106
|
+
# The +create_compatible_dc+ function creates a memory device context (DC) compatible with the specified device.
|
107
|
+
# +hdc+:: Handle to existing DC. If this +hdc+ is 0, the function creates a memory DC compatible with the application's current screen
|
108
|
+
# *Returns*:: Non-zero DC handle on success, zero on error
|
33
109
|
def Gdi32.create_compatible_dc(hdc)
|
34
110
|
ccd = gdi32 'CreateCompatibleDC', 'L', 'L'
|
35
111
|
|
36
112
|
hmemdc = ccd.call hdc
|
37
113
|
end
|
38
114
|
|
115
|
+
# Frees the specified DC
|
116
|
+
# +hdc+:: Handle to DC to be freed
|
39
117
|
def Gdi32.delete_dc(hdc)
|
40
118
|
dd = gdi32 'DeleteDC', 'L', 'I'
|
41
119
|
|
42
120
|
dd.call(hdc) != 0
|
43
121
|
end
|
44
122
|
|
123
|
+
# The +create_compatible_bitmap+ function creates a bitmap compatible with the device that is associated with the specified device context.
|
124
|
+
# +src_dc+:: Handle to device context
|
125
|
+
# +width+:: Bitmap width in pixels
|
126
|
+
# +height+:: Bitmap height in pixels
|
127
|
+
# *Returns*:: Handle to bitmap or zero on error
|
45
128
|
def Gdi32.create_compatible_bitmap(src_dc, width, height)
|
46
129
|
ccb = gdi32 'CreateCompatibleBitmap', 'LII', 'L'
|
47
130
|
|
48
131
|
hbitmap = ccb.call src_dc, width, height
|
49
132
|
end
|
50
133
|
|
51
|
-
|
134
|
+
# The +select_object+ function selects an object into the specified device context (+mem_dc+). The new object replaces the previous object of the same type.
|
135
|
+
# +mem_dc+:: Handle to the device context into which the object will be selected
|
136
|
+
# +obj_dc+:: Handle to object to be selected
|
137
|
+
# *Returns*:: If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced. If the selected object is a region and the function succeeds, the return value is one of the following values: SIMPLEREGION, COMPLEXREGION, NULLREGION. Returns zero or HGDI_ERROR on error.
|
138
|
+
def Gdi32.select_object(mem_dc, obj_dc)
|
52
139
|
so = gdi32 'SelectObject', 'LL', 'L'
|
53
140
|
|
54
|
-
hobj = so.call mem_dc,
|
141
|
+
hobj = so.call mem_dc, obj_dc
|
55
142
|
end
|
56
143
|
|
144
|
+
# The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.
|
145
|
+
# +src_dc+:: Handle to the source DC
|
146
|
+
# +src_x+:: X-coordinate to the upper-left corner of the source rectangle
|
147
|
+
# +src_y+:: Y-coordinate to the upper-left corner of the source rectangle
|
148
|
+
# +width+:: Width of the source and destination rectangle
|
149
|
+
# +height+:: Height of the source and destination rectangle
|
150
|
+
# +dest_dc+:: Destination device context
|
151
|
+
# +dest_x+:: X-coordinate to the upper-left corner of the destination rectangle
|
152
|
+
# +dest_y+:: Y-coordinate to the upper-left corner of the destination rectangle
|
153
|
+
# +opcode+:: Specifies a raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color
|
154
|
+
# *Returns*:: Non-zero on success, zero otherwise
|
57
155
|
def Gdi32.bit_blt(src_dc, src_x, src_y, width, height, dest_dc, dest_x = 0, dest_y = 0, opcode = SRCCOPY)
|
58
156
|
bb = gdi32 'BitBlt', 'LIIIILIIL', 'I'
|
59
157
|
|
60
158
|
bb.call(dest_dc, dest_x, dest_y, width, height, src_dc, src_x, src_y, opcode) != 0
|
61
159
|
end
|
160
|
+
end
|
62
161
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
162
|
+
def bytewise_file_compare(fn1, fn2)
|
163
|
+
# Open both files as binary
|
164
|
+
begin
|
165
|
+
f1, f2 = File.open(fn1, 'rb'), File.open(fn2, 'rb')
|
166
|
+
rescue => e
|
167
|
+
puts e
|
168
|
+
return false
|
68
169
|
end
|
69
|
-
|
70
|
-
#
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
nY = top
|
86
|
-
nX2 = right
|
87
|
-
nY2 = bottom
|
88
|
-
|
89
|
-
# xScrn = GetDeviceCaps(hScrDC, HORZRES);
|
90
|
-
# yScrn = GetDeviceCaps(hScrDC, VERTRES);
|
91
|
-
#
|
92
|
-
# //make sure bitmap rectangle is visible
|
93
|
-
#
|
94
|
-
# if (nX < 0)
|
95
|
-
# nX = 0;
|
96
|
-
#
|
97
|
-
# if (nY < 0)
|
98
|
-
# nY = 0;
|
99
|
-
#
|
100
|
-
# if (nX2 > xScrn)
|
101
|
-
# nX2 = xScrn;
|
102
|
-
#
|
103
|
-
# if (nY2 > yScrn)
|
104
|
-
# nY2 = yScrn;
|
105
|
-
|
106
|
-
puts nWidth = nX2 - nX;
|
107
|
-
puts nHeight = nY2 - nY;
|
108
|
-
|
109
|
-
#
|
110
|
-
# // create a bitmap compatible with the screen DC
|
111
|
-
#
|
112
|
-
hBitmap = Gdi32.create_compatible_bitmap(hScrDC, nWidth, nHeight)
|
113
|
-
#
|
114
|
-
# // select new bitmap into memory DC
|
115
|
-
#
|
116
|
-
hOldBitmap = Gdi32.select_object(hMemDC, hBitmap)
|
117
|
-
#
|
118
|
-
# // bitblt screen DC to memory DC
|
119
|
-
#
|
120
|
-
Gdi32.bit_blt(hScrDC, nX, nY, nWidth, nHeight, hMemDC)
|
121
|
-
#
|
122
|
-
# // select old bitmap back into memory DC and get handle to
|
123
|
-
# // bitmap of the screen
|
124
|
-
#
|
125
|
-
hBitmap = Gdi32.select_object(hMemDC, hOldBitmap);
|
126
|
-
#
|
127
|
-
# // clean up
|
128
|
-
#
|
129
|
-
Gdi32.delete_dc(hScrDC);
|
130
|
-
Gdi32.delete_dc(hMemDC);
|
131
|
-
#
|
132
|
-
# // return handle to the bitmap
|
133
|
-
#
|
134
|
-
hBitmap
|
170
|
+
|
171
|
+
# Assume they don't match
|
172
|
+
same = false
|
173
|
+
i, fs1, fs2 = 0, File.stat(fn1), File.stat(fn2)
|
174
|
+
if fs1.size == fs2.size
|
175
|
+
same = true
|
176
|
+
|
177
|
+
bytes1 = []
|
178
|
+
f1.each_byte{|b| bytes1 << b}
|
179
|
+
|
180
|
+
f2.each_byte do |b|
|
181
|
+
if bytes1[i] != b
|
182
|
+
same = false
|
183
|
+
end
|
184
|
+
i += 1
|
185
|
+
end
|
135
186
|
end
|
136
|
-
end
|
137
187
|
|
188
|
+
same
|
189
|
+
end
|
138
190
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: win-gdi32-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jerry Fernholz
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-22 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -35,18 +35,16 @@ files:
|
|
35
35
|
- LICENSE
|
36
36
|
- README
|
37
37
|
- Rakefile
|
38
|
+
- lib/BitmapHelper.dll
|
38
39
|
- lib/gdi32
|
39
40
|
- lib/gdi32/system_const.rb
|
40
|
-
- lib/main.rb
|
41
|
-
- lib/ScreenCapture.dll
|
42
|
-
- lib/test.bmp
|
43
|
-
- lib/test2.bmp
|
44
41
|
- lib/win-gdi32-ruby.rb
|
45
42
|
has_rdoc: true
|
46
|
-
homepage:
|
43
|
+
homepage: http://rubyforge.org/projects/win-user32-ruby
|
47
44
|
post_install_message:
|
48
|
-
rdoc_options:
|
49
|
-
|
45
|
+
rdoc_options:
|
46
|
+
- -m
|
47
|
+
- README
|
50
48
|
require_paths:
|
51
49
|
- lib
|
52
50
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -63,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
61
|
version:
|
64
62
|
requirements: []
|
65
63
|
|
66
|
-
rubyforge_project:
|
64
|
+
rubyforge_project: win-user32-ruby
|
67
65
|
rubygems_version: 1.3.1
|
68
66
|
signing_key:
|
69
67
|
specification_version: 2
|
data/lib/ScreenCapture.dll
DELETED
Binary file
|
data/lib/main.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'win-gdi32-ruby'
|
2
|
-
include WinGdi32Ruby
|
3
|
-
|
4
|
-
require 'win-user32-ruby'
|
5
|
-
include WinUser32Ruby
|
6
|
-
|
7
|
-
|
8
|
-
wnd = Window.find_window('win-gdi32-ruby - NetBeans IDE 6.7 RC1')
|
9
|
-
puts wnd.handle
|
10
|
-
|
11
|
-
rect = wnd.get_window_rect
|
12
|
-
hbitmap = Gdi32.capture(0,0,100,100)#*rect)
|
13
|
-
Gdi32.save_bitmap(wnd.handle, hbitmap, 'test2.bmp')
|
14
|
-
|
15
|
-
f1 = File.open('test.bmp', 'rb')
|
16
|
-
f2 = File.open('test2.bmp', 'rb')
|
17
|
-
|
18
|
-
i, l2 = 0, f2.readlines
|
19
|
-
f1.readline.each do |l|
|
20
|
-
if l2[i] == l
|
21
|
-
puts 'Same'
|
22
|
-
else
|
23
|
-
puts 'Diff'
|
24
|
-
end
|
25
|
-
i += 1
|
26
|
-
end
|
data/lib/test.bmp
DELETED
Binary file
|
data/lib/test2.bmp
DELETED
Binary file
|