Difference between revisions of "Lua:Class:Control"

From Cheat Engine
Jump to navigation Jump to search
(Syntax Highlighting.)
 
(16 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''Control Class''' (Inheritance Component->Object)
+
[[Category:Lua]]
'''control_setCaption'''(control, caption) 
+
{{Class|'''class''' Control ''':''' Component}}
  Sets the text on a control. All the gui objects fall in this category
 
  
 +
The Control class represents a visual GUI control.
  
'''control_getCaption'''(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.
  Returns the text of the control
 
  
 +
===Inheritance===
 +
{|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.
 +
|}
  
'''control_setPosition'''(control, x,y)
+
===Properties===
  Sets the x and y position of the object base don the top left position (relative to the client array of the owner object)
+
{|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.
 +
|}
  
 +
===Event Properties===
 +
{|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.
 +
|}
  
'''control_getPosition'''(contron)  
+
===Methods===
  Returns the x and y position of the object (relative to the client array of the owner object)
+
{|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.
 +
|}
  
 +
===Examples===
 +
<syntaxhighlight lang="lua" line>
 +
local form = createForm()
 +
form.Caption = "Control Example"
 +
form.Width = 400
 +
form.Height = 250
  
'''control_setSize'''(control, width,height)
+
local button = createButton(form)
  Sets the width and height of the control
+
button.Parent = form
 +
button.Caption = "Click me"
 +
button.Left = 20
 +
button.Top = 20
 +
button.Width = 120
 +
button.Height = 30
  
 +
button.OnClick = function(sender)
 +
  print(sender.Caption .. " clicked")
 +
end
  
'''control_getSize'''(control)
+
form.show()
  Sets the size of the control
+
</syntaxhighlight>
 
 
 
 
'''control_setAlign'''(control, alignmentoption)
 
  Sets the alignment of the control
 
 
 
 
 
'''control_getAlign'''(control, alignmentoption)
 
  Gets the alignment of the control
 
 
 
 
 
'''control_getEnabled'''(control) 
 
  Gets the enabled state of the control
 
 
 
 
 
'''control_setEnabled'''(control, boolean) 
 
  Sets the enabled state of the control
 
 
 
 
 
'''control_getVisible'''(control)
 
  Gets the visible state of the control
 
 
 
 
 
'''control_setVisible'''(control, boolean)
 
  Sets the visible state of the control
 
 
 
  
'''control_getColor'''(control)  
+
<syntaxhighlight lang="lua" line>
  Gets the color
+
local form = createForm()
 +
form.Caption = "Position and Size Example"
  
 +
local label = createLabel(form)
 +
label.Parent = form
 +
label.Caption = "Hello"
 +
label.setPosition(20, 30)
 +
label.setSize(200, 24)
  
'''control_setColor'''(control, rgb)  
+
local x, y = label.getPosition()
  Sets the color
+
local w, h = label.getSize()
  
 +
print("Position: " .. tostring(x) .. ", " .. tostring(y))
 +
print("Size: " .. tostring(w) .. " x " .. tostring(h))
  
'''control_getParent'''(control)  
+
form.show()
  Returns nil or an object that inherits from the Wincontrol class
+
</syntaxhighlight>
  
 +
<syntaxhighlight lang="lua" line>
 +
local form = createForm()
 +
form.Caption = "Visibility Example"
  
'''control_setParent'''(control)  
+
local button = createButton(form)
  Sets the parent for this control
+
button.Parent = form
 +
button.Caption = "Disabled Button"
 +
button.Enabled = false
  
 +
print("Enabled: " .. tostring(button.getEnabled()))
 +
print("Visible: " .. tostring(button.getVisible()))
  
'''control_getPopupMenu'''(control)
+
form.show()
  Gets the assigned popup menu to this control
+
</syntaxhighlight>
  
 +
<syntaxhighlight lang="lua" line>
 +
local form = createForm()
 +
form.Caption = "Font and Color Example"
  
'''control_setPopupMenu('''control)
+
local label = createLabel(form)
  Sets the popup menu for this control
+
label.Parent = form
 +
label.Caption = "Styled text"
 +
label.Left = 20
 +
label.Top = 20
 +
label.Color = 0xFFFFFF
 +
label.Font.Size = 14
 +
label.Font.Style = "[fsBold]"
  
 +
form.show()
 +
</syntaxhighlight>
  
'''control_onClick'''(control, functionnameorstring)
+
<syntaxhighlight lang="lua" line>
  Sets the onclick routine
+
local form = createForm()
  function (sender)
+
form.Caption = "Coordinate Example"
  
<pre>
+
local button = createButton(form)
--Example:
+
button.Parent = form
local forms = createForm()
+
button.Caption = "Coordinates"
local labels = createLabel(forms)
+
button.Left = 50
 +
button.Top = 50
  
--Caption
+
local screenX, screenY = button.clientToScreen(0, 0)
control_setCaption(labels, "My first label")
+
local clientX, clientY = button.screenToClient(screenX, screenY)
local variable = control_getCaption(labels)
 
print(variable)
 
  
--Position
+
print("Screen: " .. tostring(screenX) .. ", " .. tostring(screenY))
control_setPosition(labels,30,30)
+
print("Client: " .. tostring(clientX) .. ", " .. tostring(clientY))
x,y=control_getPosition(labels)
 
print("X coord of label is "..x.."; Y coord of label is "..y)
 
  
--Enabled?
+
form.show()
local editbox1 = createEdit(forms)
+
</syntaxhighlight>
local editbox2 = createEdit(forms)
 
control_setEnabled(editbox1,false)
 
if control_getEnabled(editbox1) then
 
  
  else
+
===Notes===
  print "editbox1 not enabled"
+
* Left is the horizontal x position.
  control_setCaption(editbox1,"editbox1")
+
* Top is the vertical y position.
end
+
* 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().
  
control_setEnabled(editbox2,true)
+
{{LuaSeeAlso}}
  if control_getEnabled(editbox2) then
 
  print "editbox2  enabled"
 
  control_setCaption(editbox2,"editbox2")
 
end
 
  
control_setPosition(editbox1,30,60)
+
{{Forms}}
control_setPosition(editbox2,30,90)
 
</pre>
 

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