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

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

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

01 Mar 2018, 16:02

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.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

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

01 Mar 2018, 17:29

Thanks, works great, add to my LIB folder.
Regards,
burque505
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

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

13 Mar 2018, 10:24

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

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

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

17 Dec 2019, 16:27

Thanks. I am using works.
evilC wrote:
01 Mar 2018, 16:02

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 161 guests