Difference between revisions of "Lua:Class:WinControl"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
Control '''class''': ('''Inheritance''': ''[[Control]]''->''[[Component]]''->''[[Object]]'')
+
{{Class|'''class''' WinControl ''':''' Control}}
  
Base class for windows aware controls.
+
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 : boolean
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
: Graphical updates will go to a off screen bitmap which will then be shown on the screen instead of directly to the screen.  
+
!align="left"|Property
: May reduce flickering.
+
!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
  
; ControlCount &#58; integer
+
form.destroy()
: The number of child controls of this WinControl.
+
</syntaxhighlight>
  
; Control[''index''] &#58; Control
+
===Set keyboard focus===
: Array to access a child [[Control]].
+
<syntaxhighlight lang="lua" line highlight="6,7">
 +
local form = createForm(false)
 +
local edit = createEdit(form)
  
; OnEnter &#58; function
+
form.show()
: Function to be called when the WinControl gains focus.
 
  
; OnExit &#58; function
+
if edit.canFocus() then
: Function to be called when the WinControl loses focus.
+
  edit.setFocus()
 +
end
 +
</syntaxhighlight>
  
== Methods ==
+
===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>
  
; getControlCount() &#58; integer
+
===Assign OnEnter and OnExit events through methods===
: Returns the number of Controls attached to this class
+
<syntaxhighlight lang="lua" line highlight="4,8">
 +
local form = createForm(false)
 +
local edit = createEdit(form)
  
; getControl(''index'') &#58; control
+
edit.setOnEnter(function(sender)
: Returns a WinControl class object
+
  print("Focus entered")
 +
end)
  
; getControlAtPos(''x'', ''y'') &#58; (integer, integer)
+
edit.setOnExit(function(sender)
: Gets the [[Control]] at the given x,y position relative to the WinControl's position
+
  print("Focus lost")
 +
end)
  
; canFocus() &#58; boolean
+
form.show()
: returns true if the object can be focused
+
</syntaxhighlight>
  
; focused() &#58; boolean
+
===Read assigned focus event handlers===
: returns boolean true when focused
+
<syntaxhighlight lang="lua" line highlight="6,7">
 +
local form = createForm(false)
 +
local edit = createEdit(form)
  
; setFocus()
+
edit.OnEnter = function(sender) print("Enter") end
: tries to set keyboard focus to the object.
 
  
; setShape(''region'')
+
local onEnter = edit.getOnEnter()
: Sets the region object as the new shape for this WinControl.
+
local onExit = edit.getOnExit()
  
; setShape(''bitmap'')
+
print("OnEnter assigned: " .. tostring(onEnter ~= nil))
: Sets the Bitmap object as the new shape for this WinControl.
+
print("OnExit assigned: " .. tostring(onExit ~= nil))
  
; setOnEnter(''function'')
+
form.destroy()
: Sets an 'onEnter' event. (Triggered on focus enter)
+
</syntaxhighlight>
  
; getOnEnter() &#58; function
+
===Set a Region as the shape===
: Returns the 'onEnter' event.
+
<syntaxhighlight lang="lua" line highlight="2,5">
 +
local form = createForm(false)
 +
local region = createRegion()
  
; setOnExit(''function'')
+
form.Width = 300
: Sets an 'onExit' event. (Triggered on lost focus)
+
form.setShape(region)
  
; getOnExit() &#58; function
+
form.show()
: Returns the 'onExit' event.
+
</syntaxhighlight>
  
== See also ==
+
===Set layered alpha attributes===
* [[Lua]]
+
<syntaxhighlight lang="lua" line highlight="6">
* [[Help_File:Script engine|Script engine]]
+
local form = createForm(false)
 +
form.Width = 300
 +
form.Height = 200
 +
form.show()
  
=== Related Functions ===
+
form.setLayeredAttributes(0, 180, LWA_ALPHA)
* [[createClass]]
+
</syntaxhighlight>
* [[inheritsFromObject]]
 
* [[inheritsFromComponent]]
 
* [[inheritsFromControl]]
 
* [[inheritsFromWinControl]]
 
  
=== Related Classes ===
+
{{LuaSeeAlso}}
* [[Object]]
 
* [[Control]]
 
* [[Component]]
 
* [[Application]]
 
* [[Form]]
 

Latest revision as of 01:02, 27 June 2026

{} Class

class WinControl : Control

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.

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)

Main Pages

Core Lua documentation entry points

Lua
Script Engine