Class: Autogui::Window

Inherits:
Object
  • Object
show all
Includes:
Input, Logging, Windows::Window, Windows::Window::Classes, Windows::Window::Message
Defined in:
lib/win32/autogui/window.rb

Overview

Wrapper for window

Constant Summary

Constants included from Input

Input::KEYBD_EVENT_KEYDOWN, Input::KEYBD_EVENT_KEYUP, Input::KEYBD_KEYDELAY, Input::VK_0, Input::VK_1, Input::VK_2, Input::VK_3, Input::VK_4, Input::VK_5, Input::VK_6, Input::VK_7, Input::VK_8, Input::VK_9, Input::VK_A, Input::VK_ADD, Input::VK_APPS, Input::VK_B, Input::VK_BACK, Input::VK_C, Input::VK_CANCEL, Input::VK_CLEAR, Input::VK_CONTROL, Input::VK_D, Input::VK_DECIMAL, Input::VK_DELETE, Input::VK_DIVIDE, Input::VK_DOWN, Input::VK_E, Input::VK_END, Input::VK_ESCAPE, Input::VK_EXECUTE, Input::VK_F, Input::VK_F1, Input::VK_F10, Input::VK_F11, Input::VK_F12, Input::VK_F2, Input::VK_F3, Input::VK_F4, Input::VK_F5, Input::VK_F6, Input::VK_F7, Input::VK_F8, Input::VK_F9, Input::VK_G, Input::VK_H, Input::VK_HELP, Input::VK_HOME, Input::VK_I, Input::VK_INSERT, Input::VK_J, Input::VK_K, Input::VK_L, Input::VK_LBUTTON, Input::VK_LCONTROL, Input::VK_LEFT, Input::VK_LMENU, Input::VK_LSHIFT, Input::VK_LWIN, Input::VK_M, Input::VK_MENU, Input::VK_MULTIPLY, Input::VK_N, Input::VK_NEXT, Input::VK_NUMLOCK, Input::VK_NUMPAD0, Input::VK_NUMPAD1, Input::VK_NUMPAD2, Input::VK_NUMPAD3, Input::VK_NUMPAD4, Input::VK_NUMPAD5, Input::VK_NUMPAD6, Input::VK_NUMPAD7, Input::VK_NUMPAD8, Input::VK_NUMPAD9, Input::VK_O, Input::VK_OEM_1, Input::VK_OEM_2, Input::VK_OEM_3, Input::VK_OEM_4, Input::VK_OEM_5, Input::VK_OEM_6, Input::VK_OEM_7, Input::VK_OEM_8, Input::VK_OEM_COMMA, Input::VK_OEM_EQU, Input::VK_OEM_MINUS, Input::VK_OEM_PERIOD, Input::VK_OEM_PLUS, Input::VK_P, Input::VK_PAUSE, Input::VK_PRIOR, Input::VK_Q, Input::VK_R, Input::VK_RBUTTON, Input::VK_RCONTROL, Input::VK_RETURN, Input::VK_RIGHT, Input::VK_RMENU, Input::VK_RSHIFT, Input::VK_RWIN, Input::VK_S, Input::VK_SCROLL, Input::VK_SELECT, Input::VK_SEPARATOR, Input::VK_SHIFT, Input::VK_SNAPSHOT, Input::VK_SPACE, Input::VK_SUBTRACT, Input::VK_T, Input::VK_TAB, Input::VK_U, Input::VK_UP, Input::VK_V, Input::VK_W, Input::VK_X, Input::VK_Y, Input::VK_Z

Constants included from Logging

Logging::DEBUG, Logging::ERROR, Logging::FATAL, Logging::INFO, Logging::STANDARD_LOGGER, Logging::WARN

Constants included from Windows::Window

Windows::Window::SC_CLOSE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Input

#keystroke, #type_in

Methods included from Logging

#logger

Constructor Details

#initialize(handle) ⇒ Window

Returns a new instance of Window.



91
92
93
# File 'lib/win32/autogui/window.rb', line 91

def initialize(handle)
  @handle = handle
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



89
90
91
# File 'lib/win32/autogui/window.rb', line 89

def handle
  @handle
end

Instance Method Details

#childrenObject

enumerable immeadiate child windows

See Also:



99
100
101
# File 'lib/win32/autogui/window.rb', line 99

def children
  Children.new(self)
end

#close(options = {}) ⇒ Object

PostMessage SC_CLOSE and optionally wait for the window to close

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :wait_for_close (Boolean) — default: true

    sleep while waiting for timeout or close

  • :timeout (Boolean) — default: 5

    wait_for_close timeout in seconds



116
117
118
119
# File 'lib/win32/autogui/window.rb', line 116

def close(options={})
  PostMessage(handle, WM_SYSCOMMAND, SC_CLOSE, 0)
  wait_for_close(options) if (options[:wait_for_close] == true)
end

#combined_textString

The window text including all child windows joined together with newlines. Faciliates matching text. Text from any given window is limited to 2048 characters

Examples:

partial match of the Window’s calulator’s about dialog copywrite text


dialog_about = @calculator.dialog_about
dialog_about.title.should == "About Calculator"
dialog_about.combined_text.should match(/Microsoft . Calculator/)

Returns:

  • (String)

    with newlines



246
247
248
249
250
251
252
253
254
# File 'lib/win32/autogui/window.rb', line 246

def combined_text
  return unless is_window?
  t = []
  t << text unless text == ''
  children.each do |w|
    t << w.combined_text unless w.combined_text == ''
  end
  t.join("\n")
end

#inspectString

Debugging information

Returns:

  • (String)

    with child window information



259
260
261
262
263
264
265
# File 'lib/win32/autogui/window.rb', line 259

def inspect
  c = []
  children.each do |w|
    c << w.inspect
  end
  s = super + " #{self.class}=<window_class:#{window_class} pid:#{pid} thread_id:#{thread_id} title:\"#{title}\" children=<" + c.join("\n") + ">>"
end

#is_control?Boolean

Determines whether the specified window handle identifies a window or a control

Returns:

  • (Boolean)


166
167
168
# File 'lib/win32/autogui/window.rb', line 166

def is_control?
  (handle != 0) && (GetDlgCtrlID(handle) != 0)
end

#is_window?Boolean

Determines whether the specified window handle identifies an existing window

Returns:

  • (Boolean)


174
175
176
# File 'lib/win32/autogui/window.rb', line 174

def is_window?
  (handle != 0) && (IsWindow(handle) != 0)
end

#parentObject

Returns Window or nil.

Returns:

  • (Object)

    Window or nil



105
106
107
108
# File 'lib/win32/autogui/window.rb', line 105

def parent
  h = GetParent(handle)
  Window.new h if h > 0
end

#pidInteger

The identifier (pid) of the process that created the window

Returns:

  • (Integer)

    process id if the window exists, otherwise nil



218
219
220
221
222
223
# File 'lib/win32/autogui/window.rb', line 218

def pid
  return nil unless is_window?
  process_id = 0.chr * 4
  GetWindowThreadProcessId(handle, process_id)
  process_id = process_id.unpack('L').first
end

#set_focusNumber

Brings the window into the foreground and activates it. Keyboard input is directed to the window, and various visual cues are changed for the user.

A process can set the foreground window only if one of the following conditions is true:

* The process is the foreground process.
* The process was started by the foreground process.
* The process received the last input event.
* There is no foreground process.
* The foreground process is being debugged.
* The foreground is not locked.
* The foreground lock time-out has expired.
* No menus are active.

Returns:

  • (Number)

    nonzero number if sucessful, nil or zero if failed



203
204
205
206
207
208
209
210
211
212
# File 'lib/win32/autogui/window.rb', line 203

def set_focus
  if is_window?
    # if current process was the last to receive input, we can be sure that
    # SetForegroundWindow will be allowed.  Send the shift key to whatever has
    # the focus now.  This allows IRB to set_focus.
    keystroke(VK_SHIFT)
    ret = SetForegroundWindow(handle)
    logger.warn("SetForegroundWindow failed") if ret == 0
  end
end

#text(max_length = 2048) ⇒ String Also known as: title

Window text (GetWindowText or WM_GETTEXT)

Parameters:

  • max_length (Number) (defaults to: 2048)

    (2048)

Returns:

  • (String)

    of max_length (2048)



150
151
152
153
154
155
156
157
158
159
# File 'lib/win32/autogui/window.rb', line 150

def text(max_length = 2048)
  buffer = "\0" * max_length
  length = if is_control?
    SendMessageA(handle, WM_GETTEXT, buffer.length, buffer)
  else
    GetWindowText(handle, buffer, buffer.length)
  end

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

#thread_idInteger

The identifier of the thread that created the window

Returns:

  • (Integer)

    thread id if the window exists, otherwise nil



229
230
231
232
# File 'lib/win32/autogui/window.rb', line 229

def thread_id
  return nil unless is_window?
  GetWindowThreadProcessId(handle, nil)
end

#visible?Boolean

Determines the visibility state of the window

Returns:

  • (Boolean)


182
183
184
# File 'lib/win32/autogui/window.rb', line 182

def visible?
  is_window? && (IsWindowVisible(handle) != 0)
end

#wait_for_close(options = {}) ⇒ Object

Wait for the window to close

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :timeout (Boolean) — default: 5

    timeout in seconds



126
127
128
129
130
131
132
133
134
# File 'lib/win32/autogui/window.rb', line 126

def wait_for_close(options={})
  seconds = options[:timeout] || 5
  timeout(seconds) do
    begin
      yield if block_given?
      sleep 0.05
    end until 0 == IsWindow(handle)
  end
end

#window_classString

Returns the Windows ClassName.

Returns:

  • (String)

    the Windows ClassName



138
139
140
141
142
# File 'lib/win32/autogui/window.rb', line 138

def window_class
  buffer = "\0" * 255
  length = GetClassName(handle, buffer, buffer.length)
  length == 0 ? '' : buffer[0..length - 1]
end