Difference between revisions of "Lua:Class:Control"

From Cheat Engine
Jump to navigation Jump to search
(Syntax Highlighting.)
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Control Class''': (Inheritance: [[Component]]->[[Object]])
+
[[Category:Lua]]
 +
{{Class|'''class''' Control ''':''' Component}}
  
Base class for gui controls
+
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===
 +
{|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.
 +
|}
  
 
===Properties===
 
===Properties===
  Caption: string - The text of a control
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
  Top : integer - The x position
+
!align="left"|Property
  Left : integer - The y position
+
!align="left"|Type
  Width : integer - The width of the control
+
!style="width: 80%;background-color:white;" align="left"|Description
  Height : integer - The height of the control
+
|-
  ClientWidth: integer - The usable width inside the control (minus the borders)
+
|Caption
  ClientHeight: integer - The usable height the control (minus the borders)
+
|String
  Align: AlignmentOption - Alignment of the control
+
|The text of the control.
  Enabled: boolean - Determines if the object is usable or greyed out
+
|-
  Visible: boolean - Determines if the object is visible or not
+
|Top
  Color: ColorDefinition/RGBInteger - The color of the object. Does not affect the caption
+
|Integer
  Parent: WinControl - The owner of this control
+
|The vertical position of the control relative to its parent.
  PopupMenu: PopupMenu - The popup menu that shows when rightclicking the control
+
|-
  Font: Font - The font class associated with the control
+
|Left
  OnClick: function - The function to call when a button is pressed
+
|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.
 +
|}
  
 
===Methods===
 
===Methods===
  getLeft()
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
  setLeft(integer)
+
!align="left"|Method
  getTop()
+
!align="left"|Return Type
  setTop(integer)
+
!style="width: 80%;background-color:white;" align="left"|Description
  getWidth()
+
|-
  setWidth(integer)
+
|getLeft()
  getHeight()
+
|Integer
  setHeight()
+
|Returns the horizontal position of the control.
  setCaption(caption) : sets the text on a control. All the gui objects fall in this category
+
|-
  getCaption() : Returns the text of the control
+
|setLeft(integer)
  setPosition(x,y): sets the x and y position of the object base don the top left position (relative to the client array of the owner object)
+
|void
  getPosition(): returns the x and y position of the object (relative to the client array of the owner object)
+
|Sets the horizontal position of the control.
  setSize(width,height) : Sets the width and height of the control
+
|-
  getSize() : Gets the size of the control
+
|getTop()
  setAlign(alignmentoption): sets the alignment of the control
+
|Integer
  getAlign(alignmentoption): gets the alignment of the control
+
|Returns the vertical position of the control.
  getEnabled() : gets the enabled state of the control
+
|-
  setEnabled(boolean) : Sets the enabled state of the control
+
|setTop(integer)
  getVisible() : gets the visible state of the control
+
|void
  setVisible(boolean) : sets the visible state of the control
+
|Sets the vertical position of the control.
  getColor() : gets the color
+
|-
  setColor(rgb) : Sets the color
+
|getWidth()
  getParent() : Returns nil or an object that inherits from the Wincontrol class
+
|Integer
  setParent(wincontrol) : Sets the parent for this control
+
|Returns the width of the control.
  getPopupMenu()
+
|-
  setPopupMenu()
+
|setWidth(integer)
  getFont()Returns the Font object of this object
+
|void
  setFont()Assigns a new font object. (Not recommended to use. Change the font object that's already there if you wish to change fonts)
+
|Sets the width of the control.
  repaint(): Invalidates the graphical area of the control and forces and update
+
|-
  update() : Only updates the invalidated areas
+
|getHeight()
  setOnClick(functionnameorstring) : Sets the onclick routine
+
|Integer
  getOnClick(): Gets the onclick function
+
|Returns the height of the control.
  doClick()Executes the current function under onClick
+
|-
 +
|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
 +
 
 +
local button = createButton(form)
 +
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
  
 +
form.show()
 +
</syntaxhighlight>
  
 +
<syntaxhighlight lang="lua" line>
 +
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)
  
<pre>
+
local x, y = label.getPosition()
--Example:
+
local w, h = label.getSize()
  
function clickroutine(sender) --  onClick()
+
print("Position: " .. tostring(x) .. ", " .. tostring(y))
print "editbox2 clicked"
+
print("Size: " .. tostring(w) .. " x " .. tostring(h))
end
 
  
local forms = createForm()
+
form.show()
local labels = createLabel(forms)
+
</syntaxhighlight>
  
--Caption
+
<syntaxhighlight lang="lua" line>
labels.Caption="My first label"
+
local form = createForm()
local variable = labels.Caption
+
form.Caption = "Visibility Example"
print(variable)
 
  
--Position
+
local button = createButton(form)
labels.setPosition(30,30)
+
button.Parent = form
x,y=labels.getPosition()
+
button.Caption = "Disabled Button"
print("X coord of label is "..x.."; Y coord of label is "..y)
+
button.Enabled = false
  
--Enabled?
+
print("Enabled: " .. tostring(button.getEnabled()))
local editbox1 = createEdit(forms)
+
print("Visible: " .. tostring(button.getVisible()))
local editbox2 = createEdit(forms)
 
editbox1.Enabled=false
 
if editbox1.Enabled then
 
  
  else
+
form.show()
  print "editbox1 not enabled"
+
</syntaxhighlight>
  editbox1.Caption="editbox1"
 
end
 
  
editbox2.Enabled=true
+
<syntaxhighlight lang="lua" line>
  if editbox2.Enabled then
+
local form = createForm()
  print "editbox2  enabled"
+
form.Caption = "Font and Color Example"
  editbox2.Caption="editbox2"
 
end
 
  
editbox1.setPosition(30,60)
+
local label = createLabel(form)
editbox2.setPosition(30,90)
+
label.Parent = form
 +
label.Caption = "Styled text"
 +
label.Left = 20
 +
label.Top = 20
 +
label.Color = 0xFFFFFF
 +
label.Font.Size = 14
 +
label.Font.Style = "[fsBold]"
  
--Visibility
+
form.show()
local editbox3 = createEdit(forms)
+
</syntaxhighlight>
editbox3.Visible=true  -- If false editbox3 would not be displayed on the form.
 
if editbox3.Visible then
 
  editbox3.Caption="editbox3"  -- Sets the caption only when visible
 
  editbox3.setPosition(editbox3,30,120)
 
end
 
  
--Color
+
<syntaxhighlight lang="lua" line>
labels.Color=123456  -- color code in hex (range 000000 to FFFFFF)
+
local form = createForm()
local colorCode = labels.Color
+
form.Caption = "Coordinate Example"
print(colorCode)
 
  
--Onclick
+
local button = createButton(form)
editbox2.OnClick=clickroutine -- clickroutine must have exactly one argument
+
button.Parent = form
--alternatively: editbox2.OnClick="clickroutine" --the function clickroutine will then be searched in _G when the click event triggers
+
button.Caption = "Coordinates"
 +
button.Left = 50
 +
button.Top = 50
  
 +
local screenX, screenY = button.clientToScreen(0, 0)
 +
local clientX, clientY = button.screenToClient(screenX, screenY)
  
 +
print("Screen: " .. tostring(screenX) .. ", " .. tostring(screenY))
 +
print("Client: " .. tostring(clientX) .. ", " .. tostring(clientY))
  
</pre>
+
form.show()
 +
</syntaxhighlight>
  
 +
===Notes===
 +
* 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().
  
----
+
{{LuaSeeAlso}}
  
* [[Lua|Lua Functions and Classes]]
+
{{Forms}}

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