Is the hwnd from WinExis("A") Mine? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Is the hwnd from WinExis("A") Mine?

20 Sep 2017, 05:42

I need to know if the window handle I got from WinExist("A") is one of the windows I created in my script via the GUI commands...

I could make a list of handles of windows I spawned to compare with but if there is a sure way to do it in a few ahk functions... that would spare me the list management implementation; the need to add and remove entries from the window list as they are created and destroyed.

The method should determine if the hwnd is from my script and not from any another ahk script that may be running at the same time

The method should work for when in the ahk runner, all version (32b,64b, ansi, unicode) and in the compiled version (.exe)
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Is the hwnd from WinExis("A") Mine?

20 Sep 2017, 05:56

Code: Select all

#NoEnv

Gui, +HwndHGUI
Gui, Show, w400 h400, Test

MyPID := DllCall("GetCurrentProcessId")
MyGUIs := "ahk_class AutoHotkeyGUI ahk_pid " . MyPID

If (HWND := WinActive(MyGUIs))
   MsgBox, % "Bingo! " . HGUI . " - " . HWND
ExitApp
?
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Is the hwnd from WinExis("A") Mine?  Topic is solved

20 Sep 2017, 06:02

Another option:

Code: Select all

#NoEnv

Gui, +HwndHGUI
Gui, Show, w400 h400, Test

MyPID := DllCall("GetCurrentProcessId")

If (HWND := WinExist("A")) {
   WinGetClass, Class
   WinGet, PID, PID
   If (Class = "AutoHotkeyGUI") && (PID = MyPID)
      MsgBox, % "Bingo! " . HGUI . " - " . HWND
   Else
      MsgBox, % "Nada! The window " . HWND . " doesn't belong to this script!"
}

ExitApp
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: Is the hwnd from WinExis("A") Mine?

20 Sep 2017, 06:11

Bingo indeed.

So this gets the process id of this running instance, generates a search, to use in WinActive, for a window of class AutoHotkeyGUI AND of/in process id of this instance

Clever! It works!

Thank you
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: Is the hwnd from WinExis("A") Mine?

20 Sep 2017, 06:14

Hmm, is the class name required? Is is not enough to simply check the PID?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Is the hwnd from WinExis("A") Mine?

20 Sep 2017, 09:13

Hwnd is unique. Why not hgui == winactive("a") ? I'm on the phone so reading the codeboxes is slightly awkward, sorry if I missed the point.
Cheers.
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Is the hwnd from WinExis("A") Mine?

20 Sep 2017, 09:15

You might omit the class if you want to match non-GUI windows like MsgBox, ToolTip, InputBox, etc. too.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Is the hwnd from WinExis("A") Mine?

20 Sep 2017, 09:33

Sorry, I missed the point it seems :(. Nice approach just me :thumbup:
Alternative,

Code: Select all

gui, new ,+hwndA
gui,show, w200 h200
gui, new ,+hwndB
gui,show, w200 h200
myGuiHandles := {(A):A,(B):B}

f1::msgbox % myGuiHandles.hasKey(winactive("A")) ? "My gui!" : "Not my gui"

guiclose(hwnd){
	global myGuiHandles
	myGuiHandles.delete(hwnd)
	gui % hwnd ": destroy"
}
Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 398 guests