Class: Rum::System::Window

Inherits:
Object
  • Object
show all
Includes:
Rum::System
Defined in:
lib/rum/windows/system.rb

Constant Summary

Constants included from Rum::System

EM_GETSEL, EM_SETSEL, GWL_EXSTYLE, HWND_NOTOPMOST, HWND_TOPMOST, SC_CLOSE, SC_MAXIMIZE, SC_MINIMIZE, SC_RESTORE, SWP_NOMOVE, SWP_NOSIZE, SW_FORCEMINIMIZE, SW_HIDE, SW_MAX, SW_MAXIMIZE, SW_MINIMIZE, SW_NORMAL, SW_RESTORE, SW_SHOW, SW_SHOWDEFAULT, SW_SHOWMAXIMIZED, SW_SHOWMINIMIZED, SW_SHOWMINNOACTIVE, SW_SHOWNA, SW_SHOWNOACTIVATE, SW_SHOWNORMAL, ShellExecute, WM_COMMAND, WM_GETTEXT, WM_SYSCOMMAND, WS_EX_TOPMOST

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rum::System

#active_window, #active_window_handles, #active_windows, #applescript, #applescript_quote_string, #c_string, def_api, #escape_shell_word, #get_selection, #input_box, #message_box, #send_key_event, #send_key_event_for_id, #send_keypress, #send_unicode_char, snake_case, #spawn_in_terminal, #start, #terminal_window

Constructor Details

#initialize(handle) ⇒ Window

Returns a new instance of Window.



101
102
103
# File 'lib/rum/windows/system.rb', line 101

def initialize(handle)
  @handle = handle
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



99
100
101
# File 'lib/rum/windows/system.rb', line 99

def handle
  @handle
end

Class Method Details

.[](arg, class_name = nil) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/rum/windows/system.rb', line 244

def self.[] arg, class_name=nil
  if arg.is_a? Hash
    WindowMatcher.new(arg[:title], arg[:class_name])
  else
    WindowMatcher.new(arg, class_name)
  end
end

Instance Method Details

#==(other) ⇒ Object



105
106
107
108
# File 'lib/rum/windows/system.rb', line 105

def == other
  return false unless other.respond_to? :handle
  @handle == other.handle
end

#active?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/rum/windows/system.rb', line 110

def active?
  self.handle == get_foreground_window
end

#child(id) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rum/windows/system.rb', line 195

def child(id)
  result = case id
           when String
             by_title = find_window_ex @handle, 0, nil, id.gsub('_', '&')
             by_class = find_window_ex @handle, 0, id, nil
             by_title > 0 ? by_title : by_class
           when Fixnum
             get_dlg_item @handle, id
           else
             0
           end

  raise "Control '#{id}' not found" if result == 0
  Window.new result
end

#child_window(class_name) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/rum/windows/system.rb', line 211

def child_window class_name
  if class_name.is_a? Regexp
    child_windows.find { |child| child.class_name =~ class_name }
  else
    child_windows.find { |child| child.class_name == class_name }
  end
end

#child_windowsObject



219
220
221
222
223
# File 'lib/rum/windows/system.rb', line 219

def child_windows
  Enumerator.new do |yielder|
    enum_child_windows { |handle| yielder.yield Window.new(handle) }
  end
end

#class_name(max_length = 2048) ⇒ Object



183
184
185
186
187
# File 'lib/rum/windows/system.rb', line 183

def class_name(max_length = 2048)
  buffer = "\0" * max_length
  length = get_class_name(@handle, buffer, buffer.length)
  length == 0 ? '' : buffer[0..length - 1]
end

#closeObject



114
115
116
# File 'lib/rum/windows/system.rb', line 114

def close
  post_message @handle, WM_SYSCOMMAND, SC_CLOSE, 0
end

#exe_nameObject



240
241
242
# File 'lib/rum/windows/system.rb', line 240

def exe_name
  File.basename(exe_path, '.exe')
end

#exe_pathObject



233
234
235
236
237
238
# File 'lib/rum/windows/system.rb', line 233

def exe_path
  exe_path_internal.
    force_encoding(Encoding::UTF_16LE).
    encode(Encoding::UTF_8).
    gsub('\\', '/')
end

#file_dialog?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/rum/windows/system.rb', line 151

def file_dialog?
  class_name == '#32770'
end

#force_showObject

Don’t underestimate the Force



143
144
145
146
147
148
149
# File 'lib/rum/windows/system.rb', line 143

def force_show
  100.times do
    show
    sleep 0.02
    return true if active_window == self
  end
end

#hideObject



118
119
120
# File 'lib/rum/windows/system.rb', line 118

def hide
  show_window(@handle, SW_HIDE)
end

#kill_taskObject



225
226
227
# File 'lib/rum/windows/system.rb', line 225

def kill_task
  System.kill_task exe_name
end

#maximizeObject



130
131
132
# File 'lib/rum/windows/system.rb', line 130

def maximize
  post_message @handle, WM_SYSCOMMAND, SC_MAXIMIZE, 0
end

#minimizeObject



134
135
136
# File 'lib/rum/windows/system.rb', line 134

def minimize
  post_message @handle, WM_SYSCOMMAND, SC_MINIMIZE, 0
end

#move(x, y, width, height) ⇒ Object



170
171
172
173
# File 'lib/rum/windows/system.rb', line 170

def move(x, y, width, height)
  restore if zoomed?
  move_window(@handle, x, y, width, height, 1)
end

#office_file_dialog?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/rum/windows/system.rb', line 155

def office_file_dialog?
  class_name =~ /^bosa_sdm/
end

#reportObject



229
230
231
# File 'lib/rum/windows/system.rb', line 229

def report
  "Title: #{title}\nClass: #{class_name}"
end

#restoreObject



138
139
140
# File 'lib/rum/windows/system.rb', line 138

def restore
  post_message @handle, WM_SYSCOMMAND, SC_RESTORE, 0
end

#text(max_length = 2048) ⇒ Object



189
190
191
192
193
# File 'lib/rum/windows/system.rb', line 189

def text(max_length = 2048)
  buffer = '\0' * max_length
  length = send_with_buffer @handle, WM_GETTEXT, buffer.length, buffer
  length == 0 ? '' : buffer[0..length - 1]
end

#title(max_length = 1024) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/rum/windows/system.rb', line 175

def title(max_length = 1024)
  buffer = "\0" * (max_length * 2)
  length = get_window_text_w(@handle, buffer, buffer.length)
  result = (length == 0 ? '' : buffer[0..(length * 2 - 1)])
  result.force_encoding(Encoding::UTF_16LE)
  result.encode(Encoding::UTF_8, invalid: :replace)
end

#toggle_always_on_topObject



159
160
161
162
163
164
165
166
167
168
# File 'lib/rum/windows/system.rb', line 159

def toggle_always_on_top
  window_style = get_window_long(@handle, GWL_EXSTYLE)
  return if window_style == 0
  topmost_or_not = if (window_style & WS_EX_TOPMOST == WS_EX_TOPMOST)
                     HWND_NOTOPMOST
                   else
                     HWND_TOPMOST
                   end
  set_window_pos(@handle, topmost_or_not, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
end

#toggle_maximizedObject



126
127
128
# File 'lib/rum/windows/system.rb', line 126

def toggle_maximized
  zoomed? ? restore : maximize
end

#zoomed?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/rum/windows/system.rb', line 122

def zoomed?
  is_zoomed(@handle) == 1 ? true : false
end