Jump to content
Sign in to follow this  
johnmcloud

Minimize to tray + TrayCreateItem

Recommended Posts

johnmcloud

Hi guys, i have this script:

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

Global $About, $Exit

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)

TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent")
TraySetOnEvent($TRAY_EVENT_SECONDARYUP,"TrayMenu")

GUICreate("TestGUI", 392, 316, -1, -1)
GUISetState()

While 1
$msg = GUIGetMsg()
Select
  Case $msg = $GUI_EVENT_MINIMIZE
   GUISetState(@SW_HIDE)
   TraySetState(1) ; show
  Case $msg = $GUI_EVENT_CLOSE
   ExitLoop
EndSelect
WEnd

Func TrayMenu()
Local $About = TrayCreateItem("About")
TrayCreateItem("") ; Create a separator line.
Local $Exit = TrayCreateItem("Exit")
While 1
  Switch TrayGetMsg()
     Case $About
        MsgBox(0,0,"Test")
     Case $Exit ; Exit the loop.
        ExitLoop
        EndSwitch
    WEnd
EndFunc

Func SpecialEvent()
GUISetState(@SW_SHOW)
TraySetState(2) ; hide
EndFunc   ;==>SpecialEvent

I want to minimize the GUI ( work ) and, when minimized, add a menu to the tray icon ( i see the menù but don't work )

Second question, i have a Func with a MsgBox at the end. I'd like to have a MsgBox only if the GUI isn't minimized, so how to check the status of the GUI? Example if not minimized do a MsgBox, if is minimized do a TrayTip.

Thanks for support :oops:

Edited by johnmcloud

Share this post


Link to post
Share on other sites
Melba23

johnmcloud,

You were mixing TraySetOnEvent and TrayGetMsg - just as with the GUI versions you can only have one or the other. You were also missing the TraySetClick function to limit when the tray menu would appear - that is why your TraySetOnEvent did not work. :bye:

I think this is working as you want: :doh:

#NoTrayIcon
#include <guiconstantsex.au3>
#include <constants.au3>

Global $iAbout, $iExit

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)

; Create tray menu items and set the events
$About = TrayCreateItem("About")
TrayItemSetOnEvent($About, "_About")
TrayCreateItem("") ; Create a separator line.
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($Exit, "_Exit")

; Set the action if you left click on the tray icon
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent")
; Show the menu if the icon is right clicked
TraySetClick(8)

GUICreate("TestGUI", 392, 316, -1, -1)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_MINIMIZE
            ; Hide the GUI
            GUISetState(@SW_HIDE)
            ; Show the icon
            TraySetState(1)
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func SpecialEvent()
    ; Show the GUI
    GUISetState(@SW_SHOW)
    GUISetState(@SW_RESTORE)
    ; Hide the icon
    TraySetState(2)
EndFunc   ;==>SpecialEvent

Func _About()
    MsgBox(0,0,"Test")
EndFunc

Func _Exit()
    Exit
EndFunc

All clear? :oops:

M23

Edited by Melba23
Missing line in code

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
johnmcloud

Thanks Melba, but when minimized i can't see the icon :oops:

EDIT: You have forget this:

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_MINIMIZE
            ; Hide the GUI
            GUISetState(@SW_HIDE)
            ; Show the icon
            TraySetState(1) ; ------------------> LOL
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

For my second question, how to check if GUI is minimized or not?

Like:

If GUI_Minimized Then
TrayTip("Test", "", 0)
Else
MsgBox(0,0,"Test")
EndIf

Thanks for your time

Edited by johnmcloud

Share this post


Link to post
Share on other sites
Melba23

johnmcloud,

Sorry, copy-paste problem - I have fixed it now. :oops:

For the second question, look at WinGetState in the Help file. Look closely at the example - you need to use BitAnd to check as the state could be a combination of several values. :bye:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
johnmcloud

#NoTrayIcon
#include <guiconstantsex.au3>
#include <constants.au3>

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)
 
; Create tray menu items and set the events
$About = TrayCreateItem("About")
TrayItemSetOnEvent($About, "_About")
TrayCreateItem("") ; Create a separator line.
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($Exit, "_Exit")
; Set the action if you left click on the tray icon
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent")
; Show the menu if the icon is right clicked
TraySetClick(8)
 
GUICreate("TestGUI", 392, 316, -1, -1)
GUISetState()
 
Test()
 
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_MINIMIZE
            ; Hide the GUI
            GUISetState(@SW_HIDE)
            ; Show the icon
   TraySetState(1)
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func SpecialEvent()
    ; Show the GUI
    GUISetState(@SW_SHOW)
    GUISetState(@SW_RESTORE)
    ; Hide the icon
    TraySetState(2)
EndFunc   ;==>SpecialEvent

Func _About()
    MsgBox(0,0,"Test")
EndFunc

Func _Exit()
    Exit
EndFunc

Func Test()
Sleep(2000)
Local $state = WinGetState("Test GUI")
If BitAND($state, 16) Then
TrayTip("Test", "", 0)
Else
MsgBox(0,0,"Test")
EndIf
EndFunc

No problem Melba, even the best make mistakes sometimes :oops:

But seems minimized to tray not work with the Test() func...

Edited by johnmcloud

Share this post


Link to post
Share on other sites
Aipion

#NoTrayIcon
#include <guiconstantsex.au3>
#include <constants.au3>

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)

; Create tray menu items and set the events
$About = TrayCreateItem("About")
TrayItemSetOnEvent($About, "_About")
TrayCreateItem("") ; Create a separator line.
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($Exit, "_Exit")
; Set the action if you left click on the tray icon
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent")
; Show the menu if the icon is right clicked
TraySetClick(8)

GUICreate("TestGUI", 392, 316, -1, -1)
GUISetState()

Test()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_MINIMIZE
            ; Hide the GUI
            GUISetState(@SW_HIDE)
            ; Show the icon
   TraySetState(1)
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func SpecialEvent()
    ; Show the GUI
    GUISetState(@SW_SHOW)
    GUISetState(@SW_RESTORE)
    ; Hide the icon
    TraySetState(2)
EndFunc   ;==>SpecialEvent

Func _About()
    MsgBox(0,0,"Test")
EndFunc

Func _Exit()
    Exit
EndFunc

Func Test()
Sleep(2000)
Local $state = WinGetState("Test GUI")
If BitAND($state, 16) Then
TrayTip("Test", "", 0)
Else
MsgBox(0,0,"Test")
EndIf
EndFunc

No problem Melba, even the best make mistakes sometimes :oops:

But seems minimized to tray not work with the Test() func...

Try this :

#NoTrayIcon
#include <guiconstantsex.au3>
#include <constants.au3>
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)
; Create tray menu items and set the events
$About = TrayCreateItem("About")
TrayItemSetOnEvent($About, "_About")
TrayCreateItem("") ; Create a separator line.
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($Exit, "_Exit")
; Set the action if you left click on the tray icon
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent")
; Show the menu if the icon is right clicked
TraySetClick(8)
Global $MainGui = GUICreate("TestGUI", 392, 316, -1, -1)
GUISetState()
Test()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_MINIMIZE
            ; Hide the GUI
            GUISetState(@SW_HIDE)
            ; Show the icon
   TraySetState(1)
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd
Func SpecialEvent()
    ; Show the GUI
    GUISetState(@SW_SHOW)
    GUISetState(@SW_RESTORE)
    ; Hide the icon
    TraySetState(2)
EndFunc   ;==>SpecialEvent
Func _About()
    MsgBox(0,0,"Test")
EndFunc
Func _Exit()
    Exit
EndFunc
Func Test()
Sleep(2000)
Local $state = WinGetState($MainGui)
If BitAND($state, 16) Then
TrayTip("Test", "", 0)
MsgBox(0,0,"Min 1")
Else
MsgBox(0,0,"Min 0")
EndIf
EndFunc

Share this post


Link to post
Share on other sites
johnmcloud

Not work Aipion, the problem is, when i click on minimize button, the gui don't minimize to tray but minimize to startbar, so i have always the "Min 1". If i remove the Test() func working

Edited by johnmcloud

Share this post


Link to post
Share on other sites
Melba23

johnmcloud,

Your GUI is not "MINIMIZED" - it is "HIDDEN". :bye:

Try this - you can test via the button and the tray menu: :doh:

#NoTrayIcon
#include <guiconstantsex.au3>
#include <constants.au3>

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)

; Create tray menu items and set the events
$About = TrayCreateItem("About")
TrayItemSetOnEvent($About, "_About")
$Test = TrayCreateItem("Test")
TrayItemSetOnEvent($Test, "_Test")
TrayCreateItem("") ; Create a separator line.
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($Exit, "_Exit")
; Set the action if you left click on the tray icon
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent")
; Show the menu if the icon is right clicked
TraySetClick(8)

GUICreate("TestGUI", 392, 316, -1, -1)

$cButton = GUICtrlCreateButton("Test", 10, 10, 80, 30)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_MINIMIZE
            ; Hide the GUI
            GUISetState(@SW_HIDE)
            ; Show the icon
            TraySetState(1)
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $cButton
            _Test()
    EndSwitch
WEnd

Func SpecialEvent()
    ; Show the GUI
    GUISetState(@SW_SHOW)
    GUISetState(@SW_RESTORE)
    ; Hide the icon
    TraySetState(2)
EndFunc   ;==>SpecialEvent

Func _About()
    MsgBox(0, 0, "Test")
EndFunc   ;==>_About

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _Test()
    If BitAND(WinGetState("TestGUI"), 2) Then
        MsgBox(0, "GUI State", "Visible")
    Else
        MsgBox(0, "GUI State", "Hidden")
    EndIf
EndFunc   ;==>Test

Happy now? :oops:

M23

Edit: You also had GUICreate("TestGUI"..) and WinGetState("Test GUI") which did not help either. ;)

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
johnmcloud

Mmm...not :bye:

If i add to the func a sample sleep like this:

Func _Test()
    Sleep(2000)
    If BitAND(WinGetState("TestGUI"), 2) Then
        MsgBox(0, "GUI State", "Visible")
    Else
        MsgBox(0, "GUI State", "Hidden")
    EndIf
EndFunc   ;==>Test

Or a Do Until, i can't see the icon on tray and i see the windows on startbar when i click on minimize button. Sorry for my not perfect english, hope you understand :oops:

EDIT. Check the image, maybe is more clear:

Posted Image

The GUI disappear and the tray icon apperars after 2 sec, so when sleep finish

Edited by johnmcloud

Share this post


Link to post
Share on other sites
Aipion

Mmm...not :bye:

If i add to the func a sample sleep like this:

Func _Test()
    Sleep(2000)
    If BitAND(WinGetState("TestGUI"), 2) Then
        MsgBox(0, "GUI State", "Visible")
    Else
        MsgBox(0, "GUI State", "Hidden")
    EndIf
EndFunc   ;==>Test

Or a Do Until, i can't see the icon on tray and i see the windows on startbar when i click on minimize button. Sorry for my not perfect english, hope you understand :oops:

Do not add any loop or sleep because it will wait until the function has finished and then up date the icon. Edited by Aipion

Share this post


Link to post
Share on other sites
Melba23

johnmcloud,

When I minimize the GUI I get no taskbar icon and the tray icon appears. That Sleep will only affect the timing of the "Visible/Hidden" MsgBox. :oops:

Please post the whole script you are using - there must be something else happening. :bye:

M23

Edited by Melba23
Fixed tags

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
johnmcloud

Melba, it's only your script of post#9, nothing else :oops: ( except the Sleep )

Edited by johnmcloud

Share this post


Link to post
Share on other sites
Melba23

johnmcloud,

So when you run this script:

#NoTrayIcon
#include <guiconstantsex.au3>
#include <constants.au3>

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)

; Create tray menu items and set the events
$About = TrayCreateItem("About")
TrayItemSetOnEvent($About, "_About")
$Test = TrayCreateItem("Test")
TrayItemSetOnEvent($Test, "_Test")
TrayCreateItem("") ; Create a separator line.
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($Exit, "_Exit")
; Set the action if you left click on the tray icon
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent")
; Show the menu if the icon is right clicked
TraySetClick(8)

GUICreate("TestGUI", 392, 316, -1, -1)

$cButton = GUICtrlCreateButton("Test", 10, 10, 80, 30)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_MINIMIZE
            ; Hide the GUI
            GUISetState(@SW_HIDE)
            ; Show the icon
            TraySetState(1)
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $cButton
            _Test()
    EndSwitch
WEnd

Func SpecialEvent()
    ; Show the GUI
    GUISetState(@SW_SHOW)
    GUISetState(@SW_RESTORE)
    ; Hide the icon
    TraySetState(2)
EndFunc   ;==>SpecialEvent

Func _About()
    MsgBox(0, 0, "Test")
EndFunc   ;==>_About

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _Test()
    Sleep(2000)
    If BitAND(WinGetState("TestGUI"), 2) Then
        MsgBox(0, "GUI State", "Visible")
    Else
        MsgBox(0, "GUI State", "Hidden")
    EndIf
EndFunc   ;==>Test

and mimimize the GUI, you still see a taskbar button and no tray icon? :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
johnmcloud

Posted Image

If i click on OK i see the trayicon and the remove the taskbar, but only after that click.

I'm do this:

1) Run script

2) Click on button "Test"

3) Minimize the GUI

4) --> See image posted <--

5) Click OK, i have the trayicon and no TestGUI tasbar

Edited by johnmcloud

Share this post


Link to post
Share on other sites
Melba23

johnmcloud,

I imagine you did this:

- 1. Press the button on the GUI.

- 2. Minimize the GUI.

Remember that MsgBox is a blocking function so the script is halted until it returns. Once you have pressed the button, you remain in the _Test function until you press the "OK" button on the MsgBox. The "Minimize" button press on the GUI is queued until that point, but Windows still actions it immediately and the GUI is indeed minimized although still visible on the taskbar. It is only when the _Test function ends that your script will honour the $GUI_EVENT_MINIMIZE event and hide the GUI (removing it from the taskbar) and show the tray icon. Do you follow that? :oops:

The way to prevent this is to add Opt("GUIEventOptions", 1) at the top of your script. Now Windows will no longer action the standard $GUI_EVENT_* events. Try that and see if it does what you wish. :bye:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
johnmcloud

Melba, i have check it out but i think this time i'm not using this for my script, on main script i have loop and msgbox on every function, the goal was minimize the GUI if user want a silent script, without info/progress/error etc. with only traytip on end of every function

But seems not work like this, so for this time I let it go and leave the GUI without a minimize/silent function :bye:

Thanks for you time and your help :oops:

Share this post


Link to post
Share on other sites
johnmcloud

Melba ( and other who want to help me ) i have found a UDF who make the script like want, so work also with sleep and MsgBox:

The script:

#include <_MinimizeToTray.au3> ; external UDF
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>

$About = TrayCreateItem("About")
TrayItemSetOnEvent($About, "_About")
TrayCreateItem("") ; Create a separator line.
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($Exit, "_Exit")
; Show the menu if the icon is right clicked
TraySetClick(8)

$GUI = GUICreate("TestGUI", 401, 301, -1, -1)
$Button = GUICtrlCreateButton("Check Status", 8, 8, 89, 33)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
_MinimizeToTray($GUI, True)
TraySetState(2)
 
GUISetState()

Test(); Check if hidden or not

While 1
    Switch GUIGetMsg()
  Case $Button
   Test()
EndSwitch
WEnd

Func Test()
    Sleep(2000)
    If BitAND(WinGetState("TestGUI"), 16) Then
        MsgBox(0, "GUI State", "Hidden")
    Else
        MsgBox(0, "GUI State", "Visible")
    EndIf
EndFunc   ;==>Test

Func _About()
    MsgBox(0,0,"Test")
EndFunc

Func _Exit()
Exit
EndFunc

The UDF

#include-once
#include <GuiConstantsEx.au3>
#include <Constants.au3>
#include <GuiToolbar.au3>
Global $g_hToolbar, $g_hGUI, $g_bTray
; #FUNCTION# ;===============================================================================
;
; Name...........: _MinimizeToTray
; Description ...: Enables a GUI to minimize to a tray icon
; Syntax.........: _MinimizeToTray($hGUI, $bTray)
; Parameters ....: $hGUI - handle to the main GUI window, as returned by GUICreate()
;      $bTray - show / hide tray icon also
; Return values .: Success - Returns True
;                 Failure - Returns False and Sets @Error to 1
; Author ........: Erik Pilsits
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
;
; ;==========================================================================================
Func _MinimizeToTray($hGUI, $bTray = False)
Local $return = False, $err = 1
$g_hToolbar = _FindToolbarWindow()
If $g_hToolbar Then
  ; these options MUST be set
  Opt("GUIOnEventMode", 1) ; OnEvent mode
  Opt("TrayOnEventMode", 1) ; ditto
  Opt("TrayMenuMode", 3) ; no default tray menu (no pause or exit), user can still create custom menu
  Opt("GUIEventOptions", 1) ; minimize, maximize, and restore only send notifications, we handle the actions
  $g_hGUI = $hGUI ; store global handle
  $g_bTray = $bTray ; change tray icon state also
  ; set min/max/restore events
  GUISetOnEvent($GUI_EVENT_MINIMIZE, "_OnEventMinimize")
  GUISetOnEvent($GUI_EVENT_MAXIMIZE, "_OnEventMaximize")
  GUISetOnEvent($GUI_EVENT_RESTORE, "_OnEventRestore")
  $return = True
  $err = 0
EndIf
Return SetError($err, 0, $return)
EndFunc
; ===================================================
; INTERNAL FUNCTIONS
; ===================================================
Func _OnEventMinimize()
; set tray restore event
TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, "_OnEventRestore")
GUISetState(@SW_MINIMIZE, $g_hGUI)
_HideToolbarButton($g_hGUI, $g_hToolbar)
If $g_bTray Then TraySetState(1) ; show tray icon
EndFunc
Func _OnEventMaximize()
; unset tray event, hide icon, and show toolbar button in case we're max'ing from tray menu or hotkey
If $g_bTray Then TraySetState(2)
TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, "")
GUISetState(@SW_MAXIMIZE, $g_hGUI)
_HideToolbarButton($g_hGUI, $g_hToolbar, False)
EndFunc
Func _OnEventRestore()
If $g_bTray Then TraySetState(2) ; hide tray icon
; unset tray event
TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, "")
GUISetState(@SW_RESTORE, $g_hGUI)
_HideToolbarButton($g_hGUI, $g_hToolbar, False)
EndFunc
Func _FindToolbarWindow()
; find explorer toolbar window
; XP+
Local $hDesktop = _WinAPI_GetDesktopWindow()
Local $hTray = _FindWindowEx($hDesktop, 0, "Shell_TrayWnd", "")
Local $hReBar = _FindWindowEx($hTray, 0, "ReBarWindow32", "")
Local $hTask = _FindWindowEx($hReBar, 0, "MSTaskSwWClass", "")
Return _FindWindowEx($hTask, 0, "ToolbarWindow32", "")
EndFunc
Func _FindWindowEx($hParent, $hChild, $sClass, $sWindow)
; must create structs and use ptrs to account for passing a true NULL as classname or window title
; simply using "wstr" and "" does NOT work
Local $s1, $s2
If $sClass == "" Then
  $sClass = 0
Else
  $s1 = DllStructCreate("wchar[256]")
  DllStructSetData($s1, 1, $sClass)
  $sClass = DllStructGetPtr($s1)
EndIf
If $sWindow == "" Then
  $sWindow = 0
Else
  $s2 = DllStructCreate("wchar[256]")
  DllStructSetData($s2, 1, $sWindow)
  $sWindow = DllStructGetPtr($s2)
EndIf
Local $ret = DllCall("user32.dll", "hwnd", "FindWindowExW", "hwnd", $hParent, "hwnd", $hChild, "ptr", $sClass, "ptr", $sWindow)
$s1 = 0
$s2 = 0
Return $ret[0]
EndFunc
Func _FindToolbarButton($hwnd, $hTB)
; finds and returns the toolbar button ID for given window handle
Local $return = -1
Local $s = DllStructCreate("ptr")
; open process owning toolbar control
Local $PID
_WinAPI_GetWindowThreadProcessId($hTB, $PID)
Local $hProcess = _WinAPI_OpenProcess(0x410, False, $PID)
If $hProcess Then
  Local $count = _GUICtrlToolbar_ButtonCount($hTB)
  For $i = 0 To $count - 1
   Local $ID = _GUICtrlToolbar_IndexToCommand($hTB, $i)
   ; button param is ptr to owner's window handle, stored in target process's memory space :)
   Local $dwData = _GUICtrlToolbar_GetButtonParam($hTB, $ID)
   ; read the window handle from the explorer process
   Local $ret = DllCall("kernel32.dll", "int", "ReadProcessMemory", "ptr", $hProcess, "ptr", $dwData, "ptr", DllStructGetPtr($s), "uint", 4, "uint*", 0)
   If $ret[5] Then
    If $hwnd == DllStructGetData($s, 1) Then
     $return = $ID
     ExitLoop
    EndIf
   EndIf
  Next
  _WinAPI_CloseHandle($hProcess)
EndIf
Return $return
EndFunc
Func _HideToolbarButton($hwnd, $hTB, $hide = True)
; hides / shows a window's toolbar button
Local $ID = _FindToolbarButton($hwnd, $hTB)
If $ID <> -1 Then
  _GUICtrlToolbar_HideButton($hTB, $ID, $hide)
EndIf
EndFunc

I have a problem, the button don't work, i don't know why

Thanks for support :oops:

Edited by johnmcloud

Share this post


Link to post
Share on other sites
Melba23

johnmcloud,

Please READ the UDF - it tells you why your button does not work! :oops:

From the UDF:

; these options MUST be set
Opt("GUIOnEventMode", 1) ; OnEvent mode

Form your script:

Switch GUIGetMsg()
    Case $Button

Slight mismatch there! :bye:

I told you earlier in this very thread that you cannot mix modes (or not without very careful coding). If you find UDFs in the forum, you really do need to make sure that you can use them by READING them and understanding roughly how they work - just using them blindly can lead you into trouble. :doh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
johnmcloud

You have right Melba, with GUIOnEventMode the result is the same of the first script. I give up :oops:

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  

  • Similar Content

    • nitekram
      By nitekram
      I have tried _GUIToolTip_AddTool() & GUICtrlSetTip() & TrayItemGetHandle()  but have had no luck...is there a way that I have missed, or is it not possible to create a ToolTip for an individual item in the Tray?
    • nitekram
      By nitekram
      Looked around, but could not find anything, and thought I would just ask if it was possible. Looking to pop a png or jpg file on hover over a TrayCreateItem - is that possible (similar to HTML, where you hover over an item, and either a larger pic or a different pic shows while hover (just as a reference to what I am looking to achieve)?
    • TrickyDeath
      By TrickyDeath
      Hello everyone,
      i do have a simple question what i do not realy understand what hapend and why.
      I did made a small program, what is converting Valuta prices to other type, cause i got tired of use all the time google for it. After that i "convert" it into an exe file. All the Valute prices are in INI file, and work perfect.
      I did made another small script (Tray menu) and then when i click on the proper TrayItem it should run the Valute exchanger exe file, but something is wrong. The exe is running, but when i calculate with it, it give me only 0 resoults. If i run the exe manualy it is work properly. How does it come, from Tray menu item wont work, and from manualy run it work hot it have to work?
      Case $iValutaCalculator Run("C:\Users\asd\Desktop\EU_USD.exe") Best Regards,
      Tricky
    • johnmcloud
      By johnmcloud
      Hi guys, i have this script:


      #NoTrayIcon Opt("TrayMenuMode",1) $prefsitem = TrayCreateItem("Preferences") TrayCreateItem("") $aboutitem = TrayCreateItem("About") TrayCreateItem("") $exititem = TrayCreateItem("Exit") TraySetState() While 1 $msg = TrayGetMsg() Select Case $msg = 0 ContinueLoop Case $msg = $prefsitem Msgbox(64, "Preferences:", "Test") Case $msg = $aboutitem Msgbox(64, "About:", "Test") Case $msg = $exititem ExitLoop EndSelect WEnd Exit
      From help, i see there is only two option for TrayCreateItem, "Normal" and "Radio"
      It's possible to have a version without the radio and the flag?
      Thanks for support
×