Jump to content
Sign in to follow this  
amin84

[Solved] Tray Menu, Main GUI and Child GUI

Recommended Posts

Hello,

I'm trying to make a tray menu that will open the mainGui and then mainGui will open a childGui.

This is what I've been using when I needed tray menu and one mainGui like settings for example:

#Include <GUIConstantsEx.au3>
#Include <WindowsConstants.au3>

Opt('TrayMenuMode',3)
Opt('TrayAutoPause',0)
Opt("GUICloseOnESC", 0)

Global $mainGui

$showMainGuiTray = TrayCreateItem('Show Main Gui...')
TrayCreateItem('')
$exitTray = TrayCreateItem('Exit')

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete($mainGui)
    EndSwitch
    $tMsg = TrayGetMsg()
    Switch $tMsg
        Case $exitTray
            Exit
        Case $showMainGuiTray
            mainGui()
    EndSwitch
WEnd

Func mainGui()
    $mainGui = GUICreate("Parent", 320, 240, -1, -1, $WS_SYSMENU)
    GUISetState(@SW_SHOW, $mainGui)
EndFunc

But I can't make it work when I need additional child GUI that will be opened by the main GUI.

I've tried different things but I don't know how to manage clicks from tray and two GUIs.

This is what I have so far but doesn't work at all!

#Include <GUIConstantsEx.au3>
#Include <WindowsConstants.au3>

Opt('TrayMenuMode',3)
Opt('TrayAutoPause',0)
Opt("GUICloseOnESC", 0)

Global $mainGui, $showChildBtn, $childGui, $hideChildBtn

$showMainGuiTray = TrayCreateItem('Show Main Gui...')
TrayCreateItem('')
$exitTray = TrayCreateItem('Exit')

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete($mainGui)
        Case $showChildBtn
            GUISetState(@SW_DISABLE, $mainGui)
            childGui()
            WinActivate($childGui)
        Case $hideChildBtn
            GUIDelete($childGui)
            GUISetState(@SW_ENABLE, $mainGui)
            WinActivate($mainGui)
    EndSwitch
    $tMsg = TrayGetMsg()
    Switch $tMsg
        Case $exitTray
            Exit
        Case $showMainGuiTray
            mainGui()
    EndSwitch
WEnd

Func mainGui()
    $mainGui = GUICreate("Parent", 320, 240, -1, -1, $WS_SYSMENU)
    $showChildBtn = GUICtrlCreateButton("Show Child", 5, 5)
    GUISetState(@SW_SHOW, $mainGui)
EndFunc

Func childGui()
    $childGui = GUICreate("child",200,200,-1,-1,$WS_EX_TOOLWINDOW,-1,$mainGui)
    $hideChildBtn = GUICtrlCreateButton("Hide Child", 5, 5)
    GUISetState(@SW_SHOW, $childGui)
EndFunc

Thanks.

Edited by leomoon

Share this post


Link to post
Share on other sites

I managed to make it work using OnEvents for tray and both GUIs.

#Include <GUIConstantsEx.au3>
#Include <WindowsConstants.au3>
#include <Constants.au3>
#include <StaticConstants.au3>

Opt('TrayMenuMode',3)
Opt("TrayOnEventMode",1)
Opt('TrayAutoPause',0)
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 0)

TraySetClick(8)

Dim $mainGui, $showChildBtn, $childGui, $hideChildBtn

$showMainGuiTray = TrayCreateItem('Show Main Gui...')
TrayItemSetOnEvent(-1, "mainGui")
TrayCreateItem('')
$exitTray = TrayCreateItem('Exit')
TrayItemSetOnEvent(-1, "_exit")
TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, '_toggle')

While 1
    Sleep(100)
WEnd

Exit

Func _toggle()
    ConsoleWrite('DOUBLE'&@LF)
EndFunc

Func mainGui()
    $mainGui = GUICreate("Parent", 320, 240, -1, -1, $WS_SYSMENU)
    $showChildBtn = GUICtrlCreateButton("Show Child", 5, 5)
    GUICtrlSetOnEvent(-1, "showChild")
    GUISetOnEvent($GUI_EVENT_CLOSE, "guiDel", $mainGui)
    GUISetState(@SW_SHOW, $mainGui)
EndFunc

Func guiDel()
    GUIDelete($mainGui)
EndFunc

Func childGui()
    $childGui = GUICreate("child",200,200,-1,-1,$WS_EX_TOOLWINDOW,$WS_EX_MDICHILD,$mainGui)
    $hideChildBtn = GUICtrlCreateButton("Hide Child", 5, 5)
    GUICtrlSetOnEvent(-1, "hideChild")
    GUISetState(@SW_SHOW, $childGui)
EndFunc

Func showChild()
    GUISetState(@SW_DISABLE, $mainGui)
    childGui()
    WinActivate($childGui)
EndFunc

Func hideChild()
    GUIDelete($childGui)
    GUISetState(@SW_ENABLE, $mainGui)
    WinActivate($mainGui)
EndFunc

Func _exit()
    Exit
EndFunc

I just don't like that Sleep(100) thing! Is there a another way to do this?

Share this post


Link to post
Share on other sites
17 hours ago, leomoon said:

I just don't like that Sleep(100) thing!

Not sure why you're tweakd by a 100ms sleep, but you could always lower it to 10ms if you really wanted to. It's just there so the script doesn't eat all of your CPU cycles while it's running.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
5 minutes ago, BrewManNH said:

It's just there so the script doesn't eat all of your CPU cycles while it's running. It's just there so the script doesn't eat all of your CPU cycles while it's running. 

I do not think he is bothered by the long delay, but due to the fact that it is required to keep the script from exiting when operating in OnEvent mode.

 

Share this post


Link to post
Share on other sites

I was wondering if there is another way of doing this without having sleep in there. And if there is no way, what is the best sleep amount that wouldn't slowdown the computer?

Thanks for the replies btw.

Share this post


Link to post
Share on other sites
6 minutes ago, leomoon said:

I was wondering if there is another way of doing this without having sleep in there.

It is not possible to prevent the script from exiting without giving it any tasks... and the task which takes the least amount of resources is a Sleep function in an infinite loop :)

8 minutes ago, leomoon said:

And if there is no way, what is the best sleep amount that wouldn't slowdown the computer? 

As I have said, Sleep takes the least amount of resources... and most of that are used in the inital phase of sleep. Once the OS has processed the sleep call, it will schedule your process accordingly so that it takes nil resources until the sleep is finished. So the most effective way is to call Sleep with the biggest possible number in AutoIt... or directly call Windows' Sleep function with the value of INFINITE.

The latter maybe harder to accomplish by yourself as it requires digging around and finding the value of the INFINITE macro.

Share this post


Link to post
Share on other sites
15 minutes ago, leomoon said:

what is the best sleep amount that wouldn't slowdown the computer?

Sleep doesn't affect the computer's speed directly. Not having one in there will affect it because your script is taking up all the free processing cycles for itself. The lowest value of sleep you can use is 10ms, and is usually sufficient to prevent any problems. The longer the sleep, the more lag you're going to see in your script, so keep it on the lower end to make sure your script is responsive, and your computer doesn't melt.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Well, in this use case it wouldn't affect it, but in general if not using OnEvent it would affect things. For example, if he had managed to fix his mistakes in his first script it would have affected the response of the GUI controls.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
14 minutes ago, BrewManNH said:

Well, in this use case it wouldn't affect it, but in general if not using OnEvent it would affect things.

It depends on how often one wants to delay before the next operation, so I believe it is a misleading to say that it results in lag as it is only doing the job that it has been assigned, i.e sleep for a given amount of time :D

15 minutes ago, BrewManNH said:

For example, if he had managed to fix his mistakes in his first script it would have affected the response of the GUI controls. 

His first script doesn't have a sleep, and as far as I know, the GUIGetMsg function will handle sleep for you automatically :)

Share this post


Link to post
Share on other sites
1 minute ago, TheDcoder said:

o I believe it is a misleading to say that it results in lag

I disagree completely. If you want to be able to click a control and have it work, and it doesn't because of the sleep you put in the script, then I call that lag. Regardless of whether you intended it or not.

 

2 minutes ago, TheDcoder said:

His first script doesn't have a sleep, and as far as I know, the GUIGetMsg function will handle sleep for you automatically

No, but if he had followed your explanation and put one in with a large sleep time, it would have delayed his script (Lag) unnecessarily. 

BTW, emoticons don't negate the fact you're trying to be a smarta$$ ;)

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
1 minute ago, BrewManNH said:

No, but if he had followed your explanation and put one in with a large sleep time, it would have delayed his script (Lag) unnecessarily.  

Why would it if it was event based? It would only reduce the amount of calls required to the sleep function.

2 minutes ago, BrewManNH said:

BTW, emoticons don't negate the fact you're trying to be a smarta$$ ;)

Sorry if I came around as one, I was just trying to make everything clear  B):evil::muttley:

5 minutes ago, BrewManNH said:

I disagree completely. If you want to be able to click a control and have it work, and it doesn't because of the sleep you put in the script, then I call that lag. Regardless of whether you intended it or not. 

You would be correct if the sleep was in the same loop along with GUIGetMsg, but it is not what I suggested and it was never the case here.

Share this post


Link to post
Share on other sites
25 minutes ago, TheDcoder said:

You would be correct if the sleep was in the same loop along with GUIGetMsg, but it is not what I suggested and it was never the case here.

I just did a test with Sleep(5000) and it had no lag showing the GUIs or executing the hotkey.

The while loop is only here to keep the program running (not exit). OnEvent is listening and it is separate from the while loop.

Now if you put something in that while loop, it will affect it. So use OnEvent calls instead. It makes scenes now. :D

Edited by leomoon

Share this post


Link to post
Share on other sites
2 minutes ago, leomoon said:

The while loop is only here to keep the program running (not exit). OnEvent is listening and it is separate from the while loop.

Yup, you are right about both points.

2 minutes ago, leomoon said:

I just did a test with Sleep(5000) and it had no lag showing the GUIs or executing the hotkey. 

The reason is same as you have mentioned, the while loop is paused while events are being proceed, that includes the sleep which will be paused (not interrupted), after all events have been processed, the script's execution will resume as normal and sleep will continue.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...