Jump to content

Minimize to tray + TrayCreateItem


johnmcloud
 Share

Recommended Posts

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

×
×
  • Create New...