AutoHotkey Community

It is currently May 26th, 2012, 1:30 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: Simple minimize to tray
PostPosted: September 6th, 2008, 7:53 pm 
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
   }
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 6th, 2008, 8:12 pm 
Gui, Cancel would probably be a better choice now that I think about it.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 6th, 2008, 8:24 pm 
:roll: Ok, now the working code:

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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2008, 11:38 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2008, 12:07 pm 
Offline

Joined: July 17th, 2008, 9:46 am
Posts: 225
Wow!
good script!
Example: (with small gui :) )
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2008, 8:56 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 6th, 2009, 6:49 pm 
Offline

Joined: February 7th, 2009, 5:37 am
Posts: 43
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 6th, 2009, 7:57 pm 
Offline

Joined: May 3rd, 2009, 7:16 pm
Posts: 345
Location: OH, USA
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 18th, 2009, 5:36 am 
Offline

Joined: February 11th, 2007, 4:10 pm
Posts: 185
It's truely simple and effective, thanks, Solar, good job.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 20th, 2009, 7:38 pm 
Offline
User avatar

Joined: July 17th, 2008, 12:58 am
Posts: 270
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 :)

_________________
QuickSubs | Popcorn Movie Db
All my scripts are just in AutoHotkey v1.0.48.05


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 8th, 2009, 2:59 pm 
Offline

Joined: November 3rd, 2009, 6:09 pm
Posts: 10
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2010, 6:41 am 
can someone explain why a Timer is used?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2010, 2:26 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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".


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2010, 4:45 pm 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2010, 6:00 pm 
thanks guys, i was just curious. i don't really understand it, and the docs confuse me as well. :?


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, maraskan_user and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group