Difference between revisions of "Lua:Class:Control"
Jump to navigation
Jump to search
(Undo revision 5662 by TheyCallMeTim13 (Talk)) |
(Major overhaul of the post.) |
||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | + | {{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=== |
| − | + | {|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 | + | ===Properties=== |
| − | + | {|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. | ||
| + | |} | ||
| − | ; | + | ===Methods=== |
| − | + | {|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=== | |
| − | + | <pre> | |
| + | 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() | |
| − | + | </pre> | |
| − | + | <pre> | |
| − | + | 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) | ||
| − | + | local x, y = label.getPosition() | |
| − | + | local w, h = label.getSize() | |
| − | + | print("Position: " .. tostring(x) .. ", " .. tostring(y)) | |
| − | : | + | print("Size: " .. tostring(w) .. " x " .. tostring(h)) |
| − | + | form.show() | |
| − | + | </pre> | |
| − | + | <pre> | |
| − | + | local form = createForm() | |
| + | form.Caption = "Visibility Example" | ||
| − | + | local button = createButton(form) | |
| − | + | button.Parent = form | |
| + | button.Caption = "Disabled Button" | ||
| + | button.Enabled = false | ||
| − | + | print("Enabled: " .. tostring(button.getEnabled())) | |
| + | print("Visible: " .. tostring(button.getVisible())) | ||
| − | + | form.show() | |
| − | + | </pre> | |
| − | + | <pre> | |
| − | + | local form = createForm() | |
| + | form.Caption = "Font and Color Example" | ||
| − | + | local label = createLabel(form) | |
| − | + | 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() | |
| − | + | </pre> | |
| − | + | <pre> | |
| − | + | local form = createForm() | |
| + | form.Caption = "Coordinate Example" | ||
| − | + | local button = createButton(form) | |
| − | + | button.Parent = form | |
| + | 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)) |
| − | + | form.show() | |
| − | + | </pre> | |
| − | + | ===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}} | {{LuaSeeAlso}} | ||
| − | + | {{Forms}} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Revision as of 00:32, 25 June 2026
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
| Class | Inherits From | Description |
|---|---|---|
| Control | Component | Base class for visual controls. |
| Component | Object | Base class for components. |
Properties
| 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
| 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
| 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
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()
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)
local x, y = label.getPosition()
local w, h = label.getSize()
print("Position: " .. tostring(x) .. ", " .. tostring(y))
print("Size: " .. tostring(w) .. " x " .. tostring(h))
form.show()
local form = createForm()
form.Caption = "Visibility Example"
local button = createButton(form)
button.Parent = form
button.Caption = "Disabled Button"
button.Enabled = false
print("Enabled: " .. tostring(button.getEnabled()))
print("Visible: " .. tostring(button.getVisible()))
form.show()
local form = createForm() form.Caption = "Font and Color Example" local label = createLabel(form) 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()
local form = createForm()
form.Caption = "Coordinate Example"
local button = createButton(form)
button.Parent = form
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))
form.show()
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().