Jump to content

Minimize to tray UDF?


Diana (Cda)
 Share

Recommended Posts

There are so many scripts for minimizing a GUI to the tray but they're all rather long. I thought it would be simpler to just do something along of calling something like _Minimize2Tray() from a UDF. Has anyone ever used that approach? I searched for a UDF but no-one has posted anything like that; however, it's often the case that the search function doesn't bring up everything. So I thought I'd ask instead of assuming that there isn't anything like this just because I can't find it.

Thanks. <g>

Link to comment
Share on other sites

There are so many scripts for minimizing a GUI to the tray but they're all rather long. I thought it would be simpler to just do something along of calling something like _Minimize2Tray() from a UDF. Has anyone ever used that approach? I searched for a UDF but no-one has posted anything like that; however, it's often the case that the search function doesn't bring up everything. So I thought I'd ask instead of assuming that there isn't anything like this just because I can't find it.

Thanks. <g>

This is a short and good example.

When the words fail... music speaks.

Link to comment
Share on other sites

  • Moderators

Here's a fun way I thought to do it:

Opt("GUIEventOptions", 1)

Global $h_gui = GuiCreate("Minimize Test")
GUISetState()
    
Global $a_last_pos = 0
While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $GUI_EVENT_MINIMIZE
            $a_last_pos = _Win_Minimize_ToTray($h_gui)
            Sleep(3000)
            _Win_Restore_FromTray($h_gui, $a_last_pos)
    EndSwitch
WEnd

Func _Win_Restore_FromTray($h_wnd, $a_position = -1)
    If IsArray($a_position) Then
        DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $h_wnd, "dword", 100, "dword", BitOR(0x002, 0x0040000, 0x008))
        WinMove($h_wnd, "", $a_position[0], $a_position[1], $a_position[2], $a_position[3])
        Return
    EndIf
    WinSetState($h_wnd, "", @SW_SHOW)
    WinSetState($h_wnd, "", @SW_RESTORE)
    Return
EndFunc

Func _Win_Minimize_ToTray($h_wnd)
    Local Const $AW_SLIDE        =  0x0040000
    Local Const $AW_ACTIVATE      =  0x0020000
    Local Const $AW_BLEND        =  0x0080000
    Local Const $AW_HIDE          =  0x0010000
    Local Const $AW_CENTER      =  0x0010
    Local Const $AW_HOR_POSITIVE  =  0x001
    Local Const $AW_HOR_NEGATIVE  =  0x002
    Local Const $AW_VER_POSITIVE  =  0x004
    Local Const $AW_VER_NEGATIVE  =  0x008
    
    If Opt("GUIEventOptions") Then
        Local $a_wpos = WinGetPos($h_wnd)
        If Not @error Then
            Local $a_cpos = ControlGetPos("[CLASS:Shell_TrayWnd]", "", "ReBarWindow321")
            If Not @error Then
        
                Local $i_x = (@DesktopWidth - $a_wpos[2])
                Local $i_y = @DesktopHeight - ($a_wpos[3] + $a_cpos[3])
                WinMove($h_wnd, "", $i_x, $i_y)
        
                DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $h_wnd, "dword", 100, "dword", BitOR($AW_HIDE, $AW_SLIDE, $AW_HOR_POSITIVE, $AW_VER_POSITIVE))
                WinSetState($h_wnd, "", @SW_HIDE)
                Return $a_wpos
            EndIf
        EndIf
    EndIf
    
    While Not BitAND(WinGetState($h_wnd), 16)
        Sleep(10)
    WEnd
    
    WinSetState($h_wnd, "", @SW_HIDE)
    Return
EndFunc
Now you'd have to plugin the Tray stuff, I only showed examples of how to use it.

To see both effects, comment and uncomment Opt("GUIEventOptions", 1).

Edited by SmOke_N
Changed restore

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Nice example SmOke_N. She just need to add GUIConstantsEx.au3 if isn't included in script before.

Well there seems to be an issue I didn't notice before... It isn't hiding the window once I use $AW_HIDE .. kind of ruins the effect not using it though...

I tried changing the windows long ex style value to: $WS_EX_TOOLWINDOW ... but that's not working either.

CODE
#include <GUIConstantsEx.au3>

Opt("GUIEventOptions", 1)

Global $h_gui = GuiCreate("Minimize Test")

GUISetState()

Global $a_last_pos = 0

While 1

Switch GUIGetMsg()

Case -3

Exit

Case $GUI_EVENT_MINIMIZE

$a_last_pos = _Win_Minimize_ToTray($h_gui)

Sleep(3000)

_Win_Restore_FromTray($h_gui, $a_last_pos)

EndSwitch

WEnd

Func _Win_Restore_FromTray($h_wnd, $a_position = -1)

If IsArray($a_position) Then

If $a_position[4] Then

DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $h_wnd, "int", -20, "long", $a_position[4])

EndIf

DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $h_wnd, "dword", 100, "dword", BitOR(0x002, 0x0040000, 0x008))

WinMove($h_wnd, "", $a_position[0], $a_position[1], $a_position[2], $a_position[3])

Return

EndIf

WinSetState($h_wnd, "", @SW_SHOW)

WinSetState($h_wnd, "", @SW_RESTORE)

Return

EndFunc

Func _Win_Minimize_ToTray($h_wnd)

Local Const $AW_SLIDE = 0x0040000

Local Const $AW_ACTIVATE = 0x0020000

Local Const $AW_BLEND = 0x0080000

Local Const $AW_HIDE = 0x0010000

Local Const $AW_CENTER = 0x0010

Local Const $AW_HOR_POSITIVE = 0x001

Local Const $AW_HOR_NEGATIVE = 0x002

Local Const $AW_VER_POSITIVE = 0x004

Local Const $AW_VER_NEGATIVE = 0x008

If Opt("GUIEventOptions") Then

Local $a_wpos = WinGetPos($h_wnd)

If Not @error Then

Local $a_ret[5] = [$a_wpos[0], $a_wpos[1], $a_wpos[2], $a_wpos[3], 0]

Local $a_cpos = ControlGetPos("[CLASS:Shell_TrayWnd]", "", "ReBarWindow321")

If Not @error Then

Local $i_x = (@DesktopWidth - $a_wpos[2])

Local $i_y = @DesktopHeight - ($a_wpos[3] + $a_cpos[3])

WinMove($h_wnd, "", $i_x, $i_y)

Local $a_winlong = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $h_wnd, "int", -20)

If Not BitAND($a_winlong[0], 0x00000080) Then

DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $h_wnd, "int", -20, "long", 0x00000080)

$a_ret[4] = $a_winlong[0]

EndIf

DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $h_wnd, "dword", 100, "dword", BitOR($AW_HIDE, $AW_SLIDE, $AW_HOR_POSITIVE, $AW_VER_POSITIVE))

Return $a_ret

EndIf

EndIf

EndIf

While Not BitAND(WinGetState($h_wnd), 16)

Sleep(10)

WEnd

WinSetState($h_wnd, "", @SW_HIDE)

Return

EndFunc

I have not more time to play with it... maybe someone can enhance it (or actually make that part work lol ... )

On GUIConstantsEx... Yeah, I usually use just the constant values so I forget to include the includes.

Edit:

Here's a temp fix until then:

CODE
#include <GUIConstantsEx.au3>

Opt("GUIEventOptions", 1)

Global $h_gui = GuiCreate("Minimize Test")

GUISetState()

    

Global $a_last_pos = 0

While 1

    Switch GUIGetMsg()

        Case -3

            Exit

        Case $GUI_EVENT_MINIMIZE

            $a_last_pos = _Win_Minimize_ToTray($h_gui)

            Sleep(3000)

            _Win_Restore_FromTray($h_gui, $a_last_pos)

    EndSwitch

WEnd

Func _Win_Restore_FromTray($h_wnd, $a_position = -1)

    If IsArray($a_position) Then

;~         WinMove($h_wnd, "", $a_position[4], $a_position[5])

;~         WinSetState($h_wnd, "", @SW_SHOW)

;~         DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $h_wnd, "dword", 20, "dword", BitOR(0x002, 0x0040000, 0x008))

        WinMove($h_wnd, "", $a_position[0], $a_position[1], $a_position[2], $a_position[3])

        Return

    EndIf

    WinSetState($h_wnd, "", @SW_SHOW)

    WinSetState($h_wnd, "", @SW_RESTORE)

    Return

EndFunc

Func _Win_Minimize_ToTray($h_wnd)

    Local Const $AW_SLIDE         =  0x0040000

    Local Const $AW_ACTIVATE      =  0x0020000

    Local Const $AW_BLEND         =  0x0080000

    Local Const $AW_HIDE          =  0x0010000

    Local Const $AW_CENTER        =  0x0010

    Local Const $AW_HOR_POSITIVE  =  0x001

    Local Const $AW_HOR_NEGATIVE  =  0x002

    Local Const $AW_VER_POSITIVE  =  0x004

    Local Const $AW_VER_NEGATIVE  =  0x008

    

    If Opt("GUIEventOptions") Then

        Local $a_wpos = WinGetPos($h_wnd)

        If Not @error Then

            Local $a_cpos = ControlGetPos("[CLASS:Shell_TrayWnd]", "", "ReBarWindow321")

            If Not @error Then

                

                Local $i_x = (@DesktopWidth - $a_wpos[2])

                Local $i_y = @DesktopHeight - ($a_wpos[3] + $a_cpos[3])

                Local $a_ret[6] = [$a_wpos[0], $a_wpos[1], $a_wpos[2], $a_wpos[3], $i_x, $i_y]

                

                WinMove($h_wnd, "", $i_x, $i_y)

                

                DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $h_wnd, "dword", 100, "dword", BitOR($AW_HIDE, $AW_SLIDE, $AW_HOR_POSITIVE, $AW_VER_POSITIVE))

                

            ; Move out of view and animate window again so we can hide now and restore properly later

                WinMove($h_wnd, "", -32000, -32000)

                DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $h_wnd, "dword", 0, "dword", BitOR($AW_SLIDE, $AW_VER_POSITIVE))

                

                WinSetState($h_wnd, "", @SW_HIDE)

                Return $a_ret

            EndIf

        EndIf

    EndIf

    

    While Not BitAND(WinGetState($h_wnd), 16)

        Sleep(10)

    WEnd

    

    WinSetState($h_wnd, "", @SW_HIDE)

    Return

EndFunc

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

My solution. This one is good because it doesn't hide the window. FYI, hidden windows don't receive GUIOnEventMode() messages. So a hidden window wouldn't respond to hotkeys or tray event in OnEventMode.

#include <GuiConstantsEx.au3>
#include <GuiToolbar.au3>
#include <Constants.au3>

Opt("TrayMenuMode", 1)

$gui = GUICreate("Minimize to tray example.")

GUISetState()

Do
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_MINIMIZE
            _ToolbarButtonState($gui, 0)
    EndSwitch
    $tmsg = TrayGetMsg()
    Switch $tmsg
        Case $TRAY_EVENT_PRIMARYDOWN
            If BitAND(WinGetState($gui), 16) Then ; 16 = minimized
                GUISetState(@SW_RESTORE)
                _ToolbarButtonState($gui, 1)
            EndIf
    EndSwitch
Until $msg == -3

Func _ToolbarButtonState($hwnd, $state)
    Local $wndname = WinGetTitle($hwnd)
    Local $toolbar = ControlGetHandle("[CLASS:Shell_TrayWnd]", "", "ToolbarWindow324") ; ToolbarWindow322 on WinXP
    Local $count = _GUICtrlToolbar_ButtonCount($toolbar)
    For $i = 0 To $count - 1
        Local $cID = _GUICtrlToolbar_IndexToCommand($toolbar, $i)
        If $wndname = _GUICtrlToolbar_GetButtonText($toolbar, $cID) Then
            Switch $state
                Case 0
                    _GUICtrlToolbar_HideButton($toolbar, $cID)
                Case 1
                    _GUICtrlToolbar_HideButton($toolbar, $cID, False)
            EndSwitch
        EndIf
    Next
EndFunc
Edited by wraithdu
Link to comment
Share on other sites

wraithdu

Hi! Sorry, but I don't understand, why need the _ToolbarButtonState function? Anyway this function doesn't work :). Simple example:

#NoTrayIcon

#include <GuiConstantsEx.au3>
#include <GuiToolbar.au3>
#include <Constants.au3>

Opt("TrayMenuMode", 1)

$gui = GUICreate("Minimize to tray example.")

GUISetState()

Do
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_MINIMIZE
            TraySetState(1)
    EndSwitch
    $tmsg = TrayGetMsg()
    Switch $tmsg
        Case $TRAY_EVENT_PRIMARYDOWN
            If BitAND(WinGetState($gui), 16) Then ; 16 = minimized
                GUISetState(@SW_RESTORE)
                TraySetState(2)
            EndIf
    EndSwitch
Until $msg = -3

:)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...