Close a Window - How

Get help with using AutoHotkey and its commands and hotkeys
Albireo
Posts: 576
Joined: 16 Oct 2013, 13:53

Close a Window - How

18 May 2017, 08:29

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%
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Close a Window - How

18 May 2017, 08:34

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.
Albireo
Posts: 576
Joined: 16 Oct 2013, 13:53

Re: Close a Window - How

18 May 2017, 09:01

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)....
Albireo
Posts: 576
Joined: 16 Oct 2013, 13:53

Re: Close a Window - How

18 May 2017, 09:20

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!
User avatar
jeeswg
Posts: 6122
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Close a Window - How

18 May 2017, 09:25

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Close a Window - How

18 May 2017, 09:49

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?
Albireo
Posts: 576
Joined: 16 Oct 2013, 13:53

Re: Close a Window - How

18 May 2017, 10:31

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?

Return to “Ask For Help”

Who is online

Users browsing this forum: Odlanir and 185 guests