Difference between revisions of "Lua:Class:WinControl"
Jump to navigation
Jump to search
m (moved WinControl to Lua:Class:WinControl) |
(Major overhaul of the post.) |
||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | + | {{Class|'''class''' WinControl ''':''' Control}} | |
| − | Base class for | + | The WinControl class represents a visual control that can own child controls and receive keyboard focus. |
| + | |||
| + | WinControl inherits from [[Lua:Class:Control|Control]], [[Lua:Class:Component|Component]], and [[Lua:Class:Object|Object]]. It is commonly used as a base class for forms, panels, group boxes, list boxes, edit controls, and other windowed controls. | ||
| + | |||
| + | ===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 | ||
| + | |- | ||
| + | |WinControl | ||
| + | |Control | ||
| + | |Base class for controls that have a window handle, can contain child controls, and can receive focus. | ||
| + | |- | ||
| + | |Control | ||
| + | |Component | ||
| + | |Base class for visual controls. | ||
| + | |- | ||
| + | |Component | ||
| + | |Object | ||
| + | |Base class for owned components. | ||
| + | |- | ||
| + | |Object | ||
| + | |None | ||
| + | |Base class for Lua-exposed objects. | ||
| + | |} | ||
== Properties == | == Properties == | ||
| − | ; DoubleBuffered | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" |
| − | + | !align="left"|Property | |
| − | : | + | !align="left"|Type |
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |Handle | ||
| + | |Integer | ||
| + | |The internal Windows handle of the control. | ||
| + | |- | ||
| + | |DoubleBuffered | ||
| + | |Boolean | ||
| + | |When enabled, graphical updates are first drawn to an offscreen bitmap and then shown on screen. This may reduce flickering. | ||
| + | |- | ||
| + | |ControlCount | ||
| + | |Integer | ||
| + | |The number of child controls attached to this WinControl. | ||
| + | |- | ||
| + | |Control[index] | ||
| + | |[[Lua:Class:Control|Control]] | ||
| + | |Array-style access to a child control. | ||
| + | |- | ||
| + | |OnEnter | ||
| + | |Function | ||
| + | |Function to call when the WinControl gains focus. | ||
| + | |- | ||
| + | |OnExit | ||
| + | |Function | ||
| + | |Function to call when the WinControl loses focus. | ||
| + | |} | ||
| + | |||
| + | == 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 | ||
| + | |- | ||
| + | |getControlCount() | ||
| + | |Integer | ||
| + | |Returns the number of controls attached to this WinControl. | ||
| + | |- | ||
| + | |getControl(index) | ||
| + | |[[Lua:Class:Control|Control]] | ||
| + | |Returns the child control at the specified index. | ||
| + | |- | ||
| + | |getControlAtPos(x, y) | ||
| + | |[[Lua:Class:Control|Control]] | ||
| + | |Returns the child control at the given x and y position relative to the WinControl. | ||
| + | |- | ||
| + | |canFocus() | ||
| + | |Boolean | ||
| + | |Returns true if the control can receive keyboard focus. | ||
| + | |- | ||
| + | |focused() | ||
| + | |Boolean | ||
| + | |Returns true if the control currently has focus. | ||
| + | |- | ||
| + | |setFocus() | ||
| + | |void | ||
| + | |Tries to set keyboard focus to the control. | ||
| + | |- | ||
| + | |setShape(region) | ||
| + | |void | ||
| + | |Sets the specified [[Lua:Class:Region|Region]] object as the new shape for this WinControl. | ||
| + | |- | ||
| + | |setShape(bitmap) | ||
| + | |void | ||
| + | |Sets the specified [[Lua:Class:Bitmap|Bitmap]] object as the new shape for this WinControl. | ||
| + | |- | ||
| + | |setOnEnter(function) | ||
| + | |void | ||
| + | |Sets the OnEnter event handler. The event is triggered when the WinControl gains focus. | ||
| + | |- | ||
| + | |getOnEnter() | ||
| + | |Function | ||
| + | |Returns the current OnEnter event handler. | ||
| + | |- | ||
| + | |setOnExit(function) | ||
| + | |void | ||
| + | |Sets the OnExit event handler. The event is triggered when the WinControl loses focus. | ||
| + | |- | ||
| + | |getOnExit() | ||
| + | |Function | ||
| + | |Returns the current OnExit event handler. | ||
| + | |- | ||
| + | |setLayeredAttributes(key, alpha, flags) | ||
| + | |void | ||
| + | |Sets layered window attributes for the control if possible. Flags can be a combination of <code>LWA_ALPHA</code> and/or <code>LWA_COLORKEY</code>. | ||
| + | |} | ||
| + | |||
| + | == Method Parameters == | ||
| + | |||
| + | ===getControl=== | ||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |index | ||
| + | |Integer | ||
| + | |The zero-based index of the child control. | ||
| + | |} | ||
| + | |||
| + | ===getControlAtPos=== | ||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |x | ||
| + | |Integer | ||
| + | |The x coordinate relative to the WinControl. | ||
| + | |- | ||
| + | |y | ||
| + | |Integer | ||
| + | |The y coordinate relative to the WinControl. | ||
| + | |} | ||
| + | |||
| + | ===setShape=== | ||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |region | ||
| + | |[[Lua:Class:Region|Region]] | ||
| + | |The region object to use as the new shape. | ||
| + | |- | ||
| + | |bitmap | ||
| + | |[[Lua:Class:Bitmap|Bitmap]] | ||
| + | |The bitmap object to use as the new shape. | ||
| + | |} | ||
| + | |||
| + | ===setOnEnter / setOnExit=== | ||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |function | ||
| + | |Function | ||
| + | |The event handler function to assign. | ||
| + | |} | ||
| + | |||
| + | ===setLayeredAttributes=== | ||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |key | ||
| + | |Integer | ||
| + | |The color key value used when <code>LWA_COLORKEY</code> is included in the flags. | ||
| + | |- | ||
| + | |alpha | ||
| + | |Integer | ||
| + | |The alpha value used when <code>LWA_ALPHA</code> is included in the flags. | ||
| + | |- | ||
| + | |flags | ||
| + | |Integer | ||
| + | |A combination of layered attribute flags, such as <code>LWA_ALPHA</code> and/or <code>LWA_COLORKEY</code>. | ||
| + | |} | ||
| + | |||
| + | == Notes == | ||
| + | |||
| + | <code>setLayeredAttributes</code> depends on operating system support. On Windows 7 and earlier, only forms are supported. | ||
| + | |||
| + | == Examples == | ||
| + | |||
| + | ===Get the handle of a WinControl=== | ||
| + | <syntaxhighlight lang="lua" line highlight="4"> | ||
| + | local form = createForm(false) | ||
| + | local button = createButton(form) | ||
| + | |||
| + | print("Button handle: " .. tostring(button.Handle)) | ||
| + | |||
| + | form.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Enable DoubleBuffered on a panel=== | ||
| + | <syntaxhighlight lang="lua" line highlight="4"> | ||
| + | local form = createForm(false) | ||
| + | local panel = createPanel(form) | ||
| + | |||
| + | panel.DoubleBuffered = true | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Count child controls=== | ||
| + | <syntaxhighlight lang="lua" line highlight="5,7"> | ||
| + | local form = createForm(false) | ||
| + | createButton(form) | ||
| + | createEdit(form) | ||
| + | |||
| + | local count = form.ControlCount | ||
| + | |||
| + | print("Child controls: " .. tostring(count)) | ||
| + | |||
| + | form.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Access child controls by index=== | ||
| + | <syntaxhighlight lang="lua" line highlight="6"> | ||
| + | local form = createForm(false) | ||
| + | createButton(form) | ||
| + | createEdit(form) | ||
| + | |||
| + | for i = 0, form.ControlCount - 1 do | ||
| + | local control = form.Control[i] | ||
| + | print(control.ClassName) | ||
| + | end | ||
| + | |||
| + | form.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Use getControlCount and getControl=== | ||
| + | <syntaxhighlight lang="lua" line highlight="5,8"> | ||
| + | local form = createForm(false) | ||
| + | createButton(form) | ||
| + | createEdit(form) | ||
| + | |||
| + | local count = form.getControlCount() | ||
| + | |||
| + | for i = 0, count - 1 do | ||
| + | local control = form.getControl(i) | ||
| + | print(control.ClassName) | ||
| + | end | ||
| + | |||
| + | form.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Get the control at a relative position=== | ||
| + | <syntaxhighlight lang="lua" line highlight="6"> | ||
| + | local form = createForm(false) | ||
| + | local button = createButton(form) | ||
| + | button.Left = 10 | ||
| + | button.Top = 10 | ||
| + | |||
| + | local control = form.getControlAtPos(15, 15) | ||
| + | |||
| + | if control ~= nil then | ||
| + | print("Control at position: " .. control.ClassName) | ||
| + | end | ||
| + | |||
| + | form.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Check whether a control can receive focus=== | ||
| + | <syntaxhighlight lang="lua" line highlight="4"> | ||
| + | local form = createForm(false) | ||
| + | local edit = createEdit(form) | ||
| + | |||
| + | if edit.canFocus() then | ||
| + | print("Edit can receive focus") | ||
| + | end | ||
| − | + | form.destroy() | |
| − | + | </syntaxhighlight> | |
| − | + | ===Set keyboard focus=== | |
| − | + | <syntaxhighlight lang="lua" line highlight="6,7"> | |
| + | local form = createForm(false) | ||
| + | local edit = createEdit(form) | ||
| − | + | form.show() | |
| − | |||
| − | + | if edit.canFocus() then | |
| − | + | edit.setFocus() | |
| + | end | ||
| + | </syntaxhighlight> | ||
| − | == | + | ===Check whether a control is focused=== |
| + | <syntaxhighlight lang="lua" line highlight="4"> | ||
| + | local form = createForm(false) | ||
| + | local edit = createEdit(form) | ||
| + | |||
| + | print("Focused: " .. tostring(edit.focused())) | ||
| + | |||
| + | form.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Assign OnEnter and OnExit events through properties=== | ||
| + | <syntaxhighlight lang="lua" line highlight="4,8"> | ||
| + | local form = createForm(false) | ||
| + | local edit = createEdit(form) | ||
| + | |||
| + | edit.OnEnter = function(sender) | ||
| + | print("Focus entered") | ||
| + | end | ||
| + | |||
| + | edit.OnExit = function(sender) | ||
| + | print("Focus lost") | ||
| + | end | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| − | + | ===Assign OnEnter and OnExit events through methods=== | |
| − | + | <syntaxhighlight lang="lua" line highlight="4,8"> | |
| + | local form = createForm(false) | ||
| + | local edit = createEdit(form) | ||
| − | + | edit.setOnEnter(function(sender) | |
| − | + | print("Focus entered") | |
| + | end) | ||
| − | + | edit.setOnExit(function(sender) | |
| − | + | print("Focus lost") | |
| + | end) | ||
| − | + | form.show() | |
| − | + | </syntaxhighlight> | |
| − | + | ===Read assigned focus event handlers=== | |
| − | + | <syntaxhighlight lang="lua" line highlight="6,7"> | |
| + | local form = createForm(false) | ||
| + | local edit = createEdit(form) | ||
| − | + | edit.OnEnter = function(sender) print("Enter") end | |
| − | |||
| − | + | local onEnter = edit.getOnEnter() | |
| − | + | local onExit = edit.getOnExit() | |
| − | + | print("OnEnter assigned: " .. tostring(onEnter ~= nil)) | |
| − | : | + | print("OnExit assigned: " .. tostring(onExit ~= nil)) |
| − | + | form.destroy() | |
| − | + | </syntaxhighlight> | |
| − | + | ===Set a Region as the shape=== | |
| − | + | <syntaxhighlight lang="lua" line highlight="2,5"> | |
| + | local form = createForm(false) | ||
| + | local region = createRegion() | ||
| − | + | form.Width = 300 | |
| − | + | form.setShape(region) | |
| − | + | form.show() | |
| − | + | </syntaxhighlight> | |
| − | == | + | ===Set layered alpha attributes=== |
| − | + | <syntaxhighlight lang="lua" line highlight="6"> | |
| − | + | local form = createForm(false) | |
| + | form.Width = 300 | ||
| + | form.Height = 200 | ||
| + | form.show() | ||
| − | + | form.setLayeredAttributes(0, 180, LWA_ALPHA) | |
| − | + | </syntaxhighlight> | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | {{LuaSeeAlso}} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 01:02, 27 June 2026
The WinControl class represents a visual control that can own child controls and receive keyboard focus.
WinControl inherits from Control, Component, and Object. It is commonly used as a base class for forms, panels, group boxes, list boxes, edit controls, and other windowed controls.
Contents
- 1 Inheritance
- 2 Properties
- 3 Methods
- 4 Method Parameters
- 5 Notes
- 6 Examples
- 6.1 Get the handle of a WinControl
- 6.2 Enable DoubleBuffered on a panel
- 6.3 Count child controls
- 6.4 Access child controls by index
- 6.5 Use getControlCount and getControl
- 6.6 Get the control at a relative position
- 6.7 Check whether a control can receive focus
- 6.8 Set keyboard focus
- 6.9 Check whether a control is focused
- 6.10 Assign OnEnter and OnExit events through properties
- 6.11 Assign OnEnter and OnExit events through methods
- 6.12 Read assigned focus event handlers
- 6.13 Set a Region as the shape
- 6.14 Set layered alpha attributes
Inheritance[edit]
| Class | Inherits From | Description |
|---|---|---|
| WinControl | Control | Base class for controls that have a window handle, can contain child controls, and can receive focus. |
| Control | Component | Base class for visual controls. |
| Component | Object | Base class for owned components. |
| Object | None | Base class for Lua-exposed objects. |
Properties[edit]
| Property | Type | Description |
|---|---|---|
| Handle | Integer | The internal Windows handle of the control. |
| DoubleBuffered | Boolean | When enabled, graphical updates are first drawn to an offscreen bitmap and then shown on screen. This may reduce flickering. |
| ControlCount | Integer | The number of child controls attached to this WinControl. |
| Control[index] | Control | Array-style access to a child control. |
| OnEnter | Function | Function to call when the WinControl gains focus. |
| OnExit | Function | Function to call when the WinControl loses focus. |
Methods[edit]
| Method | Return Type | Description |
|---|---|---|
| getControlCount() | Integer | Returns the number of controls attached to this WinControl. |
| getControl(index) | Control | Returns the child control at the specified index. |
| getControlAtPos(x, y) | Control | Returns the child control at the given x and y position relative to the WinControl. |
| canFocus() | Boolean | Returns true if the control can receive keyboard focus. |
| focused() | Boolean | Returns true if the control currently has focus. |
| setFocus() | void | Tries to set keyboard focus to the control. |
| setShape(region) | void | Sets the specified Region object as the new shape for this WinControl. |
| setShape(bitmap) | void | Sets the specified Bitmap object as the new shape for this WinControl. |
| setOnEnter(function) | void | Sets the OnEnter event handler. The event is triggered when the WinControl gains focus. |
| getOnEnter() | Function | Returns the current OnEnter event handler. |
| setOnExit(function) | void | Sets the OnExit event handler. The event is triggered when the WinControl loses focus. |
| getOnExit() | Function | Returns the current OnExit event handler. |
| setLayeredAttributes(key, alpha, flags) | void | Sets layered window attributes for the control if possible. Flags can be a combination of LWA_ALPHA and/or LWA_COLORKEY.
|
Method Parameters[edit]
getControl[edit]
| Parameter | Type | Description |
|---|---|---|
| index | Integer | The zero-based index of the child control. |
getControlAtPos[edit]
| Parameter | Type | Description |
|---|---|---|
| x | Integer | The x coordinate relative to the WinControl. |
| y | Integer | The y coordinate relative to the WinControl. |
setShape[edit]
| Parameter | Type | Description |
|---|---|---|
| region | Region | The region object to use as the new shape. |
| bitmap | Bitmap | The bitmap object to use as the new shape. |
setOnEnter / setOnExit[edit]
| Parameter | Type | Description |
|---|---|---|
| function | Function | The event handler function to assign. |
setLayeredAttributes[edit]
| Parameter | Type | Description |
|---|---|---|
| key | Integer | The color key value used when LWA_COLORKEY is included in the flags.
|
| alpha | Integer | The alpha value used when LWA_ALPHA is included in the flags.
|
| flags | Integer | A combination of layered attribute flags, such as LWA_ALPHA and/or LWA_COLORKEY.
|
Notes[edit]
setLayeredAttributes depends on operating system support. On Windows 7 and earlier, only forms are supported.
Examples[edit]
Get the handle of a WinControl[edit]
1 local form = createForm(false)
2 local button = createButton(form)
3
4 print("Button handle: " .. tostring(button.Handle))
5
6 form.destroy()
Enable DoubleBuffered on a panel[edit]
1 local form = createForm(false)
2 local panel = createPanel(form)
3
4 panel.DoubleBuffered = true
5
6 form.show()
Count child controls[edit]
1 local form = createForm(false)
2 createButton(form)
3 createEdit(form)
4
5 local count = form.ControlCount
6
7 print("Child controls: " .. tostring(count))
8
9 form.destroy()
Access child controls by index[edit]
1 local form = createForm(false)
2 createButton(form)
3 createEdit(form)
4
5 for i = 0, form.ControlCount - 1 do
6 local control = form.Control[i]
7 print(control.ClassName)
8 end
9
10 form.destroy()
Use getControlCount and getControl[edit]
1 local form = createForm(false)
2 createButton(form)
3 createEdit(form)
4
5 local count = form.getControlCount()
6
7 for i = 0, count - 1 do
8 local control = form.getControl(i)
9 print(control.ClassName)
10 end
11
12 form.destroy()
Get the control at a relative position[edit]
1 local form = createForm(false)
2 local button = createButton(form)
3 button.Left = 10
4 button.Top = 10
5
6 local control = form.getControlAtPos(15, 15)
7
8 if control ~= nil then
9 print("Control at position: " .. control.ClassName)
10 end
11
12 form.destroy()
Check whether a control can receive focus[edit]
1 local form = createForm(false)
2 local edit = createEdit(form)
3
4 if edit.canFocus() then
5 print("Edit can receive focus")
6 end
7
8 form.destroy()
Set keyboard focus[edit]
1 local form = createForm(false)
2 local edit = createEdit(form)
3
4 form.show()
5
6 if edit.canFocus() then
7 edit.setFocus()
8 end
Check whether a control is focused[edit]
1 local form = createForm(false)
2 local edit = createEdit(form)
3
4 print("Focused: " .. tostring(edit.focused()))
5
6 form.destroy()
Assign OnEnter and OnExit events through properties[edit]
1 local form = createForm(false)
2 local edit = createEdit(form)
3
4 edit.OnEnter = function(sender)
5 print("Focus entered")
6 end
7
8 edit.OnExit = function(sender)
9 print("Focus lost")
10 end
11
12 form.show()
Assign OnEnter and OnExit events through methods[edit]
1 local form = createForm(false)
2 local edit = createEdit(form)
3
4 edit.setOnEnter(function(sender)
5 print("Focus entered")
6 end)
7
8 edit.setOnExit(function(sender)
9 print("Focus lost")
10 end)
11
12 form.show()
Read assigned focus event handlers[edit]
1 local form = createForm(false)
2 local edit = createEdit(form)
3
4 edit.OnEnter = function(sender) print("Enter") end
5
6 local onEnter = edit.getOnEnter()
7 local onExit = edit.getOnExit()
8
9 print("OnEnter assigned: " .. tostring(onEnter ~= nil))
10 print("OnExit assigned: " .. tostring(onExit ~= nil))
11
12 form.destroy()
Set a Region as the shape[edit]
1 local form = createForm(false)
2 local region = createRegion()
3
4 form.Width = 300
5 form.setShape(region)
6
7 form.show()
Set layered alpha attributes[edit]
1 local form = createForm(false)
2 form.Width = 300
3 form.Height = 200
4 form.show()
5
6 form.setLayeredAttributes(0, 180, LWA_ALPHA)