TrayMinimizer - Enable Minimize to tray with two lines and minimal impact
Posted: 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:
If you call TrayMinimizer.Init(false), the window will stay open, if you just call TrayMinimizer.Init(), it will minimize on startup
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.
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
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
}
}