Difference between revisions of "Lua:Class:Control"

From Cheat Engine
Jump to navigation Jump to search
(Undo revision 5662 by TheyCallMeTim13 (Talk))
(Syntax Highlighting.)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
Control '''class''': ('''Inheritance''': ''[[Lua:Class:Component|Component]]''->''[[Lua:Class:Object|Object]]'')
+
{{Class|'''class''' Control ''':''' Component}}
  
Base class for gui controls.
+
The Control class represents a visual GUI control.
  
== Properties ==
+
A Control inherits from Component and Object. It provides common visual properties such as position, size, caption, visibility, color, font, parent control, and click handling.
  
; Caption : string
+
===Inheritance===
: The text of a control.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Class
 +
!align="left"|Inherits From
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|Control
 +
|Component
 +
|Base class for visual controls.
 +
|-
 +
|Component
 +
|Object
 +
|Base class for components.
 +
|}
  
; Top : integer
+
===Properties===
: The x position.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Property
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|Caption
 +
|String
 +
|The text of the control.
 +
|-
 +
|Top
 +
|Integer
 +
|The vertical position of the control relative to its parent.
 +
|-
 +
|Left
 +
|Integer
 +
|The horizontal position of the control relative to its parent.
 +
|-
 +
|Width
 +
|Integer
 +
|The width of the control.
 +
|-
 +
|Height
 +
|Integer
 +
|The height of the control.
 +
|-
 +
|ClientWidth
 +
|Integer
 +
|The usable width inside the control, excluding borders.
 +
|-
 +
|ClientHeight
 +
|Integer
 +
|The usable height inside the control, excluding borders.
 +
|-
 +
|Align
 +
|AlignmentOption
 +
|The alignment of the control.
 +
|-
 +
|Enabled
 +
|Boolean
 +
|Determines whether the control is usable or disabled.
 +
|-
 +
|Visible
 +
|Boolean
 +
|Determines whether the control is visible.
 +
|-
 +
|Color
 +
|ColorDefinition or RGBInteger
 +
|The color of the control. This does not affect the caption text.
 +
|-
 +
|RGBColor
 +
|RGBInteger
 +
|The color of the control in RGB format.
 +
|-
 +
|Parent
 +
|WinControl
 +
|The parent of this control.
 +
|-
 +
|PopupMenu
 +
|PopupMenu
 +
|The popup menu that is shown when right-clicking the control.
 +
|-
 +
|Font
 +
|Font
 +
|The Font object associated with the control.
 +
|-
 +
|OnClick
 +
|Function
 +
|The function to call when the control is clicked.
 +
|-
 +
|OnChangeBounds
 +
|Function
 +
|The function to call when the size or position of the control changes.
 +
|}
  
; Left : integer
+
===Event Properties===
: The y position.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Event
 +
!align="left"|Function Signature
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|OnClick
 +
|function(sender)
 +
|Called when the control is clicked.
 +
|-
 +
|OnChangeBounds
 +
|function(sender)
 +
|Called when the size or position of the control changes.
 +
|}
  
; Width : integer
+
===Methods===
: The width of the control.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Method
 +
!align="left"|Return Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|getLeft()
 +
|Integer
 +
|Returns the horizontal position of the control.
 +
|-
 +
|setLeft(integer)
 +
|void
 +
|Sets the horizontal position of the control.
 +
|-
 +
|getTop()
 +
|Integer
 +
|Returns the vertical position of the control.
 +
|-
 +
|setTop(integer)
 +
|void
 +
|Sets the vertical position of the control.
 +
|-
 +
|getWidth()
 +
|Integer
 +
|Returns the width of the control.
 +
|-
 +
|setWidth(integer)
 +
|void
 +
|Sets the width of the control.
 +
|-
 +
|getHeight()
 +
|Integer
 +
|Returns the height of the control.
 +
|-
 +
|setHeight(integer)
 +
|void
 +
|Sets the height of the control.
 +
|-
 +
|setCaption(caption)
 +
|void
 +
|Sets the text of the control.
 +
|-
 +
|getCaption()
 +
|String
 +
|Returns the text of the control.
 +
|-
 +
|setPosition(x, y)
 +
|void
 +
|Sets the x and y position of the control based on the top-left position, relative to the client area of the parent object.
 +
|-
 +
|getPosition()
 +
|Integer, Integer
 +
|Returns the x and y position of the control, relative to the client area of the parent object.
 +
|-
 +
|setSize(width, height)
 +
|void
 +
|Sets the width and height of the control.
 +
|-
 +
|getSize()
 +
|Integer, Integer
 +
|Returns the width and height of the control.
 +
|-
 +
|setAlign(alignmentoption)
 +
|void
 +
|Sets the alignment of the control.
 +
|-
 +
|getAlign()
 +
|AlignmentOption
 +
|Returns the alignment of the control.
 +
|-
 +
|getEnabled()
 +
|Boolean
 +
|Returns the enabled state of the control.
 +
|-
 +
|setEnabled(boolean)
 +
|void
 +
|Sets the enabled state of the control.
 +
|-
 +
|getVisible()
 +
|Boolean
 +
|Returns the visible state of the control.
 +
|-
 +
|setVisible(boolean)
 +
|void
 +
|Sets the visible state of the control.
 +
|-
 +
|getColor()
 +
|ColorDefinition or RGBInteger
 +
|Returns the color of the control.
 +
|-
 +
|setColor(rgb)
 +
|void
 +
|Sets the color of the control.
 +
|-
 +
|getParent()
 +
|WinControl or nil
 +
|Returns the parent object, or nil if no parent is assigned.
 +
|-
 +
|setParent(wincontrol)
 +
|void
 +
|Sets the parent of this control.
 +
|-
 +
|getPopupMenu()
 +
|PopupMenu
 +
|Returns the assigned popup menu.
 +
|-
 +
|setPopupMenu(popupmenu)
 +
|void
 +
|Sets the popup menu for this control.
 +
|-
 +
|getFont()
 +
|Font
 +
|Returns the Font object of this control.
 +
|-
 +
|setFont(font)
 +
|void
 +
|Assigns a new Font object. This is not recommended; usually, the existing Font object should be modified instead.
 +
|-
 +
|repaint()
 +
|void
 +
|Invalidates the graphical area of the control and forces an update.
 +
|-
 +
|refresh()
 +
|void
 +
|Updates the control. Usually causes a repaint.
 +
|-
 +
|update()
 +
|void
 +
|Only updates invalidated areas.
 +
|-
 +
|setOnClick(functionnameorstring)
 +
|void
 +
|Sets the OnClick routine.
 +
|-
 +
|getOnClick()
 +
|Function
 +
|Returns the current OnClick function.
 +
|-
 +
|doClick()
 +
|void
 +
|Executes the current OnClick function.
 +
|-
 +
|bringToFront()
 +
|void
 +
|Changes the z-order of the control so it is at the front.
 +
|-
 +
|sendToBack()
 +
|void
 +
|Changes the z-order of the control so it is at the back.
 +
|-
 +
|screenToClient(x, y)
 +
|Integer, Integer
 +
|Converts screen coordinates to coordinates relative to the control.
 +
|-
 +
|clientToScreen(x, y)
 +
|Integer, Integer
 +
|Converts coordinates relative to the control to screen coordinates.
 +
|}
  
; Height : integer
+
===Examples===
: The height of the control.
+
<syntaxhighlight lang="lua" line>
 +
local form = createForm()
 +
form.Caption = "Control Example"
 +
form.Width = 400
 +
form.Height = 250
  
; ClientWidth &#58; integer
+
local button = createButton(form)
: The usable width inside the control (minus the borders).
+
button.Parent = form
 +
button.Caption = "Click me"
 +
button.Left = 20
 +
button.Top = 20
 +
button.Width = 120
 +
button.Height = 30
  
; ClientHeight &#58; integer
+
button.OnClick = function(sender)
: The usable height the control (minus the borders).
+
  print(sender.Caption .. " clicked")
 +
end
  
; Align &#58; AlignmentOption
+
form.show()
: Alignment of the control.
+
</syntaxhighlight>
  
; Enabled &#58; boolean
+
<syntaxhighlight lang="lua" line>
: Determines if the object is usable or greyed out.
+
local form = createForm()
 +
form.Caption = "Position and Size Example"
  
; Visible &#58; boolean
+
local label = createLabel(form)
: Determines if the object is visible or not.
+
label.Parent = form
 +
label.Caption = "Hello"
 +
label.setPosition(20, 30)
 +
label.setSize(200, 24)
  
; Color &#58; ColorDefinition/RGBInteger
+
local x, y = label.getPosition()
: The color of the object. Does not affect the caption.
+
local w, h = label.getSize()
  
; Parent &#58; WinControl
+
print("Position: " .. tostring(x) .. ", " .. tostring(y))
: The owner of this control.
+
print("Size: " .. tostring(w) .. " x " .. tostring(h))
  
; PopupMenu &#58; PopupMenu
+
form.show()
: The popup menu that shows when rightclicking the control.
+
</syntaxhighlight>
  
; Font &#58; Font
+
<syntaxhighlight lang="lua" line>
: The font class associated with the control.
+
local form = createForm()
 +
form.Caption = "Visibility Example"
  
; OnClick &#58; function
+
local button = createButton(form)
: The function to call when a button is pressed.
+
button.Parent = form
 +
button.Caption = "Disabled Button"
 +
button.Enabled = false
  
== Methods ==
+
print("Enabled: " .. tostring(button.getEnabled()))
 +
print("Visible: " .. tostring(button.getVisible()))
  
; getLeft() &#58; integer
+
form.show()
: Returns the left position.
+
</syntaxhighlight>
  
; setLeft(''value'')
+
<syntaxhighlight lang="lua" line>
: Sets the left position.
+
local form = createForm()
 +
form.Caption = "Font and Color Example"
  
; getTop() &#58; integer
+
local label = createLabel(form)
: Returns the top position.
+
label.Parent = form
 +
label.Caption = "Styled text"
 +
label.Left = 20
 +
label.Top = 20
 +
label.Color = 0xFFFFFF
 +
label.Font.Size = 14
 +
label.Font.Style = "[fsBold]"
  
; setTop(''value'')
+
form.show()
: Sets the top position.
+
</syntaxhighlight>
  
; getWidth() &#58; integer
+
<syntaxhighlight lang="lua" line>
: Returns the width.
+
local form = createForm()
 +
form.Caption = "Coordinate Example"
  
; setWidth(''value'')
+
local button = createButton(form)
: Sets the width.
+
button.Parent = form
 +
button.Caption = "Coordinates"
 +
button.Left = 50
 +
button.Top = 50
  
; getHeight() &#58; integer
+
local screenX, screenY = button.clientToScreen(0, 0)
: Returns the height.
+
local clientX, clientY = button.screenToClient(screenX, screenY)
  
; setHeight()
+
print("Screen: " .. tostring(screenX) .. ", " .. tostring(screenY))
: Sets the height.
+
print("Client: " .. tostring(clientX) .. ", " .. tostring(clientY))
  
; setCaption(''caption'')
+
form.show()
: Sets the text on a control. All the gui objects fall in this category.
+
</syntaxhighlight>
  
; getCaption() &#58; string
+
===Notes===
: Returns the text of the control
+
* Left is the horizontal x position.
 
+
* Top is the vertical y position.
; setPosition(''x'', ''y'')
+
* Position values are relative to the client area of the parent control.
: Sets the x and y position of the object base don the top left position (relative to the client array of the owner object)
+
* Many GUI classes inherit from Control and therefore share these properties and methods.
 
+
* For font changes, it is usually better to modify the existing Font object instead of replacing it with setFont().
; getPosition() &#58; (integer, integer)
 
: Returns the x and y position of the object (relative to the client array of the owner object)
 
 
 
; setSize(''width'', ''height'')
 
: Sets the width and height of the control
 
 
 
; getSize() &#58; (integer, integer)
 
: Gets the size of the control
 
 
 
; setAlign(''alignmentOption'')
 
: sets the alignment of the control.
 
 
 
; getAlign(''alignmentOption'')
 
: gets the alignment of the control.
 
 
 
; getEnabled() &#58; boolean
 
: gets the enabled state of the control.
 
 
 
; setEnabled(''state'')
 
: Sets the enabled state of the control.
 
 
 
; getVisible() &#58; boolean
 
: gets the visible state of the control.
 
 
 
; setVisible(''state'')
 
: sets the visible state of the control.
 
 
 
; getColor() &#58; integer
 
: gets the color.
 
 
 
; setColor(''rgb'')
 
: Sets the color.
 
 
 
; getParent() &#58; WinControl
 
: Returns nil or an object that inherits from the [[WinControl]] class.
 
 
 
; setParent(''winControl'')
 
: Sets the parent for this control.
 
 
 
; getPopupMenu() &#58; Menu
 
: Returns the pop up menu.
 
 
 
; setPopupMenu()
 
: Sets the pop up menu.
 
 
 
; getFont() &#58; Font
 
: Returns the Font object of this object.
 
 
 
; setFont()
 
: Assigns a new font object. (Not recommended to use. Change the font object that's already there if you wish to change fonts).
 
 
 
; repaint()
 
: Invalidates the graphical area of the control and forces and update.
 
 
 
; update()
 
: Only updates the invalidated areas.
 
 
 
; setOnClick(''functionNameOrString'')
 
: Sets the onclick routine.
 
 
 
; getOnClick() &#58; function
 
: Gets the onclick function.
 
 
 
; doClick()
 
: Executes the current function under onClick.
 
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}
  
=== Related Functions ===
+
{{Forms}}
* [[Lua:createClass|createClass]]
 
* [[Lua:inheritsFromObject|inheritsFromObject]]
 
* [[Lua:inheritsFromComponent|inheritsFromComponent]]
 
* [[Lua:inheritsFromControl|inheritsFromControl]]
 
* [[Lua:inheritsFromWinControl|inheritsFromWinControl]]
 
 
 
=== Related Classes ===
 
* [[Lua:Class:Object|Object]]
 
* [[Lua:Class:Control|Control]]
 
* [[Lua:Class:WinControl|WinControl]]
 
* [[Lua:Class:Application|Application]]
 
* [[Lua:Class:Form|Form]]
 

Latest revision as of 20:25, 25 June 2026

{} Class

class Control : Component

The Control class represents a visual GUI control.

A Control inherits from Component and Object. It provides common visual properties such as position, size, caption, visibility, color, font, parent control, and click handling.

Inheritance[edit]

Class Inherits From Description
Control Component Base class for visual controls.
Component Object Base class for components.

Properties[edit]

Property Type Description
Caption String The text of the control.
Top Integer The vertical position of the control relative to its parent.
Left Integer The horizontal position of the control relative to its parent.
Width Integer The width of the control.
Height Integer The height of the control.
ClientWidth Integer The usable width inside the control, excluding borders.
ClientHeight Integer The usable height inside the control, excluding borders.
Align AlignmentOption The alignment of the control.
Enabled Boolean Determines whether the control is usable or disabled.
Visible Boolean Determines whether the control is visible.
Color ColorDefinition or RGBInteger The color of the control. This does not affect the caption text.
RGBColor RGBInteger The color of the control in RGB format.
Parent WinControl The parent of this control.
PopupMenu PopupMenu The popup menu that is shown when right-clicking the control.
Font Font The Font object associated with the control.
OnClick Function The function to call when the control is clicked.
OnChangeBounds Function The function to call when the size or position of the control changes.

Event Properties[edit]

Event Function Signature Description
OnClick function(sender) Called when the control is clicked.
OnChangeBounds function(sender) Called when the size or position of the control changes.

Methods[edit]

Method Return Type Description
getLeft() Integer Returns the horizontal position of the control.
setLeft(integer) void Sets the horizontal position of the control.
getTop() Integer Returns the vertical position of the control.
setTop(integer) void Sets the vertical position of the control.
getWidth() Integer Returns the width of the control.
setWidth(integer) void Sets the width of the control.
getHeight() Integer Returns the height of the control.
setHeight(integer) void Sets the height of the control.
setCaption(caption) void Sets the text of the control.
getCaption() String Returns the text of the control.
setPosition(x, y) void Sets the x and y position of the control based on the top-left position, relative to the client area of the parent object.
getPosition() Integer, Integer Returns the x and y position of the control, relative to the client area of the parent object.
setSize(width, height) void Sets the width and height of the control.
getSize() Integer, Integer Returns the width and height of the control.
setAlign(alignmentoption) void Sets the alignment of the control.
getAlign() AlignmentOption Returns the alignment of the control.
getEnabled() Boolean Returns the enabled state of the control.
setEnabled(boolean) void Sets the enabled state of the control.
getVisible() Boolean Returns the visible state of the control.
setVisible(boolean) void Sets the visible state of the control.
getColor() ColorDefinition or RGBInteger Returns the color of the control.
setColor(rgb) void Sets the color of the control.
getParent() WinControl or nil Returns the parent object, or nil if no parent is assigned.
setParent(wincontrol) void Sets the parent of this control.
getPopupMenu() PopupMenu Returns the assigned popup menu.
setPopupMenu(popupmenu) void Sets the popup menu for this control.
getFont() Font Returns the Font object of this control.
setFont(font) void Assigns a new Font object. This is not recommended; usually, the existing Font object should be modified instead.
repaint() void Invalidates the graphical area of the control and forces an update.
refresh() void Updates the control. Usually causes a repaint.
update() void Only updates invalidated areas.
setOnClick(functionnameorstring) void Sets the OnClick routine.
getOnClick() Function Returns the current OnClick function.
doClick() void Executes the current OnClick function.
bringToFront() void Changes the z-order of the control so it is at the front.
sendToBack() void Changes the z-order of the control so it is at the back.
screenToClient(x, y) Integer, Integer Converts screen coordinates to coordinates relative to the control.
clientToScreen(x, y) Integer, Integer Converts coordinates relative to the control to screen coordinates.

Examples[edit]

 1 local form = createForm()
 2 form.Caption = "Control Example"
 3 form.Width = 400
 4 form.Height = 250
 5 
 6 local button = createButton(form)
 7 button.Parent = form
 8 button.Caption = "Click me"
 9 button.Left = 20
10 button.Top = 20
11 button.Width = 120
12 button.Height = 30
13 
14 button.OnClick = function(sender)
15   print(sender.Caption .. " clicked")
16 end
17 
18 form.show()
 1 local form = createForm()
 2 form.Caption = "Position and Size Example"
 3 
 4 local label = createLabel(form)
 5 label.Parent = form
 6 label.Caption = "Hello"
 7 label.setPosition(20, 30)
 8 label.setSize(200, 24)
 9 
10 local x, y = label.getPosition()
11 local w, h = label.getSize()
12 
13 print("Position: " .. tostring(x) .. ", " .. tostring(y))
14 print("Size: " .. tostring(w) .. " x " .. tostring(h))
15 
16 form.show()
 1 local form = createForm()
 2 form.Caption = "Visibility Example"
 3 
 4 local button = createButton(form)
 5 button.Parent = form
 6 button.Caption = "Disabled Button"
 7 button.Enabled = false
 8 
 9 print("Enabled: " .. tostring(button.getEnabled()))
10 print("Visible: " .. tostring(button.getVisible()))
11 
12 form.show()
 1 local form = createForm()
 2 form.Caption = "Font and Color Example"
 3 
 4 local label = createLabel(form)
 5 label.Parent = form
 6 label.Caption = "Styled text"
 7 label.Left = 20
 8 label.Top = 20
 9 label.Color = 0xFFFFFF
10 label.Font.Size = 14
11 label.Font.Style = "[fsBold]"
12 
13 form.show()
 1 local form = createForm()
 2 form.Caption = "Coordinate Example"
 3 
 4 local button = createButton(form)
 5 button.Parent = form
 6 button.Caption = "Coordinates"
 7 button.Left = 50
 8 button.Top = 50
 9 
10 local screenX, screenY = button.clientToScreen(0, 0)
11 local clientX, clientY = button.screenToClient(screenX, screenY)
12 
13 print("Screen: " .. tostring(screenX) .. ", " .. tostring(screenY))
14 print("Client: " .. tostring(clientX) .. ", " .. tostring(clientY))
15 
16 form.show()

Notes[edit]

  • Left is the horizontal x position.
  • Top is the vertical y position.
  • Position values are relative to the client area of the parent control.
  • Many GUI classes inherit from Control and therefore share these properties and methods.
  • For font changes, it is usually better to modify the existing Font object instead of replacing it with setFont().

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Form Related Classes