Difference between revisions of "Lua:Class:Control"
Jump to navigation
Jump to search
(Syntax Highlighting.) |
|||
| (16 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
| − | ''' | + | [[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. | ||
| + | |} | ||
| − | + | ===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=== | ||
| + | <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) | ||
| − | + | 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() | |
| − | + | </syntaxhighlight> | |
| + | <syntaxhighlight lang="lua" line> | ||
| + | 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() | |
| − | + | </syntaxhighlight> | |
| + | <syntaxhighlight lang="lua" line> | ||
| + | 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() | ||
| + | </syntaxhighlight> | ||
| − | + | <syntaxhighlight lang="lua" line> | |
| − | + | local form = createForm() | |
| − | + | form.Caption = "Coordinate Example" | |
| − | + | local button = createButton(form) | |
| − | + | button.Parent = form | |
| − | local | + | button.Caption = "Coordinates" |
| − | + | button.Left = 50 | |
| + | button.Top = 50 | ||
| − | + | local screenX, screenY = button.clientToScreen(0, 0) | |
| − | + | local clientX, clientY = button.screenToClient(screenX, screenY) | |
| − | local | ||
| − | |||
| − | + | print("Screen: " .. tostring(screenX) .. ", " .. tostring(screenY)) | |
| − | + | print("Client: " .. tostring(clientX) .. ", " .. tostring(clientY)) | |
| − | |||
| − | print(" | ||
| − | + | 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}} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | {{Forms}} | |
| − | |||
| − | |||
Latest revision as of 20:25, 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[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().