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.



17
18
19
# File 'lib/win-user32-ruby.rb', line 17

def initialize(hwnd)
  @handle = hwnd
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



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

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



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/win-user32-ruby.rb', line 28

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



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

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

  r = b.call

  sleep 0.5
  r
end

.get_top_windowObject

Wrapper for GetTopWindow.

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



75
76
77
78
79
80
81
82
# File 'lib/win-user32-ruby.rb', line 75

def self.get_top_window
  parent = 0
  fw= user32 'GetTopWindow', 'L', 'L'

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

  Window.new wnd
end

Instance Method Details

#bring_window_to_topObject

Wrapper for BringWindowToTop.

hwnd

handle to window



140
141
142
143
144
# File 'lib/win-user32-ruby.rb', line 140

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

#clickObject



133
134
135
136
# File 'lib/win-user32-ruby.rb', line 133

def click
  cursor_center
  mouse_click
end

#cursor_centerObject



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

def cursor_center
  set_cursor_pos(*get_center)
end

#exit!Object

Helper to Post Message to close window

hwnd

handle to the window to close



188
189
190
191
# File 'lib/win-user32-ruby.rb', line 188

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

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

Return

window handle



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/win-user32-ruby.rb', line 44

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



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

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

#get_next_childObject

Wrapper for GetWindow.

hwnd

handle to window on which cmd will be called

cmd: command to issue to hwnd

Return

handle to window according to cmd



88
89
90
91
92
93
# File 'lib/win-user32-ruby.rb', line 88

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.

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



62
63
64
65
66
67
68
69
# File 'lib/win-user32-ruby.rb', line 62

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.

hwnd

handle the window whose rectangle we are trying to get

Return
left, top, right, bottom


111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/win-user32-ruby.rb', line 111

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]
end

#get_window_textObject

Wrapper for WM_GETTEXT.

hwnd

handle to the window whose text we want

Return

window text



98
99
100
101
102
103
104
105
# File 'lib/win-user32-ruby.rb', line 98

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.

hwnd

handle to window

Return

window handle



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

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

#set_foreground_windowObject

Wrapper for SetForegroundWindow.

hwnd

handle to window

Return

true if successful



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

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.

hwnd

handle to window

Return

true if successful



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

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

#textObject



21
22
23
# File 'lib/win-user32-ruby.rb', line 21

def text

end