Class: WinUser32Ruby::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/win-user32-ruby.rb

Constant Summary collapse

TIMEOUT =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hwnd) ⇒ Window

Returns a new instance of Window.



15
16
17
# File 'lib/win-user32-ruby.rb', line 15

def initialize(hwnd)
  @handle = hwnd
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



13
14
15
# File 'lib/win-user32-ruby.rb', line 13

def handle
  @handle
end

Class Method Details

.find_window(title, class_name = nil) ⇒ Object

Wrapper for FindWindow.

title

is the title of the window we are trying to find

class_name: is the predefined registered control-class name, generally not used

Return

window handle



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/win-user32-ruby.rb', line 25

def self.find_window(title, class_name = nil)
  fw= user32 'FindWindow', 'PP', 'L'

  begin
    wnd = timeout(TIMEOUT) {sleep 0.2 while (h = fw.call class_name, title) <= 0; h}
  rescue TimeoutError
    return nil
  end

  Window.new wnd
end

.get_foreground_windowObject

Wrapper for GetForegroundWindow.

Returns

WinUser32Ruby::Window



39
40
41
42
43
44
45
46
# File 'lib/win-user32-ruby.rb', line 39

def self.get_foreground_window
  b = user32 'GetForegroundWindow', 'V', 'L'

  r = b.call

  sleep 0.5
  r = Window.new(r)
end

Instance Method Details

#bring_window_to_topObject

Wrapper for BringWindowToTop.

Returns

non-zero on success, zero otherwise



162
163
164
165
166
# File 'lib/win-user32-ruby.rb', line 162

def bring_window_to_top
  hwnd = @handle
  b = user32 'BringWindowToTop', 'L', 'I'
  b.call hwnd
end

#clickObject



155
156
157
158
# File 'lib/win-user32-ruby.rb', line 155

def click
  cursor_center
  mouse_click
end

#cursor_centerObject

Centers cursor on this window

Returns

non-zero on success, zero otherwise



151
152
153
# File 'lib/win-user32-ruby.rb', line 151

def cursor_center
  set_cursor_pos(*get_center)
end

#exit!Object

Helper to Post Message to close window

Returns

non-zero on success, zero otherwise



198
199
200
201
# File 'lib/win-user32-ruby.rb', line 198

def exit!
  hwnd = @handle
  send_message hwnd, WM_CLOSE, 0, 0
end

#find_window_ex(title, child = 0, class_name = nil) ⇒ Object

Wrapper for FindWindowEx.

title

is the title of the window we are trying to find

child

handle to child from which to start the search

class_name: is the predefined registered control-class name, generally not used

Return

window handle



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/win-user32-ruby.rb', line 55

def find_window_ex(title, child = 0, class_name = nil)
  parent = @handle

  fw= user32 'FindWindowEx', 'LLPP', 'L'

  begin
    wnd = timeout(TIMEOUT) {sleep 0.2 while (h = fw.call parent, child, class_name, title) <= 0; h}
  rescue TimeoutError
    return nil
  end

  Window.new wnd
end

#get_centerObject

Gets the coordinates to the center of this window

Returns

[x, y]



144
145
146
147
# File 'lib/win-user32-ruby.rb', line 144

def get_center
  r = get_window_rect
  [(r[0] + r[2]) / 2, (r[1] + r[3]) / 2]
end

#get_client_rectObject

Wrapper for GetClientRect.

Return

[left, top, right, bottom], coordinates are in screen coordinates and not relative to the client window as in the equivalent user32.dll



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/win-user32-ruby.rb', line 129

def get_client_rect
  hwnd = @handle
  crect = [0, 0, 0, 0].pack("L*")
  gr = user32 'GetClientRect', 'LP', 'I'
  gr.call hwnd, crect

  left, top, right, bottom = crect.unpack("L*")
  crect = [left, top, right, bottom].map { |e| [e].pack('L').unpack('l').first }

  pout = client_to_screen(hwnd, crect[0], crect[1])
  [pout[0], pout[1], pout[0] + crect[2], pout[1] + crect[3]]
end

#get_next_childObject

Wrapper for GetWindow.

Return

handle to window according to cmd



82
83
84
85
86
87
# File 'lib/win-user32-ruby.rb', line 82

def get_next_child
  hwnd, cmd = @handle, GW_CHILD
  fw = user32 'GetWindow', 'LI', 'L'

  Window.new fw.call(hwnd, cmd)
end

#get_top_windowObject

Wrapper for GetTopWindow.

Return

window handle



71
72
73
74
75
76
77
78
# File 'lib/win-user32-ruby.rb', line 71

def get_top_window
  parent = @handle
  fw= user32 'GetTopWindow', 'L', 'L'

  wnd = timeout(TIMEOUT) {sleep 0.2 while (h = fw.call parent) <= 0; h}

  Window.new wnd
end

#get_window_rectObject

Wrapper for GetWindowRect.

Return
left, top, right, bottom


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/win-user32-ruby.rb', line 114

def get_window_rect
  hwnd = @handle
  rect = [0, 0, 0, 0].pack("L*")

  gr = user32 'GetWindowRect', 'LP', 'I'

  gr.call hwnd, rect

  left, top, right, bottom = rect.unpack("L*")

  [left, top, right, bottom].map{|e| [e].pack('L').unpack('l').first}
end

#get_window_textObject

Wrapper for WM_GETTEXT.

Return

window text



91
92
93
94
95
96
97
98
# File 'lib/win-user32-ruby.rb', line 91

def get_window_text
  hwnd = @handle
  buffer = ' ' * 2048

  length = send_message hwnd, WM_GETTEXT, buffer.length, buffer

  length == 0 ? '' : buffer[0..length - 1]
end

#set_focusObject

Wrapper for SetFocus.

Returns

non-zero on success, zero otherwise



170
171
172
173
174
# File 'lib/win-user32-ruby.rb', line 170

def set_focus
  hwnd = @handle
  b = user32 'SetFocus', 'L', 'L'
  b.call hwnd
end

#set_foreground_windowObject

Wrapper for SetForegroundWindow.

Returns

non-zero on success, zero otherwise



178
179
180
181
182
183
184
185
186
# File 'lib/win-user32-ruby.rb', line 178

def set_foreground_window
  hwnd = @handle
  b = user32 'SetForegroundWindow', 'L', 'I'

  r = b.call hwnd

  sleep 0.5
  r
end

#set_window_text(text) ⇒ Object

Wrapper for WM_SETTEXT.

text

desired window text

Return


103
104
105
106
107
108
109
110
# File 'lib/win-user32-ruby.rb', line 103

def set_window_text(text)

  text = text + "\0"

  ret = send_message @handle, WM_SETTEXT, 0, text

  ret != 0
end

#show_windowObject

Wrapper for ShowWindow.

Returns

non-zero on success, zero otherwise



190
191
192
193
194
# File 'lib/win-user32-ruby.rb', line 190

def show_window
  hwnd = @handle
  b = user32 'ShowWindow', 'LI', 'I'
  b.call hwnd, SW_RESTORE
end