Toggle ( Run or Show ) / ( Hide ) Window

Post your working scripts, libraries and tools for AHK v1.1 and older
brodoplovac
Posts: 2
Joined: 19 Mar 2018, 06:49

Toggle ( Run or Show ) / ( Hide ) Window

19 Mar 2018, 10:05

I needed a way to conveniently open my calculator, so I made this script.

Only shortcut is Alt + Ctrl + P.
If calculator Speedcrunch.exe is not open, this shortcut will open it.
Afterward, this shortcut is toggle for hide/show Speedcrunch.exe window. Even taskbar button will be hidden.

If program is minimized, this shortcut will restore window and will place it on the top.
If calculator window is behind some other program, this shortcut will bring Speedcrunch.exe window on top.

When calculator window is hidden, focus will go back to last active window. Last active window will be remembered in C:\SpeedCrunchONOFF.txt INI file.
For this reason you have to make one "C:\SpeedCrunchONOFF.txt" INI file, and you have to place this text inside of it.

Code: Select all

[Etc]
LastActiveWindow=
If you want to close Speedcrunch, that is done manually. Just click "X".

This is text of a script.

Code: Select all

#NoEnv
SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Window
SendMode Input
#SingleInstance Force
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1


^!p::
SpeedCrunchONOFF:
SetTitleMatchMode, 3

ErrorLevel = % ProcessExist( "speedcrunch.exe" ) 

ProcessExist(Name){
	Process,Exist,%Name%
	return Errorlevel
}

If ErrorLevel = 0
{	
	Run C:\Program Files (x86)\SpeedCrunch\speedcrunch.exe
	Return
}  

IfWinActive, SpeedCrunch
{
	WinHide, SpeedCrunch
	IniRead, returnActiveWindowTitle, C:\SpeedCrunchONOFF.txt, Etc, LastActiveWindow
	WinActivate, % returnActiveWindowTitle
	Return
}
Else
{
	WinGetActiveTitle, goActiveWindowTitle
	IniWrite, % goActiveWindowTitle, C:\SpeedCrunchONOFF.txt, Etc, LastActiveWindow
	WinRestore, SpeedCrunch
	WinShow, SpeedCrunch
	WinActivate, SpeedCrunch
	Return
}
Return
brodoplovac
Posts: 2
Joined: 19 Mar 2018, 06:49

Re: Toggle ( Run or Show ) / ( Hide ) Window

16 May 2020, 06:48

This is improvement of my code.
This code has another hotkey. It is left click on the mouse. When you click on "X" button in speedcrunch program, instead of closing, speedcrunch will hide itself. This prevents me from accidentally closing the program. Program can be closed by other means (like Alt+F4).

Code: Select all

#NoEnv
SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Window
SendMode Input
#SingleInstance Force
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1

global SpeedCrunchHWND                ;global variable to share data between hotkey procedures.

^!p::
SpeedCrunchONOFF:
SetTitleMatchMode, 3

ErrorLevel = % ProcessExist1( "speedcrunch.exe" )                   ;is speedcrunch open or not

ProcessExist1(Name){
	Process,Exist,%Name%
	return Errorlevel
}

If ErrorLevel = 0                                                                        ;if it is not open, then open it
{	
	Run C:\Program Files (x86)\SpeedCrunch\speedcrunch.exe
	WinWaitActive, SpeedCrunch
	SpeedCrunchHWND := WinActive( "SpeedCrunch" )                ;I am saving handle of speedcrunch window for use in "Click" hotkey procedure
	Return
}  

IfWinActive, SpeedCrunch               ;if it is open and active, then we hide it. We activate last active window.
{
	WinHide, SpeedCrunch
	IniRead, returnActiveWindowTitle, C:\SpeedCrunchONOFF.txt, Etc, LastActiveWindow
	WinActivate, % returnActiveWindowTitle
	Return
}
Else                                          ;if it is not active or it is hidden, we show it to user. We remember last active window.
{
	WinGetActiveTitle, goActiveWindowTitle
	IniWrite, % goActiveWindowTitle, C:\SpeedCrunchONOFF.txt, Etc, LastActiveWindow
	WinRestore, SpeedCrunch
	WinShow, SpeedCrunch
	WinActivate, SpeedCrunch
	Return
}
Return
;----------------------------------------------------------------------
#If MouseIsOver(SpeedCrunchHWND)    ;"click" procedure works only above speedcrunch "X" button. Conditional hotkey.
LButton::
SetTitleMatchMode, 3
IfWinActive, SpeedCrunch
{                                                          ;instead closing speedcrunch, we hide it.
	WinHide, SpeedCrunch
	IniRead, returnActiveWindowTitle, C:\SpeedCrunchONOFF.txt, Etc, LastActiveWindow
	WinActivate, % returnActiveWindowTitle
	Return	
}
;----------------------------------------------------------------------
MouseIsOver(WinHWND)              ;helper function to determin whether we are above speedcrunch "X" button.
{
	SetTitleMatchMode, 3
	CoordMode, Mouse, Windows
	MouseGetPos, MouseX, MouseY, OutputVarWin
	WinGetPos, , , WinWidth, , ahk_id %WinhWND%
	If ( ( OutputVarWin = WinHWND ) and ( MouseY >= -45 ) and ( MouseY <= -2 ) and ( ( WinWidth - MouseX ) >= 2 ) and ( ( WinWidth - MouseX ) <= 75 ) )
	{
		Return 1
	}	
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 109 guests