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”. Raises an Au3Error if the control doesn’t exist.



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

def initialize(title, text, control_id)
  @title = title
  @text = text
  @c_id = control_id.to_s
  visible? #Raises an error if the control doesn't exist
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).



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

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.



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

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).



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

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)


192
193
194
# File 'lib/AutoItX3/control.rb', line 192

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

#focusObject

Gives the input focus to self.



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

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).



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

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.



134
135
136
137
138
139
# File 'lib/AutoItX3/control.rb', line 134

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.



142
143
144
145
146
147
# File 'lib/AutoItX3/control.rb', line 142

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



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

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.



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

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.



152
153
154
155
156
157
# File 'lib/AutoItX3/control.rb', line 152

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.



168
169
170
171
172
173
# File 'lib/AutoItX3/control.rb', line 168

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.



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

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.



160
161
162
163
164
165
# File 'lib/AutoItX3/control.rb', line 160

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)


187
188
189
# File 'lib/AutoItX3/control.rb', line 187

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