Jump to content

Minimize to tray


steveR
 Share

Recommended Posts

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.
Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 4 weeks later...

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

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

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
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...