Jump to content

Get the Window's class


Recommended Posts

The AutoIt Window Info tools shows at the top "Basic Window Info" and gives us the title and class of the currently active window.

I want to do just that, get the title and class (that the info tool also shows) of the currently active window. To clarify, I marked the strings that I'm after in this screenshot:

Posted Image

It's simple to get the title:

Local $wintitle = WinGetTitle("[ACTIVE]")

But how do I get the class?

Cheers!

Link to comment
Share on other sites

Why would you need to retrieve the class of a window at runtime?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

BTW: Looks like you use an older version of AutoIt. Don't know which one but when using the latest (3.3.8.1) you get a lot of bugs removed and best help on the forum because most of the users here use the latest version.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#include <WinAPI.au3>

$hwnd = WinGetHandle("[ACTIVE]")
MsgBox(0, 0, "Title is: " & WinGetTitle($hwnd) & @CRLF & "Class is: " & _WinAPI_GetClassName($hwnd))

Thanks! Works like a charm, I knew this would be easy for an AutoIt veteran :thumbsup: .

BTW: Looks like you use an older version of AutoIt. Don't know which one but when using the latest (3.3.8.1) you get a lot of bugs removed and best help on the forum because most of the users here use the latest version.

Thanks, but I just took the screenshot from the docs and added the red arrow, I downloaded the latest version from the AutoIt homepage :).

But what I want to for those that are curious:

I want to change screens (either PC monitor or TV) based on whether a certain application (XBMC) is active. Because XBMC is a full screen app the Window Info tool showed only 2 ways to identify whether XBMC is active or not, namely the active window title being "XBMC" and the class "XBMC". If I based my script on just the window title the script would change between monitor / TV when I have a folder called XBMC open in windows explorer for example, that's why I want the class as an extra check.

Anyways, this is my final script, if you have any remarks I would love to hear them :) (and I assume there are quite some ways to make it shorter and cleaner)

#include

Local $wintitle, $winclass, $lastcheck = "ACTIVE"
While 1
$hwnd = WinGetHandle("[ACTIVE]")
$wintitle = WinGetTitle($hwnd)
$winclass = _WinAPI_GetClassName($hwnd)

If $wintitle = "XBMC" AND $winclass = "XBMC" AND $lastcheck = "NOTACTIVE" Then
; XBMC is active now, but wasn't at last check, change output to TV!
ShellExecute("DisplaySwitch.exe", "/internal", "C:\Windows\System32\")
ElseIf $wintitle <> "XBMC" OR $winclass <> "XBMC" AND $lastcheck = "ACTIVE" Then
; XBMC not active now, but was at last check, change output to PC!
ShellExecute("DisplaySwitch.exe", "/external", "C:\Windows\System32\")
Endif

If $wintitle = "XBMC" AND $winclass = "XBMC" Then
$lastcheck = "ACTIVE"
Else
$lastcheck = "NOTACTIVE"
Endif

Sleep(250)
WEnd
.

EDIT:

I totally went in another direction with this (because I couldn't get the right resolution as XBMC outputs to the wrong monitor on startup, so nevermind. Thanks for the help though!

Edited by itsmeagain_
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...