Page 1 of 1

TrayMinimizer - Enable Minimize to tray with two lines and minimal impact

Posted: 01 Mar 2018, 16:02
by evilC
For a while now, I have been using Skan's MinimizeToTray, but I find it a bit of a pain to add to a script - I wanted a quick and easy solution.
So I ripped his code apart, and here is a version which should be much simpler to use.

For easiest use, place the library in your "My Documents" folder, in AutoHotkey\Lib
Then just include it in any script in any folder by adding this line to the start: #include <TrayMinimizer>

Once you have done building your gui, call TrayMinimizer.Init() - for example:

Code: Select all

#Include <TrayMinimizer>	; <-- Pulls in the code from another file

; Build your GUI
Gui, Add, Text,, THIS IS A TEST
Gui, Show, w200

TrayMinimizer.Init(false)	; <-- Initializes and optionally minimizes
return
If you call TrayMinimizer.Init(false), the window will stay open, if you just call TrayMinimizer.Init(), it will minimize on startup

Code: Select all

; Tray Minimizer class by evilC
; Based on Skan's MinimizeToTray

; Include this file, then call TrayMinimizer.Init()
; By default, will minimize Gui on start
; To disable, initialize with TrayMinimizer.Init(false)
class TrayMinimizer {
	Init(minimizeOnStart := true){
		; Store the HWND of the main Gui
		Gui, +HwndhGui
		this.hGui := hGui
		; Create a BoundFunc for the Minimize handler
		this.MinimizeFn := this.Minimize.Bind(this)
		; Build tray menu
		this.Menu("Tray","Nostandard")
		this.Menu("Tray","Add","Restore", this.GuiShow.Bind(this))
		this.Menu("Tray","Add")
		this.Menu("Tray","Default","Restore")
		this.Menu("Tray","Click",1)
		this.Menu("Tray","Standard")
		; Listen to messages to detect minimize click
		OnMessage(0x112, this.WM_SYSCOMMAND.Bind(this))
		if (minimizeOnStart){
			this.Minimize()
		}
	}
	
	; Detects click of Minimize button
	WM_SYSCOMMAND(wParam){
		If ( wParam == 61472 ) {
			fn := this.MinimizeFn
			; Async fire off the minimze function
			SetTimer, % fn, -1
			; Swallow this message (Stop window from doing normal minimze)
			Return 0
		}
	}
	
	; Handles transition from tray minimized to restored
	GuiShow(){
		; Remove tray icon - ToDo: should we not leave this?
		this.Menu("Tray","NoIcon")
		Gui, Show
	}
	
	; Minimizes to tray
	Minimize() {
		WinHide, % "ahk_id " this.hGui
		this.Menu("Tray","Icon")
	}
	
	; Function wrapper for menu command
	Menu( MenuName, Cmd, P3 := "", P4 := "", P5 := "" ) {
		Menu, % MenuName, % Cmd, % P3, % P4, % P5
		Return errorLevel
	}
}
As a side note, Skan's original code used DrawAnimatedRects API calls to make the window minimize towards the tray, but this does not seem to work for me. In the original code, it seemed to use the global variable R to store two RECT structs, so it could pass those to the WinAPI call. I guess this could be done with ObjSetCapacity and stored as a class property, but I cannot even seem to get the original code to work. If anyone could help out, that would be great, but I am not hugely fussed as it is largely cosmetic.

Re: TrayMinimizer - Enable Minimize to tray with two lines and minimal impact

Posted: 01 Mar 2018, 17:29
by burque505
Thanks, works great, add to my LIB folder.
Regards,
burque505

Re: TrayMinimizer - Enable Minimize to tray with two lines and minimal impact

Posted: 13 Mar 2018, 10:24
by guest3456
evilC wrote: As a side note, Skan's original code used DrawAnimatedRects API calls to make the window minimize towards the tray, but this does not seem to work for me. In the original code, it seemed to use the global variable R to store two RECT structs, so it could pass those to the WinAPI call. I guess this could be done with ObjSetCapacity and stored as a class property, but I cannot even seem to get the original code to work. If anyone could help out, that would be great, but I am not hugely fussed as it is largely cosmetic.

this claims that that API is dependent upon Aero (which cannot be turned off in Win8+):
https://stackoverflow.com/questions/401 ... 5_40116051
https://social.msdn.microsoft.com/Forum ... aero-theme

i tested on my win7 VM with basic theme and AHK basic, and SKAN's original script did work. with Aero theme it failed.

workaround is to just loop yourself and move the window. the built-in API wasn't very smooth anyway
https://stackoverflow.com/questions/989 ... 287#989287

Re: TrayMinimizer - Enable Minimize to tray with two lines and minimal impact

Posted: 17 Dec 2019, 16:27
by hasantr
Thanks. I am using works.
evilC wrote:
01 Mar 2018, 16:02