junkew Posted December 9, 2021 Share Posted December 9, 2021 Reading the wiki and another post I was wondering if the globals are really needed when using the GUI functions Is there any better approach without the globals? Is there a way like guictrread(gui1.textcontrol1)? I use the event driven gui's and as such in the related functions I would prefer to only have a reference to the gui and control name in a more controlledmanner. If I use the KODA designer I would prefer not to have 1. prefix all relevant controls with Global in the Koda region (as then next time I have to do it again) 2. not manually declare them as global in top of my script 3. use naming convention like prefixing with the guiname Func gui1() $hGUI1 = GUICreate("Gui 1", 200, 200, 100, 100) GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close", $hGUI1) ; Call a common GUI close function $hGUI1_Button1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30) GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function $hGUI1_Button2 = GUICtrlCreateButton("Show Gui 2", 10, 60, 80, 30) GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function GUISetState() EndFunc ;==>gui1 References I was reading https://www.autoitscript.com/wiki/Managing_Multiple_GUIs FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
benners Posted December 9, 2021 Share Posted December 9, 2021 I don't think it's possible to eliminate the globals fully, currently. In the past I have made them local in the function that creates the gui and passed them as parameters to functions. If a control is not being actioned, changed or updated I don't assign it a variable. An example would be a label who's text doesn't change or, as in your example, a button that has an onevent action and is only being clicked. I have seen code where every control is assigned a variable so it does reduce the number of globals. I normally have an array for the controls and enum the variables, the control id's are then set and retrieved via get\set functions from a gui udf I have made. It's not ideal but its cleaner and I can see all the controls in one array. I can then loop through controls to enable\disable or set values. If I need to add or remove controls, it doesn't affect the controls as the enum value changes. One way to not have to declare the globals at the top of the script would be to create Local Static variables and a Local static array to hold the values in the function that creates the gui. You'd still need get\set functions and they'd still keep memory like a global. As for multiple guis, I have used them in the past and made a function to switch between the guis with the constants declared at the start of the udf. Its only useful if you want to work with one gui at a time, could be improved but it's worked for me so far.. ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GA_SwitchGUI ; Description ...: Switches the current active window to be associated with running GUI functions. ; Syntax ........: _GA_SwitchGUI($h_NewGui[, $h_OldGui [, $i_EventMode = $GA_ONEVENTMODENO[, $i_DisableOldGui = $GA_DISABLEOLDGUINO[, $i_DeleteOldGui = $GA_DELETEOLDGUINO]]]]) ; Parameters ....: $h_NewGui - The handle of the gui that is to be the current window ; $h_OldGui - The handle of the gui that is to relinquish control ; $i_EventMode - Sets the Opt("GUIOnEventMode") for notifications (enable\disable) Default is disabled ($GA_ONEVENTMODENO) ; $i_DisableOldGui - Changes the state of the $h_OldGui window (enable\disable) Default is enabled ($GA_DISABLEOLDGUINO) ; $i_DeleteOldGui - Deletes the $h_OldGui window and all controls that it contains (yes\no) Default is no ($GA_DELETEOLDGUINO) ; Return values .: None ; Author ........: Benners ; Modified ......: ; Remarks .......: ; =============================================================================================================================== Func _GA_SwitchGUI($h_NewGui, $h_OldGui, $i_EventMode = Default, $i_DisableOldGui = Default, $i_DeleteOldGui = Default) If $i_EventMode = Default Then $i_EventMode = $GA_ONEVENTMODENO If $i_DisableOldGui = Default Then $i_DisableOldGui = $GA_DISABLEOLDGUINO If $i_DeleteOldGui = Default Then $i_DeleteOldGui = $GA_DELETEOLDGUINO Opt("GUIOnEventMode", $i_EventMode) ;_ Set on event mode (disabled or enabled) GUISwitch($h_NewGui) ;_ Switch control to the new GUI. GUISetState(@SW_ENABLE, $h_NewGui) ;_ enable the new gui GUISetState(@SW_RESTORE, $h_NewGui) ;_ restore the gui If $i_DisableOldGui Then GUISetState(@SW_DISABLE, $h_OldGui) ;_ Disable the old GUI. If $i_DeleteOldGui Then GUIDelete($i_DeleteOldGui) ;_ destroy the old gui EndFunc ;==>_GA_SwitchGUI Link to comment Share on other sites More sharing options...
ad777 Posted December 10, 2021 Share Posted December 10, 2021 (edited) expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Opt("GUIOnEventMode", 1) $g_hGUI1 = GUICreate("GUI 1", 200, 200, 100, 100) GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function $g_idButton1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30) GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function GUISetState() While 1 Sleep(10) WEnd Func On_Button() Switch @GUI_CtrlId ; See which button sent the message Case $g_idButton1 MessageBox(1) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<< Case $g_idButton2 GUICtrlSetState($g_idButton2, $GUI_DISABLE) gui2() Case $g_idButton3 MessageBox(2) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<< EndSwitch EndFunc ;==>On_Button Func gui2() $g_hGUI2 = GUICreate("GUI 2", 200, 200, 350, 350) GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function $g_idButton3 = GUICtrlCreateButton("MsgBox 2", 10, 10, 80, 30) GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function GUISetState() EndFunc ;==>gui2 Func On_Close() Switch @GUI_WinHandle ; See which GUI sent the CLOSE message Case $g_hGUI1 Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<< Case $g_hGUI2 GUIDelete($g_hGUI2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<< GUICtrlSetState($g_idButton2, $GUI_ENABLE) EndSwitch EndFunc ;==>On_Close Func MessageBox($iIndex) MsgBox($MB_OK, "MsgBox " & $iIndex, "Test from Gui " & $iIndex) EndFunc ;==>MessageBox or you can use instant numbers;(if you know id of button of gui) expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Opt("GUIOnEventMode", 1) _gui() Func _gui() $g_hGUI1 = GUICreate("GUI 1", 200, 200, 100, 100) GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function $g_idButton1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30) GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function GUISetState() While 1 Sleep(10) WEnd EndFunc Func On_Button() Switch @GUI_CtrlId ; See which button sent the message Case 3;;instant number MessageBox(1) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<< EndSwitch EndFunc ;==>On_Button Func gui2() $g_hGUI2 = GUICreate("GUI 2", 200, 200, 350, 350) GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function $g_idButton3 = GUICtrlCreateButton("MsgBox 2", 10, 10, 80, 30) GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function GUISetState() EndFunc ;==>gui2 Func On_Close() Switch @GUI_WinHandle ; See which GUI sent the CLOSE message Case WinGetHandle(WinGetHandle("[active]")) Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<< Case WinGetHandle($g_hGUI2) GUIDelete($g_hGUI2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<< GUICtrlSetState($g_idButton2, $GUI_ENABLE) EndSwitch EndFunc ;==>On_Close Func MessageBox($iIndex) MsgBox($MB_OK, "MsgBox " & $iIndex, "Test from Gui " & $iIndex) EndFunc ;==>MessageBox or this soultion: Func GetText();; DllStructSetData($g_tStruct, "x", MouseGetPos(0)) DllStructSetData($g_tStruct, "y", MouseGetPos(1)) $hWnd = _WinAPI_WindowFromPoint($g_tStruct) ; Return ControlGetText($hWnd,"",$hWnd); Sleep(10) EndFunc or this: Func On_Button() for $i = 0 To 9 Step 1 if @GUI_CtrlId = $i Then MessageBox(1) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<< EndIf Next EndFunc ;==>On_Button Edited December 10, 2021 by ad777 iam ِAutoit programmer. best thing in life is to use your Brain to Achieve everything you want. Link to comment Share on other sites More sharing options...
benners Posted December 10, 2021 Share Posted December 10, 2021 _Singleton is to stop multiple instances of the same script. Link to comment Share on other sites More sharing options...
junkew Posted December 11, 2021 Author Share Posted December 11, 2021 These will probably help a lot already. @GUI_CtrlHandle Last click GUI Control handle. Only valid in an event Function. See the GUICtrlSetOnEvent() function. @GUI_CtrlId Last click GUI Control identifier. Only valid in an event Function. See the GUICtrlSetOnEvent() function. @GUI_DragFile Filename of the file being dropped. Only valid on Drop Event. See the GUISetOnEvent() function. @GUI_DragId Drag GUI Control identifier. Only valid on Drop Event. See the GUISetOnEvent() function. @GUI_DropId Drop GUI Control identifier. Only valid on Drop Event. See the GUISetOnEvent() function. @GUI_WinHandle Last click GUI window handle. Only valid in an event Function. See the GUICtrlSetOnEvent() function. FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now