Jump to content
remin

Focus and multiple GUI's problem

Recommended Posts

remin

I do have one autoit script file with multiple functions.

p.e.
 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <String.au3>
#include <GuiButton.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <HotString.au3>

Func ACase()
  $Form4=GUICreate("ACase", 100, 195, 290, 142)
    etc etc
 GUISetState(@SW_SHOW, $Form4)

  While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

     Case $GUI_EVENT_CLOSE
         SUB_Write2ini(....) ;I write a few things in an ini file
     Case $a

     Case $b

     etc
     EndSwitch
   WEnd
EndFunc

Func BCase()
  $Form5=GUICreate("BCase", 100, 195, 290, 142)
    etc etc

 GUISetState(@SW_SHOW, $Form5)

  While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

     Case $GUI_EVENT_CLOSE
         SUB_Write2ini(....) ;I write a few things in an ini file
     Case $a

     Case $b

     etc
     EndSwitch
   WEnd
EndFunc

When I activate the 1st function (ACase), using a shortcut and the 2nd function (BCase) (with a different shortcut) and I click on a button in whatever of these 2 Gui's, I can't use the other Gui anymore. It doesn't do the right thing as if autoit only remembers the Gui I first used.

What did I wrong? How can I let autoit know which GUI is active and to connect to the function of that Gui?

Hope I made myself clear.

 

Edited by remin

Share this post


Link to post
Share on other sites
careca

Well, you have 2 loops, when you're inside one, you can't expect anything from the other.

I'd go with setonevent, ditch the while loop there, transfer the loop outside of those functions.

 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <String.au3>
#include <GuiButton.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>
;#include <HotString.au3>
Opt("GUIOnEventMode", 1)
Opt("GUIEventOptions", 1)
;=============================================================================
Global $Form4, $Form5
;=============================================================================
ACase()
While 1
    Sleep(100)
    If WinExists($Form4) = 0 And WinExists($Form5) = 0 Then Exit
WEnd
;=============================================================================
Func ACase()
$Form4=GUICreate("ACase", 200, 195, 290, 142)
GUISetOnEvent($GUI_EVENT_CLOSE, "QuitA")
GUICtrlCreateButton('open B', 10, 10)
GUICtrlSetOnEvent(-1, 'BCase')
GUISetState(@SW_SHOW, $Form4)
EndFunc
;=============================================================================
Func BCase()
$Form5=GUICreate("BCase", 200, 195, 290, 142)
GUISetOnEvent($GUI_EVENT_CLOSE, "QuitB")
GUICtrlCreateButton('message', 10, 10)
GUICtrlSetOnEvent(-1, 'mbx')
GUISetState(@SW_SHOW, $Form5)
EndFunc
;=============================================================================
Func QuitA()
    GUIDelete($Form4)
EndFunc
;=============================================================================
Func QuitB()
    GUIDelete($Form5)
EndFunc
;=============================================================================
Func mbx()
    MsgBox(64 + 262144, '', 'Ding')
EndFunc
;=============================================================================

 

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
pixelsearch

Hi remin :)

If you study the following scripts (don't remember where I found them, wiki probably) it may give you ideas to solve your problem. First with GuiGetMsg :

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

; example of how to manage two GUIs simultaneously using the "advanced" parameter with GUIGetMsg : Yes ! (MessageLoop mode)
; a single While...WEnd loop which distinguishes between the two GUIs, both GUIs and their controls remain active  and we stay
; in the main idle loop while we wait

Global $g_hGUI2 = 9999 ; Predeclare this variable with dummy value to prevent firing the Case statement, only for GUI2 this time
Global $g_idButton3

gui1()

Func gui1()
    Local $hGUI1 = GUICreate("Gui 1", 200, 200, 100, 100)
    Local $idButton1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30)
    Local $idButton2 = GUICtrlCreateButton("Show Gui 2", 10, 60, 80, 30)
    GUISetState()

    Local $aMsg
    While 1
        $aMsg = GUIGetMsg(1) ; Use advanced parameter to get array
        Switch $aMsg[1] ; check which GUI sent the message
            Case $hGUI1
                Switch $aMsg[0] ; Now check for the messages for $hGUI1
                    Case $GUI_EVENT_CLOSE ; If we get the CLOSE message from this GUI - we exit <<<<<<<<<<<<<<<
                        ExitLoop
                    Case $idButton1
                        MsgBox($MB_OK, "MsgBox 1", "Test from Gui 1")
                    Case $idButton2
                        GUICtrlSetState($idButton2, $GUI_DISABLE)
                        gui2()
                EndSwitch
            Case $g_hGUI2
                Switch $aMsg[0] ; Now check for the messages for $g_hGUI2
                    Case $GUI_EVENT_CLOSE ; If we get the CLOSE message from this GUI - we just delete the GUI <<<<<<<<<<<<<<<
                        GUIDelete($g_hGUI2)
                        GUICtrlSetState($idButton2, $GUI_ENABLE)
                    Case $g_idButton3
                        MsgBox($MB_OK, "MsgBox", "Test from Gui 2")
                EndSwitch
        EndSwitch
    WEnd
EndFunc   ;==>gui1

Func gui2()
    $g_hGUI2 = GUICreate("Gui 2", 200, 200, 350, 350)
    $g_idButton3 = GUICtrlCreateButton("MsgBox 2", 10, 10, 80, 30)
    GUISetState()
EndFunc   ;==>gui2

Then, as suggested by Careca, with On Event Mode :

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

; Coders using OnEvent mode do not usually find the same problem with multiple GUIs as they can code separate functions
; for each $GUI_EVENT_CLOSE as shown in this example. Good (OnEvent mode)

Opt("GUIOnEventMode", 1)

Global $g_hGUI2, $g_idButton2 ; Predeclare these variables

gui1()

Func gui1()
    Local $hGUI1 = GUICreate("Gui 1", 200, 200, 100, 100)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close_Main") ; Run this function when the main GUI [X] is clicked

    Local $idButton1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button1")

    $g_idButton2 = GUICtrlCreateButton("Show Gui 2", 10, 60, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button2")

    GUISetState()

    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>gui1

Func gui2()
    $g_hGUI2 = GUICreate("Gui 2", 200, 200, 350, 350)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close_Secondary") ; Run this function when the secondary GUI [X] is clicked

    Local $idButton3 = GUICtrlCreateButton("MsgBox 2", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button3")

    GUISetState()
EndFunc   ;==>gui2

Func On_Close_Main()
    Exit
EndFunc   ;==>On_Close_Main

Func On_Close_Secondary()
    GUIDelete($g_hGUI2)
    GUICtrlSetState($g_idButton2, $GUI_ENABLE)
EndFunc   ;==>On_Close_Secondary

Func On_Button1()
    MsgBox($MB_OK, "MsgBox 1", "Test from Gui 1")
EndFunc   ;==>On_Button1

Func On_Button2()
    GUICtrlSetState($g_idButton2, $GUI_DISABLE)
    gui2()
EndFunc   ;==>On_Button2

Func On_Button3()
    MsgBox($MB_OK, "MsgBox 2", "Test from Gui 2")
 EndFunc   ;==>On_Button3


Good luck :)

Edited by pixelsearch
  • Confused 1

Share this post


Link to post
Share on other sites
remin
18 minutes ago, careca said:

Well, you have 2 loops, when you're inside one, you can't expect anything from the other.

I'd go with setonevent, ditch the while loop there, transfer the loop outside of those functions.

Thank you,

I think that's a lot of work. I do have 29 functions in my script file (not all with a while loop).

Maybe it's easier to refuse 2 Gui's at the same time.

Is it possible to close all open Autoit Gui's before to create the current Gui?

Edited by remin

Share this post


Link to post
Share on other sites
careca

Yep, create a function into a button or something and in that function delete all GUI's and recreate the one you want, depends on what you want exactly.


Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
Melba23

remin,

The Managing Multiple GUIs tutorial in the Wiki would be worth reading.

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

 

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

  • Similar Content

    • jpomalley
      By jpomalley
      Hi all,
      Does anyone know if there is a way to disable the Windows "fade effect" that occurs when a window appears or disappears?
      It is possible for a user to disable visual effects in windows in general through the system properties > performance settings, but I am interested just in fixing it for a particular window that pops up in my app.
      I would like it to appear and disappear instantaneously, snapping in and out, out of nowhere, rather than the short (maybe 300 ms?) fade in/fade out effect that windows has.
      Thanks for any tips
      JP
    • fastman
      By fastman
      I am struggling with activating the 2nd GUI.  References of https://www.autoitscript.com/wiki/Managing_Multiple_GUIs did not help.
      Gui2 and controls are created, but the buttons are not active.  I have tried to remove extra code.
      Thanks for any help.
      ; ; cabinet maker process to automate MasterCam ; ATP nesting process ; #include <AutoItConstants.au3> #include <Array.au3> ;#include <file.au3> ;#include <FileConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WinAPI.au3> #include <WinAPIFiles.au3> #include <WindowsConstants.au3> ;#include <XML.au3>; If XML library is updated, note that this script requires that save file will overwrite an existing file Opt("GUIOnEventMode", 1) ; 1 = OnEvent mode Opt("WinWaitDelay", 500) ; milliseconds Opt("TrayIconDebug", 1) Opt("WinSearchChildren", 1) Global $hMainGUI Global $hMainGUI2 Func GetAllWindowsControls($hCallersWindow, $bOnlyVisible = Default, $sStringIncludes = Default, $sClass = Default) If Not IsHWnd($hCallersWindow) Then ConsoleWrite("$hCallersWindow must be a handle...provided=[" & $hCallersWindow & "]" & @CRLF) Return False EndIf ; Get all list of controls If $bOnlyVisible = Default Then $bOnlyVisible = True If $sStringIncludes = Default Then $sStringIncludes = "" If $sClass = Default Then $sClass = "" $sClassList = WinGetClassList($hCallersWindow) ; Create array $aClassList = StringSplit($sClassList, @CRLF, 2) ; Sort array _ArraySort($aClassList) _ArrayDelete($aClassList, 0) ; Loop $iCurrentClass = "" $iCurrentCount = 1 $iTotalCounter = 1 If StringLen($sClass) > 0 Then For $i = UBound($aClassList) - 1 To 0 Step -1 If $aClassList[$i] <> $sClass Then _ArrayDelete($aClassList, $i) EndIf Next EndIf For $i = 0 To UBound($aClassList) - 1 If $aClassList[$i] = $iCurrentClass Then $iCurrentCount += 1 Else $iCurrentClass = $aClassList[$i] $iCurrentCount = 1 EndIf $hControl = ControlGetHandle($hCallersWindow, "", "[CLASSNN:" & $iCurrentClass & $iCurrentCount & "]") $text = StringRegExpReplace(ControlGetText($hCallersWindow, "", $hControl), "[\n\r]", "{@CRLF}") $aPos = ControlGetPos($hCallersWindow, "", $hControl) $sControlID = _WinAPI_GetDlgCtrlID($hControl) $bIsVisible = ControlCommand($hCallersWindow, "", $hControl, "IsVisible") If $bOnlyVisible And Not $bIsVisible Then $iTotalCounter += 1 ContinueLoop EndIf If StringLen($sStringIncludes) > 0 Then If Not StringInStr($text, $sStringIncludes) Then $iTotalCounter += 1 ContinueLoop EndIf EndIf If IsArray($aPos) Then ;ConsoleWrite("ControlID=[" & StringFormat("%5s", $sControlID) & "] Handle=[" & StringFormat("%10s", $hControl) & "] ClassNN=[" & StringFormat("%19s", $iCurrentClass & $iCurrentCount) & "] XPos=[" & StringFormat("%4s", $aPos[0]) & "] YPos=[" & StringFormat("%4s", $aPos[1]) & "] Width=[" & StringFormat("%4s", $aPos[2]) & "] Height=[" & StringFormat("%4s", $aPos[3]) & "] IsVisible=[" & $bIsVisible & "] Text=[" & $text & "]." & @CRLF) ConsoleWrite("Func=[GetAllWindowsControls]: ControlCounter=[" & StringFormat("%3s", $iTotalCounter) & "] ControlID=[" & StringFormat("%5s", $sControlID) & "] Handle=[" & StringFormat("%10s", $hControl) & "] ClassNN=[" & StringFormat("%19s", $iCurrentClass & $iCurrentCount) & "] XPos=[" & StringFormat("%4s", $aPos[0]) & "] YPos=[" & StringFormat("%4s", $aPos[1]) & "] Width=[" & StringFormat("%4s", $aPos[2]) & "] Height=[" & StringFormat("%4s", $aPos[3]) & "] IsVisible=[" & $bIsVisible & "] Text=[" & $text & "]." & @CRLF) Else ;ConsoleWrite("ControlID=[" & StringFormat("%5s", $sControlID) & "] Handle=[" & StringFormat("%10s", $hControl) & "] ClassNN=[" & StringFormat("%19s", $iCurrentClass & $iCurrentCount) & "] XPos=[winclosed] YPos=[winclosed] Width=[winclosed] Height=[winclosed] Text=[" & $text & "]." & @CRLF) ConsoleWrite("Func=[GetAllWindowsControls]: ControlCounter=[" & StringFormat("%3s", $iTotalCounter) & "] ControlID=[" & StringFormat("%5s", $sControlID) & "] Handle=[" & StringFormat("%10s", $hControl) & "] ClassNN=[" & StringFormat("%19s", $iCurrentClass & $iCurrentCount) & "] XPos=[winclosed] YPos=[winclosed] Width=[winclosed] Height=[winclosed] Text=[" & $text & "]." & @CRLF) EndIf If Not WinExists($hCallersWindow) Then ExitLoop $iTotalCounter += 1 Next ;MsgBox($MB_SYSTEMMODAL, "title", "check console for control list") EndFunc ;==>GetAllWindowsControls ; ======= Func GetAllWindowsControls($hCallersWindow, $bOnlyVisible = Default, $sStringIncludes = Default, $sClass = Default) ========= Func setNestingOverlay() $ovrWidth = 400 $ovrHeight = 60 $ovrLeft = -1 ;$mcamWindow[0] + $nestControlRelative[0] $ovrTop = -1;$mcamWindow[1] + $nestControlRelative[1]+ 10 $hMainGUI = GUICreate("covering mastercam buttons", $ovrWidth, $ovrHeight, $ovrLeft, $ovrTop, -1, $WS_EX_TOPMOST) GUISetOnEvent($GUI_EVENT_CLOSE, "CloseButton2") Local $iOKButton1 = GUICtrlCreateButton("Continue to gui2", 20, 20, 80) GUICtrlSetOnEvent($iOKButton1, "NestResultButtonOverlay") GUISetState(@SW_SHOW, $hMainGUI) GetAllWindowsControls(WinGetHandle("[ACTIVE]")) ;loop to monitor button click While 1 Sleep(50) ; Sleep to reduce CPU usage WEnd ConsoleWrite(" exit loop setNestingOverlay") EndFunc ;==>setNestingOverlay ;=== 2nd gui ======= 2nd gui ======= 2nd gui ======= 2nd gui ======= 2nd gui ======= 2nd gui ==== func NestResultButtonOverlay() $ovrWidth = -1;$mcamWindow[2] - 10 $ovrHeight = 70 ;40 $ovrLeft = -1 ;$mcamWindow[0] + 5 ;+ $nestControlRelative[0] $ovrTop = -1 ;$mcamWindow[1] + $nestControlRelative[1]+ 10 $hMainGUI2 = GUICreate("gui2-- covering nest result buttons", $ovrWidth, $ovrHeight, $ovrLeft, $ovrTop, -1, $WS_EX_TOPMOST) ;width, height, left, top ;GUISetState(@SW_DISABLE + @SW_MINIMIZE, $hMainGUI) ;GUISwitch($hMainGUI2) GUISetOnEvent($GUI_EVENT_CLOSE, "CloseButton2") local $iBackNestingButton = GUICtrlCreateButton("Back to Nesting", 20, 10, 120) GUICtrlSetOnEvent($iBackNestingButton, "backtoNesting") Local $iOKButton2 = GUICtrlCreateButton("Continue for g-Code", 160, 10, 120) GUICtrlSetOnEvent(-1, "generateGCode") ;GUICtrlSetOnEvent($iOKButton2, "generateGCode") Local $iDoneButton2 = GUICtrlCreateButton("Oops go back to vortex", 300, 10, 120) GUICtrlSetOnEvent($iDoneButton2, "CloseButton2") GUISetState(@SW_SHOW, $hMainGUI2) GUISetState(@SW_DISABLE + @SW_MINIMIZE, $hMainGUI) GUISwitch($hMainGUI2) GetAllWindowsControls(WinGetHandle("[ACTIVE]")) While 1 Sleep(50) ; Sleep to reduce CPU usage WEnd ConsoleWrite(" exit loop NestResultButtonOverlay") EndFunc ;==>setNestingOverlay func backtoNesting() msgbox($MB_SYSTEMMODAL, "gui2","backtoNesting") EndFunc Func CloseButton2() msgbox($MB_SYSTEMMODAL, "gui2","CloseButton2") Exit EndFunc func generateGCode() msgbox($MB_SYSTEMMODAL, "gui2","generateGCode") EndFunc ; =========== main =========== main =========== main =========== main =========== main =========== main setNestingOverlay()  
    • Blueman
      By Blueman
      Hey Guys,
      Maybe you can help me with something small.
      It is not terrible but it slows down my script and after a while even crash the script.

      First i explain what i have;
      I have a GUI with a lot of GUICtrlCreateInput controls, lets say 600 pieces.
      All the controls are hidden when the GUI is displayed, i have 3 buttons attached to 200 pieces each.
      When i press button-1 the first 200 controls are showed,
      When i press button-2 the first 200 controls are hidden and the next 200 controls are showed,
      etc, etc.
      This is working perfectly but after a while the GUI is getting slower and slower.
      After a while the controls aren't responding anymore and the script crashes.
      When i compile the script to 64-Bits version the speed is a lot better but you can still see the script slowing down each time i use the buttons.
      I think there has to be a buffer somewhere that is filling each time i show and hide the controls, but how can i clear that buffer without restarting the script?
      Deleting and re-creating the controls didn't help, deleting the entire gui (and controls) and re-creating it didn't help also.
      The only thing that is working is restarting the script.

      Thanks guys!

      PS: If you need a working example i can add it to this topic, just let me know. It is not rocket science, i just create the controls and show/hide them with a while loop and 3 buttons.
       
    • TheDcoder
      By TheDcoder

      ProxAllium
      ProxAllium is a GUI frontend to Tor, it aims to make the usage of Tor easier by directly exposing its SOCK5 proxy which can be used to access the Tor network. The GUI is designed to be simple and user-friendly and it has a few other features... namely:
      Fully portable - doesn't write outside its own directory Integrated with Tor via the controller interface and properly communicates with it Minimize to tray Option to start with Windows Interface to configure bridges if Tor is censored in your region Many customization options are available via the config.ini file Screenshots:
      The code is made with pure AutoIt, is fully open source and you are free to adapt it to your needs
      The GitHub repository hosts all the releases and code. As a bonus it has a somewhat sparsely documented Tor UDF which can be used to control Tor, the code also demonstrates the proper usage of my Process UDF which might be interesting if you want to deal with processes.
       
      As some of my friends know, I no longer use Windows as my main operating system. I switched to Linux a few months back as my primary operating system and haven't looked back since. Unfortunately that meant I could no longer use my own program due to it being Windows only... after a few months of playing around with C and making a basic program, I have decided to rewrite all of ProxAllium into C and make it cross-platform. Sadly this means that the AutoIt version of ProxAllium will not receive any major updates now.
      Let me know if this is something you guys would use, I used it daily with my IRC client to connect via Tor (to protect my I.P). I hope you enjoy using my program!
    • morion84
      By morion84
      Hi, when I call a MsgBox function I can use a "$MB_SERVICE_NOTIFICATION" flag so it will be displayed "on the current active desktop, even if there is no user logged on to the computer.". That works just great. Is there a way to do the same to Autoit GUI Form so it will be visible on desktop even if running from system account and user is not logged?
×