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



136
137
138
139
140
# File 'lib/win-user32-ruby.rb', line 136

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

#clickObject



129
130
131
132
# File 'lib/win-user32-ruby.rb', line 129

def click
  cursor_center
  mouse_click
end

#cursor_centerObject

Centers cursor on this window

Returns

non-zero on success, zero otherwise



125
126
127
# File 'lib/win-user32-ruby.rb', line 125

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



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

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]



118
119
120
121
# File 'lib/win-user32-ruby.rb', line 118

def get_center
  r = get_window_rect
  [(r[0] + r[2]) / 2, (r[1] + r[3]) / 2]
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


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

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



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

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



152
153
154
155
156
157
158
159
160
# File 'lib/win-user32-ruby.rb', line 152

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

  r = b.call hwnd

  sleep 0.5
  r
end

#show_windowObject

Wrapper for ShowWindow.

Returns

non-zero on success, zero otherwise



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

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