AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple minimize to tray

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Solar
Guest





PostPosted: Sat Sep 06, 2008 6:53 pm    Post subject: Simple minimize to tray Reply with quote

The easiest way to minimize your gui to the system tray?

Code:

OnMessage(0x112, "WM_SYSCOMMAND")
Return
WM_SYSCOMMAND(wParam)
{
   If wParam = 61472
   {
      Gui, Destroy
      If A_IconHidden = 0 ; NoTrayIcon
         Menu, Tray, Icon
   }
}
Back to top
Solar
Guest





PostPosted: Sat Sep 06, 2008 7:12 pm    Post subject: Reply with quote

Gui, Cancel would probably be a better choice now that I think about it.
Back to top
Solar
Guest





PostPosted: Sat Sep 06, 2008 7:24 pm    Post subject: Reply with quote

Rolling Eyes Ok, now the working code:

Code:

WM_SYSCOMMAND(wParam)
{
   If wParam = 61472
   {
      Gui, Hide
      If A_IconHidden = 1 ; NoTrayIcon
         Menu, Tray, Icon
   }
}
Back to top
WhoAmIAgain
Guest





PostPosted: Sun Nov 30, 2008 10:38 am    Post subject: Reply with quote

I use:

Code:

Menu, Tray, Add, Toggle Window, GuiToggle
Menu, Tray, Default, Toggle Window
;...
Gui, show, w600 h400,.
;...
Main_Window := WinExist("A")


GuiToggle:
IfWinExist, ahk_id %Main_Window%
 Gui, Hide
else
 Gui, Show
return


The minimize button can minimize it to tray and a dbl click of the tray icon will restore it if it's hidden or restore it if it's not.
Back to top
DHMH



Joined: 17 Jul 2008
Posts: 225

PostPosted: Sun Nov 30, 2008 11:07 am    Post subject: Reply with quote

Wow!
good script!
Example: (with small gui Smile )
Code:
#Notrayicon
#SingleInstance, Force
Gui, show, w120 h20

OnMessage(0x112, "WM_SYSCOMMAND")
Return

guiclose:
exitapp


WM_SYSCOMMAND(wParam)
{
   If wParam = 61472
   {
      Gui, Hide
      If A_IconHidden = 1 ; NoTrayIcon
         Menu, Tray, Icon
   }
}


Thx for the function!
Greets,
DHMH
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Sun Nov 30, 2008 7:56 pm    Post subject: Reply with quote

Thanks..

Code:
#NoTrayIcon                       ; MinimizeGUItoTray() by Skan   --   created 18-Aug-2008
                                  ; www.autohotkey.com/forum/viewtopic.php?p=214612#214612

Menu("Tray","Nostandard"), Menu("Tray","Add","Restore","GuiShow"), Menu("Tray","Add")
Menu("Tray","Default","Restore"), Menu("Tray","Click",1), Menu("Tray","Standard")
Menu("Tray","Icon","User32.dll",4)

Gui +LastFound +Owner
Gui1 := WinExist()
OnMessage(0x112, "WM_SYSCOMMAND")
Gui, Show, w250 h100, Minimize/Restore to/from Tray - Demo

Return                                                 ; // end of auto-execute section //

WM_SYSCOMMAND(wParam)
{
   If ( wParam = 61472 ) {
   SetTimer, OnMinimizeButton, -1
   Return 0
   }
}

GuiClose:
 ExitApp
Return

OnMinimizeButton:
  MinimizeGuiToTray( R, Gui1 )
  Menu("Tray","Icon")
Return

GuiShow:
  DllCall("DrawAnimatedRects", UInt,Gui1, Int,3, UInt,&R+16, UInt,&R )
  Menu("Tray","NoIcon")
  Gui, Show
Return


MinimizeGuiToTray( ByRef R, hGui ) {
  WinGetPos, X0,Y0,W0,H0, % "ahk_id " (Tray:=WinExist("ahk_class Shell_TrayWnd"))
  ControlGetPos, X1,Y1,W1,H1, TrayNotifyWnd1,ahk_id %Tray%
  SW:=A_ScreenWidth,SH:=A_ScreenHeight,X:=SW-W1,Y:=SH-H1,P:=((Y0>(SH/3))?("B"):(X0>(SW/3))
  ? ("R"):((X0<(SW/3))&&(H0<(SH/3)))?("T"):("L")),((P="L")?(X:=X1+W0):(P="T")?(Y:=Y1+H0):)
  VarSetCapacity(R,32,0), DllCall( "GetWindowRect",UInt,hGui,UInt,&R)
  NumPut(X,R,16), NumPut(Y,R,20), DllCall("RtlMoveMemory",UInt,&R+24,UInt,&R+16,UInt,8 )
  DllCall("DrawAnimatedRects", UInt,hGui, Int,3, UInt,&R, UInt,&R+16 )
  WinHide, ahk_id %hGui%
}

Menu( MenuName, Cmd, P3="", P4="", P5="" ) {
  Menu, %MenuName%, %Cmd%, %P3%, %P4%, %P5%
Return errorLevel
}
Back to top
View user's profile Send private message Send e-mail
Big Digger



Joined: 07 Feb 2009
Posts: 43

PostPosted: Sat Jun 06, 2009 5:49 pm    Post subject: Reply with quote

Code:
Menu, Tray, Add, Show, GuiShow
Menu, Tray, Default, Show
Menu, Tray, Click, 1

Gui, Add, Text, , Minimize Gui to tray…
Gui, Show

Return

GuiShow:
Gui, Show
Return

GuiSize:
If A_EventInfo = 1
   Gui, Show, Hide
Return

GuiClose:
ExitApp
Back to top
View user's profile Send private message
Solar



Joined: 03 May 2009
Posts: 345
Location: OH, USA

PostPosted: Sat Jun 06, 2009 6:57 pm    Post subject: Reply with quote

Big Digger, that won't work because the window can be restored by means other than the tray. For example, double click the window on the taskbar and see what happens. As resize and restore are both sent as the same A_EventInfo, unless you mind the extra Gui, Show commands, don't use that method.

SKAN pointed out that a timer should be used. Here's an updated example that works well:

Code:
#NoTrayIcon
Menu, Tray, Click, 1
Menu, Tray, Add, Restore, Restore
Menu, Tray, Default, Restore

OnMessage(0x112, "WM_SYSCOMMAND")
Gui, Show, w200 h100, Minimize to Tray
Return

WM_SYSCOMMAND(wParam)
{
   If (wParam = 61472) ; minimize
      SetTimer, Minimize, -1
   Else If (wParam = 61728) ; restore
      SetTimer, Restore, -1
}

Minimize:
   Critical
   Gui, Hide
   Menu, Tray, Icon
Return
   
Restore:
   Critical
   Menu, Tray, NoIcon
   Gui, Show
Return

GuiEscape:
GuiClose:
   ExitApp


Edit: Added Critical to both labels so that they cannot be interrupted by each other causing the tray icon to display improperly.
Back to top
View user's profile Send private message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Sat Jul 18, 2009 4:36 am    Post subject: Reply with quote

It's truely simple and effective, thanks, Solar, good job.
Back to top
View user's profile Send private message
Delusion



Joined: 16 Jul 2008
Posts: 210
Location: Greece/Rhodos

PostPosted: Sun Sep 20, 2009 6:38 pm    Post subject: Reply with quote

SKAN wrote:
Thanks..

Code:
#NoTrayIcon                       ; MinimizeGUItoTray() by Skan   --   created 18-Aug-2008
                                  ; www.autohotkey.com/forum/viewtopic.php?p=214612#214612

Menu("Tray","Nostandard"), Menu("Tray","Add","Restore","GuiShow"), Menu("Tray","Add")
Menu("Tray","Default","Restore"), Menu("Tray","Click",1), Menu("Tray","Standard")
Menu("Tray","Icon","User32.dll",4)

Gui +LastFound +Owner
Gui1 := WinExist()
OnMessage(0x112, "WM_SYSCOMMAND")
Gui, Show, w250 h100, Minimize/Restore to/from Tray - Demo

Return                                                 ; // end of auto-execute section //

WM_SYSCOMMAND(wParam)
{
   If ( wParam = 61472 ) {
   SetTimer, OnMinimizeButton, -1
   Return 0
   }
}

GuiClose:
 ExitApp
Return

OnMinimizeButton:
  MinimizeGuiToTray( R, Gui1 )
  Menu("Tray","Icon")
Return

GuiShow:
  DllCall("DrawAnimatedRects", UInt,Gui1, Int,3, UInt,&R+16, UInt,&R )
  Menu("Tray","NoIcon")
  Gui, Show
Return


MinimizeGuiToTray( ByRef R, hGui ) {
  WinGetPos, X0,Y0,W0,H0, % "ahk_id " (Tray:=WinExist("ahk_class Shell_TrayWnd"))
  ControlGetPos, X1,Y1,W1,H1, TrayNotifyWnd1,ahk_id %Tray%
  SW:=A_ScreenWidth,SH:=A_ScreenHeight,X:=SW-W1,Y:=SH-H1,P:=((Y0>(SH/3))?("B"):(X0>(SW/3))
  ? ("R"):((X0<(SW/3))&&(H0<(SH/3)))?("T"):("L")),((P="L")?(X:=X1+W0):(P="T")?(Y:=Y1+H0):)
  VarSetCapacity(R,32,0), DllCall( "GetWindowRect",UInt,hGui,UInt,&R)
  NumPut(X,R,16), NumPut(Y,R,20), DllCall("RtlMoveMemory",UInt,&R+24,UInt,&R+16,UInt,8 )
  DllCall("DrawAnimatedRects", UInt,hGui, Int,3, UInt,&R, UInt,&R+16 )
  WinHide, ahk_id %hGui%
}

Menu( MenuName, Cmd, P3="", P4="", P5="" ) {
  Menu, %MenuName%, %Cmd%, %P3%, %P4%, %P5%
Return errorLevel
}


thats great
but that animation doesnt seem to work on vista (i got vista ultimate x64 sp2) .is there any way to make it work?

thanks in advance Smile
_________________
Popcorn Movie Db
Simple Apnea Trainer
Back to top
View user's profile Send private message Visit poster's website
antihack



Joined: 03 Nov 2009
Posts: 10

PostPosted: Sun Nov 08, 2009 1:59 pm    Post subject: Reply with quote

WhoAmIAgain wrote:
I use:

Code:

Menu, Tray, Add, Toggle Window, GuiToggle
Menu, Tray, Default, Toggle Window
;...
Gui, show, w600 h400,.
;...
Main_Window := WinExist("A")


GuiToggle:
IfWinExist, ahk_id %Main_Window%
 Gui, Hide
else
 Gui, Show
return


The minimize button can minimize it to tray and a dbl click of the tray icon will restore it if it's hidden or restore it if it's not.


Hello!

This script works, but only if you double click the tray icon.
How to make application go to tray by clicking that minimize button in top right corner of GUI.
_________________
Follow your dreams!
Back to top
View user's profile Send private message
guest3456
Guest





PostPosted: Sun Mar 28, 2010 5:41 am    Post subject: Reply with quote

can someone explain why a Timer is used?
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Sun Mar 28, 2010 1:26 pm    Post subject: Reply with quote

guest3456 wrote:
can someone explain why a Timer is used?


Code:
WM_SYSCOMMAND(wParam)
{
   If ( wParam = 61472 ) {
   SetTimer, OnMinimizeButton, -1
   Return 0
   }
}


OnMessage functions need to return a true or false within the shortest possible time.
GoSub would halt the function from returning a value until the subroutine itself returns.
SetTimer, SubRoutine, -1 does not halt the OnMessage function from returning, and at the same time the subroutine gets triggered within 1 millisecond making the code "all safe".
Back to top
View user's profile Send private message Send e-mail
TheGood



Joined: 30 Jul 2007
Posts: 580

PostPosted: Sun Mar 28, 2010 3:45 pm    Post subject: Reply with quote

SKAN wrote:
OnMessage functions need to return a true or false within the shortest possible time.
GoSub would halt the function from returning a value until the subroutine itself returns.
SetTimer, SubRoutine, -1 does not halt the OnMessage function from returning, and at the same time the subroutine gets triggered within 1 millisecond making the code "all safe".

Just to clarify, that's only necessary if the message is less than 0x312 (like WM_SYSCOMMAND here). If the message is greater than that, you could use Critical to buffer the messages and not having to worry about returning a value ASAP.
Back to top
View user's profile Send private message Visit poster's website
guest3456
Guest





PostPosted: Sun Mar 28, 2010 5:00 pm    Post subject: Reply with quote

thanks guys, i was just curious. i don't really understand it, and the docs confuse me as well. Confused
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group