Sign in to follow this  
Followers 0
vladedoty

How do I minimize my window to system tray?

8 posts in this topic

#1 ·  Posted (edited)

I'm trying to minimize my autoit program to the system tray after clicking the Hide button. I then want a menu of "About", "Restore", and "Close Program" in the system tray icon. I then need restore to bring the window back up, and close program to close the program.

How would i do this?

P.S. I've tried all the examples in here and none of them work.

the hide button variable name is:

$HideBtn
Edited by vladedoty

Share this post


Link to post
Share on other sites



I'm trying to minimize my autoit program to the system tray after clicking the Hide button. I then want a menu of "About", "Restore", and "Close Program" in the system tray icon. I then need restore to bring the window back up, and close program to close the program.

How would i do this?

P.S. I've tried all the examples in here and none of them work.

the hide button variable name is:

$HideBtn
The name of function to minimize window is:

_MinimizeFnc()

Share this post


Link to post
Share on other sites

The name of function to minimize window is:

_MinimizeFnc()

And where do I find this function at?

Share this post


Link to post
Share on other sites

Heh, I guess it was a sarcasm :)

Have a look please:

#include <Constants.au3>
#include <GUIConstants.au3>
#include <WinAPI.au3>
Opt('GUIOnEventMode', 1)
Opt('TrayOnEventMode', 1)

Global Const $IDANI_OPEN = 1
Global Const $IDANI_CAPTION = 3

Global $hGUI = GUICreate('Test', 100, 200)
Global $fMinimized = False
Global $hTray = ControlGetHandle('[CLASS:Shell_TrayWnd]', '', 'TrayNotifyWnd1')

TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, '_Restore')
TraySetClick(16)

GUISetOnEvent($GUI_EVENT_CLOSE, '_Minimize')

GUISetState()
While 1
    Sleep(100)
WEnd

Func _Minimize()
    Local $tRcFrom , $tRcTo
    
    If Not $fMinimized Then
        $tRcFrom = _WinAPI_GetWindowRect($hGUI)
        $tRcTo = _WinAPI_GetWindowRect($hTray)
        _WinAPI_DrawAnimatedRects($hGUI, $IDANI_CAPTION, DllStructGetPtr($tRcFrom), DllStructGetPtr($tRcTo))
        GUISetState(@SW_HIDE)
        $fMinimized = True
    EndIf
EndFunc

Func _Restore()
    If $fMinimized Then
        $tRcFrom = _WinAPI_GetWindowRect($hTray)
        $tRcTo = _WinAPI_GetWindowRect($hGUI)
        _WinAPI_DrawAnimatedRects($hGUI, $IDANI_CAPTION, DllStructGetPtr($tRcFrom), DllStructGetPtr($tRcTo))
        GUISetState(@SW_SHOW)
        $fMinimized = False
    EndIf
EndFunc

Func _WinAPI_DrawAnimatedRects($hWnd, $iAnim, $pRectFrom, $pRectTo)
    Local $aResult
    
    $aResult = DllCall('user32.dll', 'int', 'DrawAnimatedRects', 'hwnd', $hWnd, 'int', $iAnim, 'ptr', $pRectFrom, 'ptr', $pRectTo)
    If @error Then Return SetError(1, 0, 0)
    Return $aResult[0]
EndFunc

Share this post


Link to post
Share on other sites

Heh, I guess it was a sarcasm :)

Have a look please:

#include <Constants.au3>
#include <GUIConstants.au3>
#include <WinAPI.au3>
Opt('GUIOnEventMode', 1)
Opt('TrayOnEventMode', 1)

Global Const $IDANI_OPEN = 1
Global Const $IDANI_CAPTION = 3

Global $hGUI = GUICreate('Test', 100, 200)
Global $fMinimized = False
Global $hTray = ControlGetHandle('[CLASS:Shell_TrayWnd]', '', 'TrayNotifyWnd1')

TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, '_Restore')
TraySetClick(16)

GUISetOnEvent($GUI_EVENT_CLOSE, '_Minimize')

GUISetState()
While 1
    Sleep(100)
WEnd

Func _Minimize()
    Local $tRcFrom , $tRcTo
    
    If Not $fMinimized Then
        $tRcFrom = _WinAPI_GetWindowRect($hGUI)
        $tRcTo = _WinAPI_GetWindowRect($hTray)
        _WinAPI_DrawAnimatedRects($hGUI, $IDANI_CAPTION, DllStructGetPtr($tRcFrom), DllStructGetPtr($tRcTo))
        GUISetState(@SW_HIDE)
        $fMinimized = True
    EndIf
EndFunc

Func _Restore()
    If $fMinimized Then
        $tRcFrom = _WinAPI_GetWindowRect($hTray)
        $tRcTo = _WinAPI_GetWindowRect($hGUI)
        _WinAPI_DrawAnimatedRects($hGUI, $IDANI_CAPTION, DllStructGetPtr($tRcFrom), DllStructGetPtr($tRcTo))
        GUISetState(@SW_SHOW)
        $fMinimized = False
    EndIf
EndFunc

Func _WinAPI_DrawAnimatedRects($hWnd, $iAnim, $pRectFrom, $pRectTo)
    Local $aResult
    
    $aResult = DllCall('user32.dll', 'int', 'DrawAnimatedRects', 'hwnd', $hWnd, 'int', $iAnim, 'ptr', $pRectFrom, 'ptr', $pRectTo)
    If @error Then Return SetError(1, 0, 0)
    Return $aResult[0]
EndFunc

so where would i have the hide button in this code?

Share this post


Link to post
Share on other sites

Basically what you need to achieve to minimize to tray is the animation. You decide whether upon clicking the close (x button) or the minimize button or to show or hide the window. All that this script does it to retrieve the two rectangles required for the _WinAPI_DrawAnimatedRects() function. You can do something like, but it won't run just like that:

Global $fMinimized = False
Global $hGUI, $HideBtn

$hGUI = GUICreate('Application', 400, 360)
$HideBtn = GUICtrlCreateButton('Minimize', 10, 20, 70, 23)

; ...

While 1
    Switch GUIGetMsg()
        Case $HideBtn
            _TrayAnimator()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _TrayAnimator()
    Local $tRcFrom , $tRcTo
    
    If $fMinimized Then
        $tRcFrom = _WinAPI_GetWindowRect($hTray)
        $tRcTo = _WinAPI_GetWindowRect($hGUI)
        _WinAPI_DrawAnimatedRects($hGUI, $IDANI_CAPTION, DllStructGetPtr($tRcFrom), DllStructGetPtr($tRcTo))
        GUISetState(@SW_SHOW)
        $fMinimized = False
    Else
        $tRcFrom = _WinAPI_GetWindowRect($hGUI)
        $tRcTo = _WinAPI_GetWindowRect($hTray)
        _WinAPI_DrawAnimatedRects($hGUI, $IDANI_CAPTION, DllStructGetPtr($tRcFrom), DllStructGetPtr($tRcTo))
        GUISetState(@SW_HIDE)
        $fMinimized = True
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

If you just want to hide it.

Just use

GUISetState ( @SW_HIDE )

Be sure the icon is visible!

And create a trayitem with something like "open"

And if that trayitem is clicked use:

GUISetState ( @SW_SHOW )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0