Jump to content
Sign in to follow this  
steveR

Minimize to tray

Recommended Posts

steveR

I have this sample code which toggles the gui to the tray. How can I have it minimize to the tray when the gui loses focus (e.g., switching to another program, or clicking off the gui)? I've tried several commands, but I keep getting conflicts.

#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
;
$SHOW = 1
$HIDE = 0
$appState = $SHOW
;
$guiQuestion = GUICreate( "Question", 200, 200)
GUISetState()
;
;
Do
    $guiMsg = GUIGetMsg()
    $trayMsg = TrayGetMsg()
;
    Select
    case $trayMsg = $TRAY_EVENT_PRIMARYDOWN Or $trayMsg = $TRAY_EVENT_PRIMARYDOUBLE
        _ToggleAppState()
    EndSelect
;
until $guiMsg = $GUI_EVENT_CLOSE
;
;
Func _ToggleAppState()
    if $appState = $HIDE then
        $appState = $SHOW
        GUISetState(@SW_SHOW, $guiQuestion)
        Return
    EndIf
        $appState = $HIDE
        GUISetState(@SW_HIDE, $guiQuestion)
EndFunc

AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.

Share this post


Link to post
Share on other sites
SlimShady

If NOT WinActive($guiQuestion) Then;Do whatever you want

Share this post


Link to post
Share on other sites
zcoacoaz

you might want to set a variable and if it is already minimized it doesn't do it again.


[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Share this post


Link to post
Share on other sites
steveR

Ok, this is where i have the problem. It now goes to the tray if the gui loses focus, and it will activate if I click the tray. But when I try to minimize by clicking the tray, it seems to fire twice. Once to minimize it, then once to bring it back up.

In summary, I want to be able to minimize and raise it with the tray icon, or minimize box. Also I want it to minimize when the gui loses focus.

#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
;
$YES = 1
$NO = 0
$focused = $YES
$SHOW = 1
$HIDE = 0
$appState = $SHOW
;
$guiQuestion = GUICreate( "Question", 200, 200)
GUISetState()
;
;
Do
    $guiMsg = GUIGetMsg()
    $trayMsg = TrayGetMsg()
;
    if Not WinActive("Question") Then 
            if $focused = $YES Then
                $appState = $SHOW
                _ToggleAppState()
            EndIf
    EndIf
;
    Select
        GUISetState(@SW_HIDE, $guiQuestion)
    case $trayMsg = $TRAY_EVENT_PRIMARYDOWN Or $trayMsg = $TRAY_EVENT_PRIMARYDOUBLE
        _ToggleAppState()
    EndSelect
;
until $guiMsg = $GUI_EVENT_CLOSE
;
;
Func _ToggleAppState()
    if $appState = $HIDE then
        $appState = $SHOW
        $focused = $YES
        GUISetState(@SW_SHOW, $guiQuestion)
        Return
    EndIf
        $appState = $HIDE
        $focused = $NO
        GUISetState(@SW_HIDE, $guiQuestion)
EndFunc
;
;
Edited by steveR

AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.

Share this post


Link to post
Share on other sites
Holger

A little bit shorted:

#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
;
;
$guiQuestion = GUICreate( "Question", 200, 200)
GUISetState()
$appState = @SW_SHOW
;
;
Do
    $guiMsg     = GUIGetMsg()
    $trayMsg    = TrayGetMsg()

    if Not WinActive("Question") And $appState = @SW_SHOW Then
        _ToggleAppState()
    EndIf

    Select
        case $trayMsg = $TRAY_EVENT_PRIMARYDOWN
            _ToggleAppState()
    EndSelect
;
until $guiMsg = $GUI_EVENT_CLOSE
;
;
Func _ToggleAppState()
    if $appState = @SW_HIDE then
        $appState = @SW_SHOW
        GUISetState(@SW_SHOW, $guiQuestion)
    Else
        $appState = @SW_HIDE
        GUISetState(@SW_HIDE, $guiQuestion)
    EndIf
EndFunc
;
;

Regards :)

Holger

Edit Did forget something, the behaviour was normal cause:

- when you click on the tray icon then the window loses the focus automatically -> and so it will be hidden

- but then there is still a click message in the queue which then toggles automatically the window state to show it

So the script is not fully working...

Edited by Holger

Share this post


Link to post
Share on other sites
steveR

Thanks for the effort Holger, but i still have the problem of it firing twice. Here i added traytips and sleep() to help see it better

#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
;
$YES = 1
$NO = 0
$focused = $YES
$SHOW = 1
$HIDE = 0
$appState = $SHOW
;
$guiQuestion = GUICreate( "Question", 200, 200)
GUISetState()
;
;
Do
    $guiMsg = GUIGetMsg()
    $trayMsg = TrayGetMsg()
;
    if Not WinActive("Question") Then 
            if $focused = $YES Then
                $appState = $SHOW
                _ToggleAppState()
            EndIf
    EndIf
;
    Select
        GUISetState(@SW_HIDE, $guiQuestion)
    case $trayMsg = $TRAY_EVENT_PRIMARYDOWN Or $trayMsg = $TRAY_EVENT_PRIMARYDOUBLE
        _ToggleAppState()
    EndSelect
;
until $guiMsg = $GUI_EVENT_CLOSE
;
;
Func _ToggleAppState()
    if $appState = $HIDE then
        traytip("showing"," ", 1)
        sleep(1000)
        $appState = $SHOW
        $focused = $YES
        GUISetState(@SW_SHOW, $guiQuestion)
        Return
    EndIf
    traytip("hiding"," ", 1)
    sleep(1000)
    $appState = $HIDE
        $focused = $NO
        GUISetState(@SW_HIDE, $guiQuestion)
EndFunc
;
;

AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.

Share this post


Link to post
Share on other sites
Holger

Try this:

;
#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
;
$guiQuestion = GUICreate( "Question", 200, 200)
GUISetState()
$appState = @SW_SHOW
$clicked = 0
;
;
Do
    $guiMsg = GUIGetMsg()
    $trayMsg = TrayGetMsg()

    If $appstate = @SW_SHOW And Not WinActive("Question") Then
        _ToggleAppState()
        $clicked = 1
    EndIf
    
    Select
        Case $trayMsg = $TRAY_EVENT_PRIMARYDOWN Or $trayMsg = $TRAY_EVENT_PRIMARYDOUBLE
            If $clicked = 0 Then
                _ToggleAppState()
                $clicked = 1
            Else
                $clicked = 0
            EndIf
    EndSelect
;
until $guiMsg = $GUI_EVENT_CLOSE
;
;
Func _ToggleAppState()
    If $appState = @SW_HIDE then
        TrayTip("showing"," ", 1)
        GUISetState(@SW_SHOW, $guiQuestion)
        $appState = @SW_SHOW
    Else
        TrayTip("hiding"," ", 1)
        GUISetState(@SW_HIDE, $guiQuestion)
        $appState = @SW_HIDE
    EndIf
EndFunc
;
;

Regards

Holger

Share this post


Link to post
Share on other sites
Toppy

Where do you guys get this from ?

Only things I get when running is ERROR: Unknown function name: TrayGetMsg()

:)

Share this post


Link to post
Share on other sites
TuMbLeWeEd

Made it a lot simpler (it works on my pc)

;
#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
;
$guiQuestion = GUICreate( "Question", 200, 200)
GUISetState()
$appState = @SW_SHOW
$clicked = 0
TraySetClick(16)
;
;
Do
    $guiMsg = GUIGetMsg()
    $trayMsg = TrayGetMsg()
    if Not WinActive("Question") and $clicked = 1 Then
      ;sleep(1000);waits a second before setting to tray   
        GUISetState(@SW_HIDE, $guiQuestion)
        $clicked = 0
    EndIf
    
    Select
        Case $trayMsg = $TRAY_EVENT_PRIMARYDOWN or $trayMsg = $TRAY_EVENT_PRIMARYDOUBLE
            If $clicked = 0 Then
                sleep(100)
                GUISetState(@SW_SHOW, $guiQuestion)
                $clicked = 1
            Else
                GUISetState(@SW_HIDE, $guiQuestion)
                $clicked = 0
            EndIf
        Case $guiMsg = $GUI_EVENT_MINIMIZE
            GUISetState(@SW_HIDE, $guiQuestion)
            $clicked = 0
    EndSelect
;
until $guiMsg = $GUI_EVENT_CLOSE

EDIT: and its stil not minimizing on tray click, damn (then i have got the same prob in my app's)

Edited by TuMbLeWeEd

Share this post


Link to post
Share on other sites
SlimShady

This one is working better.

#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
;
$guiQuestion = GUICreate( "Question", 200, 200)
GUISetState()
$hidden = 0
TraySetClick(16)
;
;
Do
    $guiMsg = GUIGetMsg()
    $trayMsg = TrayGetMsg()

    Select
        Case $trayMsg = $TRAY_EVENT_PRIMARYDOWN
            If $hidden Then
                sleep(100)
                GUISetState(@SW_SHOW, $guiQuestion)
            Else
                GUISetState(@SW_HIDE, $guiQuestion)
            EndIf
            $hidden = NOT $hidden
            
        Case $guiMsg = $GUI_EVENT_MINIMIZE
            GUISetState(@SW_HIDE, $guiQuestion)
            $hidden = 1
            
    EndSelect
;
until $guiMsg = $GUI_EVENT_CLOSE

Share this post


Link to post
Share on other sites
TuMbLeWeEd

You forgot his primary intension, he want to minimize to the tray if the window loses the focus

Lookt at it for a few minits a realized that you should include the minimize to tray on focus los in the select case like this

;
#include <guiconstants.au3>
#include <constants.au3>
;
Opt ("TrayAutoPause", 0)
Opt ("TrayMenuMode", 1)
;
$guiQuestion = GUICreate( "Question", 200, 200)
GUISetState()
$clicked = 1;window is at state show at launch
TraySetClick(16)
;
;
Do
    $guiMsg = GUIGetMsg()
    $trayMsg = TrayGetMsg()
    
    Select
        Case Not WinActive("Question") and $clicked = 1
            ConsoleWrite("Lost focus minimize" & @lf    )
            GUISetState(@SW_HIDE, $guiQuestion)
            $clicked = 0
 
        Case $trayMsg = $TRAY_EVENT_PRIMARYDOUBLE;dont use single event (still gets confused then)
            ConsoleWrite("Tray clicked" & @lf   )
            If $clicked = 0 Then
                GUISetState(@SW_SHOW, $guiQuestion)
                $clicked = 1
                ConsoleWrite("Show from tray" & @lf )
            EndIf
        Case $guiMsg = $GUI_EVENT_MINIMIZE
            ConsoleWrite("Minimize in window" & @lf )
            GUISetState(@SW_HIDE, $guiQuestion)
            $clicked = 0
    EndSelect
;
until $guiMsg = $GUI_EVENT_CLOSE

It is that simple, but it takes a while to figure out

Edited by TuMbLeWeEd

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.