Difference between revisions of "Lua:getWindowClassName"

From Cheat Engine
Jump to navigation Jump to search
(Created page with "<p><b>getWindowClassName(windowhandle)</b>: string - Returns the classname of the window</p> <pre> local hwnd = findWindow("WTWindow", "Game") print( getWindowClassName(hwnd)...")
 
(Major overhaul of the post.)
 
Line 1: Line 1:
<p><b>getWindowClassName(windowhandle)</b>: string - Returns the classname of the window</p>
+
[[Category:Lua]]
<pre>
+
{{CodeBox|'''function''' getWindowClassName(''windowhandle'') ''':''' string}}
local hwnd = findWindow("WTWindow", "Game")
+
 
print( getWindowClassName(hwnd) ) -- WTWindow
+
Returns the class name of the specified window.
</pre>
+
 
 +
===Function Parameters===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|windowhandle
 +
|Integer
 +
|The handle of the window to query.
 +
|}
 +
 
 +
===Returns===
 +
String — The class name of the specified window.
 +
 
 +
===Examples===
 +
 
 +
====Get the class name of the main form====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local form = getMainForm()
 +
local windowHandle = form.Handle
 +
 
 +
local className = getWindowClassName(windowHandle)
 +
 
 +
print(className)
 +
</syntaxhighlight>
 +
 
 +
====Get the class name of a created form====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local form = createForm(false)
 +
form.Caption = "Class Name Test"
 +
 
 +
local className = getWindowClassName(form.Handle)
 +
 
 +
print("Window class name: " .. tostring(className))
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
 +
 
 +
====Use with the foreground window====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local windowHandle = getForegroundWindow()
 +
 
 +
if windowHandle ~= nil and windowHandle ~= 0 then
 +
  local className = getWindowClassName(windowHandle)
 +
 
 +
  print("Foreground window class: " .. tostring(className))
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Compare a window class name====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local windowHandle = getForegroundWindow()
 +
 
 +
if windowHandle ~= nil and windowHandle ~= 0 then
 +
  local className = getWindowClassName(windowHandle)
 +
 
 +
  if className == "TCEForm" then
 +
    print("The foreground window is a Cheat Engine form")
 +
  end
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Get class name and process ID====
 +
<syntaxhighlight lang="lua" line highlight="4,5">
 +
local windowHandle = getForegroundWindow()
 +
 
 +
if windowHandle ~= nil and windowHandle ~= 0 then
 +
  local className = getWindowClassName(windowHandle)
 +
  local processID = getWindowProcessID(windowHandle)
 +
 
 +
  print("Class: " .. tostring(className))
 +
  print("PID: " .. tostring(processID))
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Guard against an invalid window handle====
 +
<syntaxhighlight lang="lua" line highlight="4">
 +
local windowHandle = 0
 +
 
 +
if windowHandle ~= nil and windowHandle ~= 0 then
 +
  local className = getWindowClassName(windowHandle)
 +
 
 +
  print(className)
 +
else
 +
  print("Invalid window handle")
 +
end
 +
</syntaxhighlight>
 +
 
 +
{{LuaSeeAlso}}
 +
 
 +
{{Forms}}

Latest revision as of 00:28, 27 June 2026

<> Lua API Reference

function getWindowClassName(windowhandle) : string

Returns the class name of the specified window.

Function Parameters[edit]

Parameter Type Description
windowhandle Integer The handle of the window to query.

Returns[edit]

String — The class name of the specified window.

Examples[edit]

Get the class name of the main form[edit]

1 local form = getMainForm()
2 local windowHandle = form.Handle
3 
4 local className = getWindowClassName(windowHandle)
5 
6 print(className)

Get the class name of a created form[edit]

1 local form = createForm(false)
2 form.Caption = "Class Name Test"
3 
4 local className = getWindowClassName(form.Handle)
5 
6 print("Window class name: " .. tostring(className))
7 
8 form.destroy()

Use with the foreground window[edit]

1 local windowHandle = getForegroundWindow()
2 
3 if windowHandle ~= nil and windowHandle ~= 0 then
4   local className = getWindowClassName(windowHandle)
5 
6   print("Foreground window class: " .. tostring(className))
7 end

Compare a window class name[edit]

1 local windowHandle = getForegroundWindow()
2 
3 if windowHandle ~= nil and windowHandle ~= 0 then
4   local className = getWindowClassName(windowHandle)
5 
6   if className == "TCEForm" then
7     print("The foreground window is a Cheat Engine form")
8   end
9 end

Get class name and process ID[edit]

1 local windowHandle = getForegroundWindow()
2 
3 if windowHandle ~= nil and windowHandle ~= 0 then
4   local className = getWindowClassName(windowHandle)
5   local processID = getWindowProcessID(windowHandle)
6 
7   print("Class: " .. tostring(className))
8   print("PID: " .. tostring(processID))
9 end

Guard against an invalid window handle[edit]

1 local windowHandle = 0
2 
3 if windowHandle ~= nil and windowHandle ~= 0 then
4   local className = getWindowClassName(windowHandle)
5 
6   print(className)
7 else
8   print("Invalid window handle")
9 end

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Form Related Classes