win 0.0.2 → 0.0.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.rdoc +16 -14
- data/VERSION +1 -1
- data/lib/win/library.rb +256 -45
- data/lib/win/window.rb +547 -0
- data/spec/spec_helper.rb +20 -1
- data/spec/test_apps/locknote/LockNote.exe +0 -0
- data/spec/win/library_spec.rb +1 -0
- data/spec/win/window_spec.rb +309 -0
- data/win.gemspec +68 -0
- metadata +7 -2
data/lib/win/window.rb
ADDED
@@ -0,0 +1,547 @@
|
|
1
|
+
require 'win/library'
|
2
|
+
|
3
|
+
module Win
|
4
|
+
module Window
|
5
|
+
include Win::Library
|
6
|
+
|
7
|
+
#General constants:
|
8
|
+
|
9
|
+
# Windows Message Get Text
|
10
|
+
WM_GETTEXT = 0x000D
|
11
|
+
# Windows Message Sys Command
|
12
|
+
WM_SYSCOMMAND = 0x0112
|
13
|
+
# Sys Command Close
|
14
|
+
SC_CLOSE = 0xF060
|
15
|
+
|
16
|
+
# ShowWindow constants:
|
17
|
+
|
18
|
+
# Hides the window and activates another window.
|
19
|
+
SW_HIDE = 0
|
20
|
+
# Same as SW_SHOWNORMAL
|
21
|
+
SW_NORMAL = 1
|
22
|
+
# Activates and displays a window. If the window is minimized or maximized, the system restores it to its
|
23
|
+
# original size and position. An application should specify this flag when displaying the window for the first time.
|
24
|
+
SW_SHOWNORMAL = 1
|
25
|
+
# Activates the window and displays it as a minimized window.
|
26
|
+
SW_SHOWMINIMIZED = 2
|
27
|
+
# Activates the window and displays it as a maximized window.
|
28
|
+
SW_SHOWMAXIMIZED = 3
|
29
|
+
# Activates the window and displays it as a maximized window.
|
30
|
+
SW_MAXIMIZE = 3
|
31
|
+
# Displays a window in its most recent size and position. Similar to SW_SHOWNORMAL, but the window is not activated.
|
32
|
+
SW_SHOWNOACTIVATE = 4
|
33
|
+
# Activates the window and displays it in its current size and position.
|
34
|
+
SW_SHOW = 5
|
35
|
+
# Minimizes the specified window, activates the next top-level window in the Z order.
|
36
|
+
SW_MINIMIZE = 6
|
37
|
+
# Displays the window as a minimized window. Similar to SW_SHOWMINIMIZED, except the window is not activated.
|
38
|
+
SW_SHOWMINNOACTIVE= 7
|
39
|
+
# Displays the window in its current size and position. Similar to SW_SHOW, except the window is not activated.
|
40
|
+
SW_SHOWNA = 8
|
41
|
+
# Activates and displays the window. If the window is minimized or maximized, the system restores it to its original
|
42
|
+
# size and position. An application should specify this flag when restoring a minimized window.
|
43
|
+
SW_RESTORE = 9
|
44
|
+
# Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess
|
45
|
+
# function by the program that started the application.
|
46
|
+
SW_SHOWDEFAULT = 10
|
47
|
+
# Windows 2000/XP: Minimizes a window, even if the thread that owns the window is not responding. Only use this
|
48
|
+
# flag when minimizing windows from a different thread.
|
49
|
+
SW_FORCEMINIMIZE = 11
|
50
|
+
|
51
|
+
|
52
|
+
# Windows GUI API definitions:
|
53
|
+
|
54
|
+
##
|
55
|
+
# Tests whether the specified window handle identifies an existing window.
|
56
|
+
# A thread should not use IsWindow for a window that it did not create because the window
|
57
|
+
# could be destroyed after this function was called. Further, because window handles are
|
58
|
+
# recycled the handle could even point to a different window.
|
59
|
+
#
|
60
|
+
# :call-seq:
|
61
|
+
# window?( win_handle )
|
62
|
+
#
|
63
|
+
function 'IsWindow', 'L', 'L'
|
64
|
+
|
65
|
+
##
|
66
|
+
# Tests if the specified window, its parent window, its parent's parent window, and so forth,
|
67
|
+
# have the WS_VISIBLE style. Because the return value specifies whether the window has the
|
68
|
+
# WS_VISIBLE style, it may be true even if the window is totally obscured by other windows.
|
69
|
+
#
|
70
|
+
# :call-seq:
|
71
|
+
# visible?( win_handle ), window_visible?( win_handle )
|
72
|
+
#
|
73
|
+
function 'IsWindowVisible', 'L', 'L', aliases: :visible?
|
74
|
+
|
75
|
+
##
|
76
|
+
# Tests whether the specified window is maximized.
|
77
|
+
#
|
78
|
+
# :call-seq:
|
79
|
+
# zoomed?( win_handle ), maximized?( win_handle )
|
80
|
+
#
|
81
|
+
function 'IsZoomed', 'L', 'L', aliases: :maximized?
|
82
|
+
|
83
|
+
##
|
84
|
+
# Tests whether the specified window is maximized.
|
85
|
+
#
|
86
|
+
# :call-seq:
|
87
|
+
# iconic?( win_handle ), minimized?( win_handle )
|
88
|
+
#
|
89
|
+
function 'IsIconic', 'L', 'L', aliases: :minimized?
|
90
|
+
|
91
|
+
##
|
92
|
+
# Tests whether a window is a child (or descendant) window of a specified parent window.
|
93
|
+
# A child window is the direct descendant of a specified parent window if that parent window
|
94
|
+
# is in the chain of parent windows; the chain of parent windows leads from the original overlapped
|
95
|
+
# or pop-up window to the child window.
|
96
|
+
#
|
97
|
+
# :call-seq:
|
98
|
+
# child?( win_handle )
|
99
|
+
#
|
100
|
+
function 'IsChild', 'LL', 'L'
|
101
|
+
|
102
|
+
##
|
103
|
+
# Returns a handle to the top-level window whose class and window name match the specified strings.
|
104
|
+
# This function does not search child windows. This function does not perform a case-sensitive search.
|
105
|
+
#
|
106
|
+
# Parameters:
|
107
|
+
# class_name (P) - String that specifies (window) class name OR class atom created by a previous
|
108
|
+
# call to the RegisterClass(Ex) function. The atom must be in the low-order word of class_name;
|
109
|
+
# the high-order word must be zero. The class name can be any name registered with RegisterClass(Ex),
|
110
|
+
# or any of the predefined control-class names. If this parameter is nil, it finds any window whose
|
111
|
+
# title matches the win_title parameter.
|
112
|
+
# win_name (P) - String that specifies the window name (title). If nil, all names match.
|
113
|
+
# Return Value (L): found window handle or NIL if nothing found
|
114
|
+
#
|
115
|
+
# :call-seq:
|
116
|
+
# win_handle = find_window( class_name, win_name )
|
117
|
+
#
|
118
|
+
function 'FindWindow', 'PP', 'L', zeronil: true
|
119
|
+
|
120
|
+
##
|
121
|
+
# Unicode version of find_window (strings must be encoded as utf-16LE AND terminate with "\x00\x00")
|
122
|
+
#
|
123
|
+
# :call-seq:
|
124
|
+
# win_handle = find_window_w( class_name, win_name )
|
125
|
+
#
|
126
|
+
function 'FindWindowW', 'PP', 'L', zeronil: true
|
127
|
+
|
128
|
+
##
|
129
|
+
# Retrieves a handle to a CHILD window whose class name and window name match the specified strings.
|
130
|
+
# The function searches child windows, beginning with the one following the specified child window.
|
131
|
+
# This function does NOT perform a case-sensitive search.
|
132
|
+
#
|
133
|
+
# Parameters:
|
134
|
+
# parent (L) - Handle to the parent window whose child windows are to be searched.
|
135
|
+
# If nil, the function uses the desktop window as the parent window.
|
136
|
+
# The function searches among windows that are child windows of the desktop.
|
137
|
+
# after_child (L) - Handle to a child window. Search begins with the NEXT child window in the Z order.
|
138
|
+
# The child window must be a direct child window of parent, not just a descendant window.
|
139
|
+
# If after_child is nil, the search begins with the first child window of parent.
|
140
|
+
# win_class (P), win_title (P) - Strings that specify window class and name(title). If nil, anything matches.
|
141
|
+
# Returns (L): found child window handle or NIL if nothing found
|
142
|
+
#
|
143
|
+
# :call-seq:
|
144
|
+
# win_handle = find_window_ex( win_handle, after_child, class_name, win_name )
|
145
|
+
#
|
146
|
+
function 'FindWindowEx', 'LLPP', 'L', zeronil: true
|
147
|
+
|
148
|
+
# Helper method that creates def_block returning (possibly encoded) string as a result of
|
149
|
+
# api function call or nil if zero characters was returned by api call
|
150
|
+
# TODO: should be private
|
151
|
+
def self.return_string( encode = nil ) #:nodoc:
|
152
|
+
lambda do |api, *args|
|
153
|
+
namespace.enforce_count( args, api.prototype, -2)
|
154
|
+
args += [string = buffer, string.length]
|
155
|
+
num_chars = api.call(*args)
|
156
|
+
return nil if num_chars == 0
|
157
|
+
string = string.force_encoding('utf-16LE').encode(encode) if encode
|
158
|
+
p string
|
159
|
+
string.rstrip
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
##
|
164
|
+
# Returns the text of the specified window's title bar (if it has one).
|
165
|
+
# If the specified window is a control, the text of the control is copied. However, GetWindowText
|
166
|
+
# cannot retrieve the text of a control in another application.
|
167
|
+
#
|
168
|
+
# Original Parameters:
|
169
|
+
# win_handle (L) - Handle to the window and, indirectly, the class to which the window belongs.
|
170
|
+
# text (P) - Long Pointer to the buffer that is to receive the text string.
|
171
|
+
# max_count (I) - Specifies the length, in TCHAR, of the buffer pointed to by the text parameter.
|
172
|
+
# The class name string is truncated if it is longer than the buffer and is always null-terminated.
|
173
|
+
# Original Return Value (L): Length, in characters, of the copied string, not including the terminating null
|
174
|
+
# character, indicates success. Zero indicates that the window has no title bar or text, if the title bar
|
175
|
+
# is empty, or if the window or control handle is invalid. For extended error information, call GetLastError.
|
176
|
+
#
|
177
|
+
# Enhanced API requires only win_handle and returns rstripped text
|
178
|
+
#
|
179
|
+
# Enhanced Parameters:
|
180
|
+
# win_handle (L) - Handle to the window and, indirectly, the class to which the window belongs.
|
181
|
+
# Returns: Window title bar text or nil
|
182
|
+
# If the window has no title bar or text, if the title bar is empty, or if the window or control handle
|
183
|
+
# is invalid, the return value is NIL. To get extended error information, call GetLastError.
|
184
|
+
#
|
185
|
+
# Remarks: This function CANNOT retrieve the text of an edit control in ANOTHER app.
|
186
|
+
# If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to
|
187
|
+
# be sent to the specified window or control. If the target window is owned by another process and has
|
188
|
+
# a caption, GetWindowText retrieves the window caption text. If the window does not have a caption,
|
189
|
+
# the return value is a null string. This allows to call GetWindowText without becoming unresponsive
|
190
|
+
# if the target window owner process is not responding. However, if the unresponsive target window
|
191
|
+
# belongs to the calling app, GetWindowText will cause the calling app to become unresponsive.
|
192
|
+
# To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead
|
193
|
+
# of calling GetWindowText.
|
194
|
+
#
|
195
|
+
#:call-seq:
|
196
|
+
# text = get_window_text( win_handle )
|
197
|
+
#
|
198
|
+
function 'GetWindowText', 'LPI', 'L', &return_string
|
199
|
+
|
200
|
+
##
|
201
|
+
# Unicode version of get_window_text (returns rstripped utf-8 string)
|
202
|
+
# API improved to require only win_handle and return rstripped string
|
203
|
+
#
|
204
|
+
#:call-seq:
|
205
|
+
# text = get_window_text_w( win_handle )
|
206
|
+
#
|
207
|
+
function 'GetWindowTextW', 'LPI', 'L', &return_string('utf-8')
|
208
|
+
|
209
|
+
##
|
210
|
+
# Retrieves the name of the class to which the specified window belongs.
|
211
|
+
#
|
212
|
+
# Original Parameters:
|
213
|
+
# win_handle (L) - Handle to the window and, indirectly, the class to which the window belongs.
|
214
|
+
# class_name (P) - Long Pointer to the buffer that is to receive the class name string.
|
215
|
+
# max_count (I) - Specifies the length, in TCHAR, of the buffer pointed to by the class_name parameter.
|
216
|
+
# The class name string is truncated if it is longer than the buffer and is always null-terminated.
|
217
|
+
# Original Return Value (L): Length, in characters, of the copied string, not including the terminating null
|
218
|
+
# character, indicates success. Zero indicates that the window has no title bar or text, if the title bar
|
219
|
+
# is empty, or if the window or control handle is invalid. For extended error information, call GetLastError.
|
220
|
+
#
|
221
|
+
# API improved to require only win_handle and return rstripped string
|
222
|
+
#
|
223
|
+
# Enhanced Parameters:
|
224
|
+
# win_handle (L) - Handle to the window and, indirectly, the class to which the window belongs.
|
225
|
+
# Returns: Name of the class or NIL if function fails. For extended error information, call GetLastError.
|
226
|
+
#
|
227
|
+
#:call-seq:
|
228
|
+
# text = get_class_name( win_handle )
|
229
|
+
#
|
230
|
+
function 'GetClassName', 'LPI', 'I', &return_string
|
231
|
+
|
232
|
+
##
|
233
|
+
# Unicode version of get_class_name (returns rstripped utf-8 string)
|
234
|
+
# API improved to require only win_handle and return rstripped string
|
235
|
+
#
|
236
|
+
#:call-seq:
|
237
|
+
# text = get_class_name_w( win_handle )
|
238
|
+
#
|
239
|
+
function 'GetClassNameW', 'LPI', 'I', &return_string('utf-8')
|
240
|
+
|
241
|
+
##
|
242
|
+
# Shows and hides windows.
|
243
|
+
#
|
244
|
+
# Parameters:
|
245
|
+
# win_handle (L) - Handle to the window.
|
246
|
+
# cmd (I) - Specifies how the window is to be shown. This parameter is ignored the first time an
|
247
|
+
# application calls ShowWindow, if the program that launched the application provides a STARTUPINFO
|
248
|
+
# structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained
|
249
|
+
# by the WinMain function in its nCmdShow parameter. In subsequent calls, cmd may be:
|
250
|
+
# SW_HIDE, SW_MAXIMIZE, SW_MINIMIZE, SW_SHOW, SW_SHOWMAXIMIZED, SW_SHOWMINIMIZED, SW_SHOWMINNOACTIVE,
|
251
|
+
# SW_SHOWNA, SW_SHOWNOACTIVATE, SW_SHOWNORMAL, SW_RESTORE, SW_SHOWDEFAULT, SW_FORCEMINIMIZE
|
252
|
+
#
|
253
|
+
# Original Return Value: - Nonzero if the window was PREVIOUSLY visible, otherwise zero
|
254
|
+
# Enhanced Returns: - True if the window was PREVIOUSLY visible, otherwise false
|
255
|
+
#
|
256
|
+
#:call-seq:
|
257
|
+
# was_visible = show_window( win_handle, cmd )
|
258
|
+
#
|
259
|
+
function 'ShowWindow', 'LI', 'I', boolean: true
|
260
|
+
|
261
|
+
# Hides the window and activates another window
|
262
|
+
def hide_window(win_handle)
|
263
|
+
show_window(win_handle, SW_HIDE)
|
264
|
+
end
|
265
|
+
|
266
|
+
return_thread_process = lambda do |api, *args|
|
267
|
+
namespace.enforce_count( args, api.prototype, -1)
|
268
|
+
thread = api.call(args.first, process = [1].pack('L'))
|
269
|
+
nonzero_array(thread, *process.unpack('L'))
|
270
|
+
end
|
271
|
+
|
272
|
+
##
|
273
|
+
# Retrieves the identifier of the thread that created the specified window
|
274
|
+
# and, optionally, the identifier of the process that created the window.
|
275
|
+
#
|
276
|
+
# Original Parameters:
|
277
|
+
# handle (L) - Handle to the window.
|
278
|
+
# process (P) - A POINTER to a (Long) variable that receives the process identifier.
|
279
|
+
# Original Return (L): Identifier of the thread that created the window.
|
280
|
+
#
|
281
|
+
# API improved to accept window handle as a single arg and return a pair of [thread, process] ids
|
282
|
+
#
|
283
|
+
# New Parameters:
|
284
|
+
# handle (L) - Handle to the window.
|
285
|
+
# Returns: Pair of identifiers of the thread and process_id that created the window.
|
286
|
+
#
|
287
|
+
#:call-seq:
|
288
|
+
# thread, process_id = get_window_tread_process_id( win_handle )
|
289
|
+
#
|
290
|
+
function 'GetWindowThreadProcessId', 'LP', 'L', &return_thread_process
|
291
|
+
|
292
|
+
return_rect = lambda do |api, *args|
|
293
|
+
namespace.enforce_count( args, api.prototype, -1)
|
294
|
+
rectangle = [0, 0, 0, 0].pack('L*')
|
295
|
+
res = api.call args.first, rectangle
|
296
|
+
res == 0 ? [nil, nil, nil, nil] : rectangle.unpack('L*')
|
297
|
+
end
|
298
|
+
|
299
|
+
##
|
300
|
+
# Retrieves the dimensions of the specified window bounding rectangle.
|
301
|
+
# Dimensions are given relative to the upper-left corner of the screen.
|
302
|
+
#
|
303
|
+
# Original Parameters:
|
304
|
+
# win_handle (L) - Handle to the window.
|
305
|
+
# rect (P) - Long pointer to a RECT structure that receives the screen coordinates of the upper-left and
|
306
|
+
# lower-right corners of the window.
|
307
|
+
# Original Return Value: Nonzero indicates success. Zero indicates failure. For error info, call GetLastError.
|
308
|
+
#
|
309
|
+
# API improved to accept only window handle and return 4-member dimensions array (left, top, right, bottom)
|
310
|
+
#
|
311
|
+
# New Parameters:
|
312
|
+
# win_handle (L) - Handle to the window
|
313
|
+
# New Return: Array(left, top, right, bottom) - rectangle dimensions
|
314
|
+
#
|
315
|
+
# Remarks: As a convention for the RECT structure, the bottom-right coordinates of the returned rectangle
|
316
|
+
# are exclusive. In other words, the pixel at (right, bottom) lies immediately outside the rectangle.
|
317
|
+
#
|
318
|
+
#:call-seq:
|
319
|
+
# rect = get_window_rect( win_handle )
|
320
|
+
#
|
321
|
+
function 'GetWindowRect', 'LP', 'I', &return_rect
|
322
|
+
|
323
|
+
# # Procedure that calls api function expecting a callback. If runtime block is given
|
324
|
+
# # it is converted into callback, otherwise procedure returns an array of all handles
|
325
|
+
# # pushed into callback by api enumeration
|
326
|
+
# # TODO: should be private
|
327
|
+
# #
|
328
|
+
# def self.return_enum #:nodoc:
|
329
|
+
# lambda do |api, *args, &block|
|
330
|
+
# namespace.enforce_count( args, api.prototype, -1)
|
331
|
+
# handles = []
|
332
|
+
# cb = if block
|
333
|
+
# callback('LP', 'I', &block)
|
334
|
+
# else
|
335
|
+
# callback('LP', 'I') do |handle, message|
|
336
|
+
# handles << handle
|
337
|
+
# true
|
338
|
+
# end
|
339
|
+
# end
|
340
|
+
# args[api.prototype.find_index('K'), 0] = cb # Insert callback into appropriate place of args Array
|
341
|
+
# api.call *args
|
342
|
+
# handles
|
343
|
+
# end
|
344
|
+
# end
|
345
|
+
#
|
346
|
+
# ##
|
347
|
+
# # The EnumWindows function enumerates all top-level windows on the screen by passing the handle to
|
348
|
+
# # each window, in turn, to an application-defined callback function. EnumWindows continues until
|
349
|
+
# # the last top-level window is enumerated or the callback function returns FALSE.
|
350
|
+
# #
|
351
|
+
# # Original Parameters:
|
352
|
+
# # callback [K] - Pointer to an application-defined callback function (see EnumWindowsProc).
|
353
|
+
# # message [P] - Specifies an application-defined value(message) to be passed to the callback function.
|
354
|
+
# # Original Return: Nonzero if the function succeeds, zero if the function fails. GetLastError for error info.
|
355
|
+
# # If callback returns zero, the return value is also zero. In this case, the callback function should
|
356
|
+
# # call SetLastError to obtain a meaningful error code to be returned to the caller of EnumWindows.
|
357
|
+
# #
|
358
|
+
# # API improved to accept blocks (instead of callback objects) and message as a single arg
|
359
|
+
# #
|
360
|
+
# # New Parameters:
|
361
|
+
# # message [P] - Specifies an application-defined value(message) to be passed to the callback function.
|
362
|
+
# # block given to method invocation serves as an application-defined callback function (see EnumWindowsProc).
|
363
|
+
# # Returns: True if the function succeeds, false if the function fails. GetLastError for error info.
|
364
|
+
# # If callback returns zero, the return value is also zero. In this case, the callback function should
|
365
|
+
# # call SetLastError to obtain a meaningful error code to be returned to the caller of EnumWindows.
|
366
|
+
# #
|
367
|
+
# # Remarks: The EnumWindows function does not enumerate child windows, with the exception of a few top-level
|
368
|
+
# # windows owned by the system that have the WS_CHILD style. This function is more reliable than calling
|
369
|
+
# # the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being
|
370
|
+
# # caught in an infinite loop or referencing a handle to a window that has been destroyed.
|
371
|
+
# #
|
372
|
+
# #:call-seq:
|
373
|
+
# # status = enum_windows( message ) {|win_handle, message| callback procedure }
|
374
|
+
# #
|
375
|
+
# function'EnumWindows', 'KP', 'L', boolean: true, &return_enum
|
376
|
+
#
|
377
|
+
# ##
|
378
|
+
# # Enumerates child windows to a given window.
|
379
|
+
# #
|
380
|
+
# # Original Parameters:
|
381
|
+
# # parent (L) - Handle to the parent window whose child windows are to be enumerated.
|
382
|
+
# # callback [K] - Pointer to an application-defined callback function (see EnumWindowsProc).
|
383
|
+
# # message [P] - Specifies an application-defined value(message) to be passed to the callback function.
|
384
|
+
# # Original Return: Not used (?!)
|
385
|
+
# # If callback returns zero, the return value is also zero. In this case, the callback function should
|
386
|
+
# # call SetLastError to obtain a meaningful error code to be returned to the caller of EnumWindows.
|
387
|
+
# # If it is nil, this function is equivalent to EnumWindows. Windows 95/98/Me: parent cannot be NULL.
|
388
|
+
# #
|
389
|
+
# # API improved to accept blocks (instead of callback objects) and two args: parent handle and message.
|
390
|
+
# # New Parameters:
|
391
|
+
# # parent (L) - Handle to the parent window whose child windows are to be enumerated.
|
392
|
+
# # message (P) - Specifies an application-defined value(message) to be passed to the callback function.
|
393
|
+
# # block given to method invocation serves as an application-defined callback function (see EnumChildProc).
|
394
|
+
# #
|
395
|
+
# # Remarks:
|
396
|
+
# # If a child window has created child windows of its own, EnumChildWindows enumerates those windows as well.
|
397
|
+
# # A child window that is moved or repositioned in the Z order during the enumeration process will be properly enumerated.
|
398
|
+
# # The function does not enumerate a child window that is destroyed before being enumerated or that is created during the enumeration process.
|
399
|
+
# #
|
400
|
+
# #:call-seq:
|
401
|
+
# # enum_windows( parent_handle, message ) {|win_handle, message| callback procedure }
|
402
|
+
# #
|
403
|
+
# function 'EnumChildWindows', 'LKP', 'L', &return_enum
|
404
|
+
|
405
|
+
##
|
406
|
+
# GetForegroundWindow function returns a handle to the foreground window (the window with which the user
|
407
|
+
# is currently working). The system assigns a slightly higher priority to the thread that creates the
|
408
|
+
# foreground window than it does to other threads.
|
409
|
+
#
|
410
|
+
# Syntax: HWND GetForegroundWindow(VOID);
|
411
|
+
#
|
412
|
+
# Return Value: The return value is a handle to the foreground window. The foreground window can be NULL in
|
413
|
+
# certain circumstances, such as when a window is losing activation.
|
414
|
+
#
|
415
|
+
#:call-seq:
|
416
|
+
# win_handle = (get_)foreground_window()
|
417
|
+
#
|
418
|
+
function 'GetForegroundWindow', [], 'L'
|
419
|
+
|
420
|
+
def foreground?(win_handle)
|
421
|
+
win_handle == foreground_window
|
422
|
+
end
|
423
|
+
|
424
|
+
##
|
425
|
+
# The GetActiveWindow function retrieves the window handle to the active window attached to
|
426
|
+
# the calling thread's message queue.
|
427
|
+
#
|
428
|
+
# Syntax: HWND GetActiveWindow(VOID);
|
429
|
+
#
|
430
|
+
# Return Value: The return value is the handle to the active window attached to the calling
|
431
|
+
# thread's message queue. Otherwise, the return value is NULL.
|
432
|
+
#
|
433
|
+
# Remarks: To get the handle to the foreground window, you can use GetForegroundWindow.
|
434
|
+
# To get the window handle to the active window in the message queue for another thread, use GetGUIThreadInfo.
|
435
|
+
#
|
436
|
+
#:call-seq:
|
437
|
+
# win_handle = (get_)active_window()
|
438
|
+
#
|
439
|
+
function 'GetActiveWindow', [], 'L'
|
440
|
+
|
441
|
+
function 'keybd_event', 'IILL', 'V'
|
442
|
+
function 'PostMessage', 'LLLL', 'L'
|
443
|
+
function 'SendMessage', 'LLLP', 'L'
|
444
|
+
function 'GetDlgItem', 'LL', 'L'
|
445
|
+
|
446
|
+
|
447
|
+
# Convenience wrapper methods:
|
448
|
+
|
449
|
+
# emulates combinations of keys pressed (Ctrl+Alt+P+M, etc)
|
450
|
+
def keystroke(*keys)
|
451
|
+
return if keys.empty?
|
452
|
+
keybd_event keys.first, 0, KEYEVENTF_KEYDOWN, 0
|
453
|
+
sleep WG_KEY_DELAY
|
454
|
+
keystroke *keys[1..-1]
|
455
|
+
sleep WG_KEY_DELAY
|
456
|
+
keybd_event keys.first, 0, KEYEVENTF_KEYUP, 0
|
457
|
+
end
|
458
|
+
|
459
|
+
# types text message into window holding the focus
|
460
|
+
def type_in(message)
|
461
|
+
message.scan(/./m) do |char|
|
462
|
+
keystroke(*char.to_vkeys)
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
# finds top-level dialog window by title and yields it to given block
|
467
|
+
def dialog(title, seconds=3)
|
468
|
+
d = begin
|
469
|
+
win = Window.top_level(title, seconds)
|
470
|
+
yield(win) ? win : nil
|
471
|
+
rescue TimeoutError
|
472
|
+
end
|
473
|
+
d.wait_for_close if d
|
474
|
+
return d
|
475
|
+
end
|
476
|
+
|
477
|
+
class Window
|
478
|
+
include Win::Window
|
479
|
+
extend Win::Window
|
480
|
+
|
481
|
+
attr_reader :handle
|
482
|
+
|
483
|
+
# find top level window by title, return wrapped Window object
|
484
|
+
def self.top_level(title, seconds=3)
|
485
|
+
@handle = timeout(seconds) do
|
486
|
+
sleep WG_SLEEP_DELAY while (h = find_window nil, title) == nil; h
|
487
|
+
end
|
488
|
+
Window.new @handle
|
489
|
+
end
|
490
|
+
|
491
|
+
def initialize(handle)
|
492
|
+
@handle = handle
|
493
|
+
end
|
494
|
+
|
495
|
+
# find child window (control) by title, window class, or control ID:
|
496
|
+
def child(id)
|
497
|
+
result = case id
|
498
|
+
when String
|
499
|
+
by_title = find_window_ex @handle, 0, nil, id.gsub('_' , '&' )
|
500
|
+
by_class = find_window_ex @handle, 0, id, nil
|
501
|
+
by_title ? by_title : by_class
|
502
|
+
when Fixnum
|
503
|
+
get_dlg_item @handle, id
|
504
|
+
when nil
|
505
|
+
find_window_ex @handle, 0, nil, nil
|
506
|
+
else
|
507
|
+
nil
|
508
|
+
end
|
509
|
+
raise "Control '#{id}' not found" unless result
|
510
|
+
Window.new result
|
511
|
+
end
|
512
|
+
|
513
|
+
def children
|
514
|
+
enum_child_windows(@handle,'Msg').map{|child_handle| Window.new child_handle}
|
515
|
+
end
|
516
|
+
|
517
|
+
# emulate click of the control identified by id
|
518
|
+
def click(id)
|
519
|
+
h = child(id).handle
|
520
|
+
rectangle = [0, 0, 0, 0].pack 'LLLL'
|
521
|
+
get_window_rect h, rectangle
|
522
|
+
left, top, right, bottom = rectangle.unpack 'LLLL'
|
523
|
+
center = [(left + right) / 2, (top + bottom) / 2]
|
524
|
+
set_cursor_pos *center
|
525
|
+
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
|
526
|
+
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
|
527
|
+
end
|
528
|
+
|
529
|
+
def close
|
530
|
+
post_message @handle, WM_SYSCOMMAND, SC_CLOSE, 0
|
531
|
+
end
|
532
|
+
|
533
|
+
def wait_for_close
|
534
|
+
timeout(WG_CLOSE_TIMEOUT) do
|
535
|
+
sleep WG_SLEEP_DELAY while window_visible?(@handle)
|
536
|
+
end
|
537
|
+
end
|
538
|
+
|
539
|
+
def text
|
540
|
+
buffer = "\x0" * 2048
|
541
|
+
length = send_message @handle, WM_GETTEXT, buffer.length, buffer
|
542
|
+
length == 0 ? '' : buffer[0..length - 1]
|
543
|
+
end
|
544
|
+
end
|
545
|
+
|
546
|
+
end
|
547
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -26,13 +26,32 @@ module WinTest
|
|
26
26
|
|
27
27
|
TEST_IMPOSSIBLE = 'Impossible'
|
28
28
|
TEST_CONVERSION_ERROR = /Can.t convert/
|
29
|
+
TEST_SLEEP_DELAY = 0.01
|
30
|
+
TEST_APP_PATH = File.join(File.dirname(__FILE__), "test_apps/locknote/LockNote.exe" )
|
31
|
+
TEST_APP_START = 'start "" "' + TEST_APP_PATH + '"'
|
32
|
+
TEST_WIN_TITLE = 'LockNote - Steganos LockNote'
|
33
|
+
TEST_WIN_CLASS = 'ATL:00434098'
|
34
|
+
TEST_WIN_RECT = [710, 400, 1210, 800]
|
35
|
+
TEST_TEXTAREA_CLASS = 'ATL:00434310'
|
29
36
|
|
30
37
|
def use
|
31
|
-
lambda {yield}.should_not raise_error
|
38
|
+
lambda {yield}.should_not raise_error
|
32
39
|
end
|
33
40
|
|
34
41
|
def any_block
|
35
42
|
lambda {|*args| args}
|
36
43
|
end
|
37
44
|
|
45
|
+
def any_handle
|
46
|
+
unless respond_to? :find_window
|
47
|
+
require 'win/window'
|
48
|
+
include Win::Window
|
49
|
+
end
|
50
|
+
find_window(nil, nil)
|
51
|
+
end
|
52
|
+
|
53
|
+
def not_a_handle
|
54
|
+
123
|
55
|
+
end
|
56
|
+
|
38
57
|
end
|
Binary file
|
data/spec/win/library_spec.rb
CHANGED
@@ -51,6 +51,7 @@ module WinTest
|
|
51
51
|
def hide_method(*names) # hide original method(s) if it is defined
|
52
52
|
names.map(&:to_s).each do |name|
|
53
53
|
MyLib.module_eval do
|
54
|
+
# + remove_const
|
54
55
|
aliases = generate_names(name).flatten + [name]
|
55
56
|
aliases.map(&:to_s).each do |ali|
|
56
57
|
if method_defined? ali
|