Jump to content
Sign in to follow this  
itsmeagain_

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!

Share this post


Link to post
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 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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_

Share this post


Link to post
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
Sign in to follow this  

×
×
  • Create New...