Page 1 of 1

Close a Window - How

Posted: 18 May 2017, 08:29
by Albireo
My desire is to close a window in a program. (Windows 7 pro Swe)
But whatever I do, it doesn't seem to work.

For example .:

Code: Select all

WinID = Sök ahk_exe Label32.exe ahk_class TfrmSearch
IfWinExist %WinID%
{	WinActivate %WinID%
	WinWaitActive %WinID%
}
This code always activates the window.

The window can be manually closed as follows .:
1. Alt + A
2. Press left mouse button on "TButton1"
3. Press left mouse button on the "X" in the upper right corner.
4. Tab + Tab + Enter

But nothing (below) works for me :roll: What am I doing wrong?
( WinID = Sök ahk_exe Label32.exe ahk_class TfrmSearch from Window Info )

Code: Select all

ControlSend TButton1, !A, %WinID%
ControlSend TButton1, !A

ControlSend ,, {Tab}{Tab}{Enter}, %WinID%
ControlSend ,, {Tab}{Tab}{Enter}

ControlClick TButton1, %WinID%
ControlClick TButton1

WinClose %WinID%
WinClose

WinKill %WinID%

Re: Close a Window - How

Posted: 18 May 2017, 08:34
by Exaskryz
Try using ControlSend,, !a, %WinID%, then ControlSend, ahk_parent, !a, %WinID%, then ControlSend, TButton1, !a, %WinID% - yes, Alt+A is different from Alt+a. Alt+A is Alt+Shift+a.

Additionally, your code is out of order from your 4 step instructions you had, so I'm not sure exactly what you're looking for.

Re: Close a Window - How

Posted: 18 May 2017, 09:01
by Albireo
It must be "Alt + A" ( Text .: &Avbryt )

Now I have tried this code .:

Code: Select all

WinID = Sök ahk_exe Label32.exe ahk_class TfrmSearch
IfWinExist %WinID%	; if WinExist("ahk_exe Label32.exe")
{	WinActivate %WinID%
	WinWaitActive %WinID%
	ControlSend,, !A, %WinID%
	ControlSend, ahk_parent, !A, %WinID%
	ControlSend, TButton1, !A, %WinID%
}
else
	MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, Window not found!
Nothing happens! (Just the window will be activated)....

Re: Close a Window - How

Posted: 18 May 2017, 09:20
by Albireo
The only thing I want, is to close that window.
Now I have tried .:

Code: Select all

	ControlFocus TButton1, %WinID%
	ControlSend ,, {Enter}, %WinID%
Nothing happens!

Re: Close a Window - How

Posted: 18 May 2017, 09:25
by jeeswg
Did you try ControlSend and Esc, or WinClose (which can crash some programs I believe)? Another possibility is to use the Acc library to invoke the Close button. [EDIT: OK, you did try WinClose.]

[EDIT:] Did you try SendInput though just to confirm that key presses could work? Did you try all of these techniques on 'A', the active window? Did you MsgBox the value of WinID to confirm that you successfully acquired an hWnd?

Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

Code: Select all

q:: ;Notepad - invoke Close button
WinGet, hWnd, ID, ahk_class Notepad
oAcc := Acc_Get("Object", "2", 0, "ahk_id " hWnd)
oAcc.accDoDefaultAction(5)
oAcc := ""
return

Re: Close a Window - How

Posted: 18 May 2017, 09:49
by Exaskryz
Albireo wrote:It must be "Alt + A" ( Text .: &Avbryt )

Now I have tried this code .:

Code: Select all

WinID = Sök ahk_exe Label32.exe ahk_class TfrmSearch
IfWinExist %WinID%	; if WinExist("ahk_exe Label32.exe")
{	WinActivate %WinID%
	WinWaitActive %WinID%
	ControlSend,, !A, %WinID%
	ControlSend, ahk_parent, !A, %WinID%
	ControlSend, TButton1, !A, %WinID%
}
else
	MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, Window not found!
Nothing happens! (Just the window will be activated)....
So when you go to close the window by hand, you are absolutely pressing Alt + Shift + a?

Re: Close a Window - How

Posted: 18 May 2017, 10:31
by Albireo
Thank you for all suggestions

I Probably found the solution..
The AHK-program must be running as Administrator...
But if I run the AHK-program I got a UAC-question.
A few years ago, AHK programs were started through the scheduler, to not get any questions.

How to run all AHK-program, as need to be run as administrator?