Class: AutoItX3::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/AutoItX3/control.rb

Overview

This is the superclass for all controls. If you don’t find a subclass of Control that matches your specific control, you can use the Control class itself (and if you call Window#focused_control, you will have to use it, since that method retuns a Control and not a subclass).

Direct Known Subclasses

Button, Edit, ListBox, ListView, TabBook, TreeView

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, text, control_id) ⇒ Control

Creates a new Control object. Pass in the title and text of the window holding the control (or “” if you don’t want to specify one of them) and the ID of the control. Instead of the ID you may use the name of the control in combination width the occurence number of it, like “Edit1” and “Edit2”.



43
44
45
46
47
# File 'lib/AutoItX3/control.rb', line 43

def initialize(title, text, control_id)
  @title = title
  @text = text
  @c_id = control_id.to_s
end

Class Method Details

.from_control(ctrl) ⇒ Object

Generates a control by using another control. This function is meant to be used with subclasses of Control, so you can do things like this:

#...
ctrl = window.focused_control #This returns a Control instance
#If you're sure it's an Edit, transform it into one: 
ctrl = AutoItX3::Edit.from_control(ctrl)
p ctrl.lines

Raises:

  • (ArgumentError)


32
33
34
35
# File 'lib/AutoItX3/control.rb', line 32

def from_control(ctrl)
  raise(ArgumentError, "Argument has to be a Control!") unless ctrl.kind_of? Control
  new(ctrl.instance_eval{@title}, ctrl.instance_eval{@text}, ctrl.instance_eval{@c_id})
end

.functionsObject



16
17
18
# File 'lib/AutoItX3/control.rb', line 16

def functions
  @functions
end

.functions=(hsh) ⇒ Object



20
21
22
# File 'lib/AutoItX3/control.rb', line 20

def functions=(hsh)
  @functions = hsh
end

Instance Method Details

#click(button = "Primary", clicks = 1, x = INTDEFAULT, y = INTDEFAULT) ⇒ Object

Clicks self with the given mouse button ("Primary" by default) click times (1 by default) at the given position (middle by default).



51
52
53
54
55
56
57
58
# File 'lib/AutoItX3/control.rb', line 51

def click(button = "Primary", clicks = 1, x = INTDEFAULT, y = INTDEFAULT)
  Control.functions[__method__] ||= AU3_Function.new("ControlClick", 'SSSSLLL', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide, button.wide, clicks, x, y)
  if res == 0
    raise(Au3Error, "Could not click control '#{@c_id}' in '#{@title}' for some reason!")
  end
  nil
end

#disableObject

Disables (“grays out”) self.



61
62
63
64
65
66
67
68
# File 'lib/AutoItX3/control.rb', line 61

def disable
  Control.functions[__method__] ||= AU3_Function.new("ControlDisable", 'SSS', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide)
  if res == 0
    raise(Au3Error, "Could not disable control '#{@c_id}' in '#{@title}'!")
  end
  nil
end

#enableObject

Enables self (i.e. make it accept user actions).



71
72
73
74
75
76
77
78
# File 'lib/AutoItX3/control.rb', line 71

def enable
  Control.functions[__method__] ||= AU3_Function.new("ControlEnable", 'SSS', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide)
  if res == 0
    raise(Au3Error, "Could not enable control '#{@c_id}' in '#{@title}'!")
  end
  nil
end

#enabled?Boolean

Returns true if a control can interact with the user (i.e. it’s not “grayed out”).

Returns:

  • (Boolean)


190
191
192
# File 'lib/AutoItX3/control.rb', line 190

def enabled?
  send_command_to_control("IsEnabled") == 1
end

#focusObject

Gives the input focus to self.



81
82
83
84
85
86
87
88
# File 'lib/AutoItX3/control.rb', line 81

def focus
  Control.functions[__method__] ||= AU3_Functino.new("ControlFocus", 'SSS', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide)
  if res == 0
    raise(Au3Error, "Could not focus control '#{@c_id}' in '#{@title}!")
  end
  nil
end

#handleObject

Returns the internal window handle of self. It can be used in advanced window mode or directly in Win32 API calls (but you have to call #to_i on the string than).



93
94
95
96
97
98
99
100
# File 'lib/AutoItX3/control.rb', line 93

def handle
  Control.functions[__method__] ||= AU3_Function.new("ControlGetHandle", 'SSSPI')
  buffer = " " * BUFFER_SIZE
  buffer.wide!
  Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide, buffer, BUFFER_SIZE - 1)
  raise_unfound if AutoItX3.last_error == 1
  buffer.normal.strip
end

#hideObject

Hides self.



132
133
134
135
136
137
# File 'lib/AutoItX3/control.rb', line 132

def hide
  Control.functions[__method__] ||= AU3_Function.new("ControlHide", 'SSS', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide)
  raise_unfound if res == 0
  nil
end

#move(x, y, width = -1,, height = -1)) ⇒ Object

Moves a control and optionally resizes it.



140
141
142
143
144
145
# File 'lib/AutoItX3/control.rb', line 140

def move(x, y, width = -1, height = -1)
  Control.functions[__method__] ||= AU3_Function.new("ControlMove", 'SSSLLLL', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide, x, y, width, height)
  raise_unfound if res == 0
  nil
end

#rectObject

Returns a 4-element array containing the control’s position and size. Form is: [ x , y , width , height ].



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/AutoItX3/control.rb', line 104

def rect
  Control.functions[:c_x] ||= AU3_Function.new("ControlGetPosX", 'SSS', 'L')
  Control.functions[:c_y] ||= AU3_Function.new("ControlGetPosY", 'SSS', 'L')
  Control.functions[:c_width] ||= AU3_Function.new("ControlGetPosWidth", 'SSS', 'L')
  Control.functions[:c_height] ||= AU3_Function.new("ControlGetPosHeight", 'SSS', 'L')
  
  params = [@title.wide, @text.wide, @c_id.wide]
  rectangle = [
    Control.functions[:c_x].call(*params), 
    Control.functions[:c_y].call(*params), 
    Control.functions[:c_width].call(*params), 
    Control.functions[:c_height].call(*params)
  ]
  raise_unfound if AutoItX3.last_error == 1
  rectangle
end

#send_command_to_control(command, arg = "") ⇒ Object

Sends a command to a control. You won’t need to use this method, since all commands are wrapped into methods. It’s only used internally.



175
176
177
178
179
180
181
182
# File 'lib/AutoItX3/control.rb', line 175

def send_command_to_control(command, arg = "")
  Control.functions[__method__] ||= AU3_Function.new("ControlCommand", 'SSSSSPI')
  buffer = " " * BUFFER_SIZE
  buffer.wide!
  Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide, command.wide, arg.to_s.wide, buffer, BUFFER_SIZE - 1)
  raise_unfound if AutoItX3.last_error == 1
  buffer.normal.strip
end

#send_keys(str, flag = 0) ⇒ Object

Simulates user input to a control. This works normally even on hidden and inactive windows. Please note that this method cannot send every keystroke AutoItX3.send_keys can, notably [ALT] combinations.



150
151
152
153
154
155
# File 'lib/AutoItX3/control.rb', line 150

def send_keys(str, flag = 0)
  Control.functions[__method__] ||= AU3_Function.new("ControlSend", 'SSSSI', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide, str.wide, flag)
  raise_unfound if res == 0
  nil
end

#showObject

Shows a hidden control.



166
167
168
169
170
171
# File 'lib/AutoItX3/control.rb', line 166

def show
  Control.functions[__method__] ||= AU3_Function.new("ControlShow", 'SSS', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide)
  raise_unfound if res == 0
  nil
end

#textObject

Returns the self‘s text.



122
123
124
125
126
127
128
129
# File 'lib/AutoItX3/control.rb', line 122

def text
  Control.functions[__method__] ||= AU3_Function.new("ControlGetText", 'SSSPI')
  buffer = " " * BUFFER_SIZE
  buffer.wide!
  Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide, buffer, BUFFER_SIZE - 1)
  raise_unfound if AutoItX3.last_error == 1
  buffer.normal.strip
end

#text=(text) ⇒ Object

Sets the text of a control directly.



158
159
160
161
162
163
# File 'lib/AutoItX3/control.rb', line 158

def text=(text)
  Control.functions[__method__] ||= AU3_Function.new("ControlSetText", 'SSSS', 'L')
  res = Control.functions[__method__].call(@title.wide, @text.wide, @c_id.wide, text.wide)
  raise_unfound if res == 0
  text
end

#visible?Boolean

Returns wheather or not a control is visible.

Returns:

  • (Boolean)


185
186
187
# File 'lib/AutoItX3/control.rb', line 185

def visible?
  send_command_to_control("IsVisible") == 1
end