Jump to content
Sign in to follow this  

GUIState 7 OnEvent

Recommended Posts


here I am writing simple gui with text processing capabilities.

i have main gui, background picture, all buttons with labels over them 2 static buttons (1 present here)

Opt("GUIOnEventMode", 1) and includes

$main = GUICreate("Title", 961, 721); Main Window
$bgpic = GUICtrlCreatePic(@WorkingDir & "\960x720.jpg", 0, 0, 960, 720); use background picture

$quitBtn = GUICtrlCreateButton("Quit", 885, 685, 95, 40)
GUICtrlSetOnEvent($quitBtn, "_exit"); assign quit button to function "exit"
_GuiCtrlMakeTrans(-1, 1)
$quitLabel = GUICtrlCreateLabel("Quit", 885, 685, 95, 40, BitOR($SS_CENTER, $BS_BOTTOM))
GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
GUICtrlSetColor(-1, 0x962129)


then i have the code for creating buttons dynamically from information in file

Dim $Button[$NumberFromText]
    $startX = 48
    $startY = 24
    $fromLeft = 240
    $bHeigh = 160
    $bWidth = 50

    Dim $1to4btn[0]

    For $x = 0 To _Min(UBound($Button) - 1, 3) ; creates maximum 4 button, label

        $Button = GUICtrlCreateButton($x, $startX, $startY, $bHeigh, $bWidth); create buttons
        _GuiCtrlMakeTrans(-1, 1) ; transparency 255 Solid, 0 Transparent
        $serverLabel = GUICtrlCreateLabel(FileReadLine($configF, $SN), _ ; read 1st line and 5 below on each iteration
        $startX, $startY, $bHeigh, $bWidth, BitOR($SS_CENTER, $SS_CENTERIMAGE, $WS_EX_TRANSPARENT));
        GUICtrlSetFont(-1, 14, 400, 0, "Tahoma"); set font and size
        GUICtrlSetColor($serverLabel, 0x04A111); set color of font
        GUICtrlSetBkColor($serverLabel, $GUI_BKCOLOR_TRANSPARENT); make label Background transparent

        $SN += 5; read 5 lines below
        $startX = $startX + $fromLeft

        $btnArray=_ArrayAdd($1to4btn, GUICtrlGetHandle($Button))


since buttons are created under the label $Button and I couldn't conacenate $Button[$i] , I created array and place handles into it and connected static buttons with

GUICtrlSetOnEvent($cancelBtn,"cancelFunc") and it worked

now i tried using handles to assign functions to buttons but it fails with GUICtrlSetOnEvent()

also tried GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "funcForFirstButton", $1to4btn[0])

if I assign without handle it works by clicking anywhere on MainGUI

While loop includes only sleep(10)


Window starts as inactive and return code 7

If i click dynamically created buttons, window loses focus . clicking anywhere else focuses window


this is _GuiCtrlMakeTrans function (found here on forums some time ago)


Func _GuiCtrlMakeTrans($iCtrlID, $iTrans = 255)
    Local $pHwnd, $nHwnd, $aPos, $a

    $hWnd = GUICtrlGetHandle($iCtrlID);Get the control handle
    If $hWnd = 0 Then Return SetError(1, 1, 0)
    $pHwnd = DllCall("User32.dll", "hwnd", "GetParent", "hwnd", $hWnd);Get the parent Gui Handle
    If $pHwnd[0] = 0 Then Return SetError(1, 2, 0)
    $aPos = ControlGetPos($pHwnd[0], "", $hWnd);Get the current pos of the control
    If @error Then Return SetError(1, 3, 0)
    $nHwnd = GUICreate("", $aPos[2], $aPos[3], $aPos[0], $aPos[1], 0x80000000, 0x00080000 + 0x00000040, $pHwnd[0]);greate a gui in the position of the control
    If $nHwnd = 0 Then Return SetError(1, 4, 0)
    $a = DllCall("User32.dll", "hwnd", "SetParent", "hwnd", $hWnd, "hwnd", $nHwnd);change the parent of the control to the new gui
    If $a[0] = 0 Then Return SetError(1, 5, 0)
    If Not ControlMove($nHwnd, '', $hWnd, 0, 0) Then Return SetError(1, 6, -1);Move the control to 0,0 of the newly created child gui
    GUISetState(@SW_SHOW, $nHwnd);show the new child gui
    WinSetTrans($nHwnd, "", $iTrans);set the transparency
    If @error Then Return SetError(1, 7, 0)
    GUISwitch($pHwnd[0]);switch back to the parent Gui

    Return $nHwnd;Return the handle for the new Child gui
EndFunc   ;==>_GuiCtrlMakeTrans

also created simple button on the same mainGUI and tried but didn't work

GUICtrlSetOnEvent(-1,"FunctionOne") ; shows messagebox and write console


Edited by shotiko
added undocumented UDF

Share this post

Link to post
Share on other sites

I believe GUICtrlSetOnEvent is expecting an AutoIt ControlID, not a handle.

Your last example looks fine to me.  I'd suspect there's something elsewhere in your code breaking it if that doesn't work.

Can you show more code or post a "complete" script that reproduces the issue when executed?

Share this post

Link to post
Share on other sites

problem with state 7 (no focus) lies in _GUICtrlMakeTrans function. which i couldn't fix.

i've also found workaround with buttons

Instead of

$Button = GUICtrlCreateButton($x, $startX, $startY, $bHeigh, $bWidth); create buttons
$btnArray=_ArrayAdd($1to4btn, GUICtrlGetHandle($Button))

i use

Assign("Button", GUICtrlCreateButton($x, $startX, $startY, $bHeigh, $bWidth)); create buttons
_ArrayAdd($cButtons, Eval("Button"), 1) ; this adds Button controlIDs to array

and i decided to switch back to GUIGetMsg

Edited by shotiko

Share this post

Link to post
Share on other sites

now new problem arises with Eval("Button"): Now i have

Assign("Button", GUICtrlCreateButton($x, $startX, $startY, $bHeigh, $bWidth)); create buttons
_ArrayAdd($cButtons, Eval("Button"), 1) ; this adds Button controlIDs to array

While 1
    Switch GUIGetMsg()
        Case $cButtons[0] ; Server 1
        Case $cButtons[1] ; Server 2
            ConsoleWrite('s2 click' & @CRLF)
        Case $cButtons[2] ; Server 3
            ConsoleWrite('s3 click' & @CRLF)
        Case $cButtons[3] ; Server 4
            ConsoleWrite('s4 click' & @CRLF)

problem is that if i don't have all maximum buttons created then Case $cButtons[1] and the others return error: subscript

so what's the workaround here since i can't nest switch and if

Edited by shotiko

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

    • AndreyS
      By AndreyS
      Tell me, please, why in the application of different styles($SS_BLACKFRAME, $SS_BLACKRECT, etc.) of frames stops working function Msg().
      And why in the application of certain frame styles($SS_BLACKFRAME) do not even picture displayed?
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> Opt("GUIOnEventMode", 1) Example() Func Example() GUICreate("My GUI picture", 350, 300, -1, -1) ; will create a dialog box that when displayed is centered GUISetOnEvent($GUI_EVENT_CLOSE,"Quit") $idPic = GUICtrlCreatePic("D:\Program Files\AutoIt3\Examples\GUI\mslogo.jpg", 50, 50, 200, 50, $SS_SUNKEN) GUICtrlSetOnEvent(-1,"Msg") GUISetState(@SW_SHOW) ; Loop until the user exits. While 1 WEnd EndFunc ;==>Example Func Msg() MsgBox(0,0,0) EndFunc Func Quit() Exit EndFunc  
    • tarretarretarre
      By tarretarretarre
      Inspired from

      I created a UDF that I have been using to replace GUICtrlSetOnEvent, not using Opt("GUIOnEventMode").
      AutoIt's GUICtrlSetOnEvent function only supports mouse clicks without parameters at this moment, this UDF works on the same premise but gives the user some more flexibility, in just 61 lines of code.
      Here is an example of what A callback will look like
      Func MyCallBackFunction(Const $wParam, ByRef $iCtrlId, ByRef $uData, ByRef $hWnd) Switch $wParam Case $WM_HOVERIN ConsoleWrite("Entering control; attached data = " & $uData & @CRLF) Case $WM_HOVEROUT ConsoleWrite("Leaving control" & @CRLF) Case $WM_LBUTTONDOWN ConsoleWrite("Left mousebutton DOWN" & @CRLF) Case $WM_LBUTTONUP ConsoleWrite("Left mousebutton UP" & @CRLF) Case $WM_MBUTTONDOWN ConsoleWrite("Middle mousebutton DOWN" & @CRLF) Case $WM_MBUTTONUP ConsoleWrite("Middle mousebutton UP" & @CRLF) Case $WM_RBUTTONDOWN ConsoleWrite("Right mousebutton DOWN" & @CRLF) Case $WM_RBUTTONUP ConsoleWrite("Right mousebutton UP" & @CRLF) EndSwitch EndFunc  
      More examples can be found in the zip file.
      Good 2 know
      GuiDelete($hWnd) used together with $WM_LBUTTONUP will cause the AutoIt script to crash $WM_HOVERIN and $WM_HOVEROUT ARE NOT real Windows Message codes and will only work with with this UDF Your callback functions MUST have 4 parameters assigned to it. That's about it.
      Feel free to pitch some improvements.
    • Leo1906
      By Leo1906
      I read about GUIOnEventMode while making my research and I found out that you can't use 
      Opt("GUIOnEventMode", 1)
      $msg = GUIGetMsg()
      at the same time. So far so good.
      In my script I am spawning a few child GUIs and only for the main GUI I need interaction with buttons etc.
      My question now is, is it possible to set those flags specifically for a single GUI? Can I somehow pass the handle to a GUI this option should affect? And for the main GUI I use GuiGetMsg?
      I hope you can understand what I'm trying to do ..
      Thanks for your help
    • byarotsky
      By byarotsky
      I am working on a script that pulls the current time from the internet and then reads contents from our backup server based on the month and week folders that are already created. This script will ultimate pass variables to another program that will restore the data from the respective folder to the computer that the script it running on. The script is built to find the 'windows' folders in the server structure which is \\server\serverfolder\01_16\3week
      \\server\serverfolder - is the server share
      01_16 - is month and year as to sort properly
      3week - is obviously the week
      Within the week folder there are user folders (eg. bsmith, jdoe, ganderson), in these folders are backups of full hard drives with include the windows folder which we are looking for.The script is running and is fully function the first time that it is run. I have also added a back button so if for some reason you are in the wrong list of folders you can navigate to the correct folder and then show the results again.
      The issue I am having is that I can show the contents and everything works well, unless I use the back button and navigate to a different set of folders (different week). The radio buttons do not respond properly. They will show both incorrect data and the count of radio buttons is not being updated.
      I have attempted to look for a solution and have tried many different ideas, but I just don't fully understand how a GUICtrlCreateRadio in a for loop with GUICtrlSetOnEvent should work.
      Any suggestions would be greatly appreciated. I apologize for the messy code.
      #include <Array.au3> #include <File.au3> #include <GUIConstantsEx.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GetTimeOnline ; Description ...: Retrieve the current date and time from TimeAPI.org. ; Syntax ........: _GetTimeOnline($iTimeZone) ; Parameters ....: $iTimeZone - An integer value of the timezone . ; 0 - UTC (Universal Time) ; 1 - EST (Eastern Time) ; 2 - CST (Central Time) ; 3 - MST (Mountain Time) ; 4 - PST (Pacific Time) ; 5 - AKST (Alaska Time) ; 6 - HAST (Hawaii-Aleutian Time) ; Return values .: Success: Returns the current Date and Time in the format YYYY/MM/DD HH:MM:SS ; Failure: Sets @error to non-zero and returns the same format as a successful return but using the system time. ; Author ........: guinness ; Link ..........: According to http://www.programmableweb.com/api/timeapi, this is for non-commercial use. ; Example .......: Yes ; =============================================================================================================================== Opt("GUIResizeMode", 1) Opt("GUIOnEventMode", 1) Global $vGUI_V = 450 Global $vGUI_ = 400 Local $ServerPath = "\\vault\backups\" Local $aFileList, $Current_Radio, $CurrentMonthFolder Local $Current_Gui = GUICreate("Test", 450, 400) Local $GuiBack = GUICtrlCreateButton("Back", 200, 250, 40, 40) GUICtrlCreateLabel("Please select the Windows folder to use", 20, 20) GUICtrlSetOnEvent($GuiBack, "BackPressed") GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents") GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents") GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents") _GetTimeOnline(2) ;Central time zone _SelectFolder() While 1 Sleep(100) WEnd Exit Func _GetTimeOnline($iTimeZone) ; Retrieve the current time from TimeAPI.org. Ideal if your Windows clock is out of sync. Local $aTimeZone[7] = ['utc', 'est', 'cst', 'mst', 'pst', 'akst', 'hast'] Local $sRead = BinaryToString(InetRead('http://www.timeapi.org/' & $aTimeZone[$iTimeZone] & '/now?format=\Y/\m/\d%20/\H:\M:\S')) If @error Then SetError(1, 0, @YEAR & '/' & @MON & '/' & @MDAY & '/' & @HOUR & ':' & @MIN & ':' & @SEC) EndIf Local $aDays = StringSplit($sRead, "/") ; Split the string of days using the delimiter "," and the default flag value. ; For $i = 1 To $aDays[0] ; Loop through the array returned by StringSplit to display the individual values. ; MsgBox(4096, "", "$aDays[" & $i & "] - " & $aDays[$i]) ; Next $CurrentMonthFolder = $ServerPath & $aDays[2] & "_" & StringTrimLeft($aDays[1], 2) & "\" & Ceiling((@MDAY + (7 - @WDAY)) / 7) & "week" EndFunc ;==>_GetTimeOnline Func _SelectFolder() Local Const $sMessage = "Select users folder to restore data from:" ; Display an open dialog to select a file. Local $sFileSelectFolder = FileSelectFolder($sMessage, $CurrentMonthFolder) If @error Then ; Display the error message. ; MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.") Exit Else ; Display the selected folder. ; MsgBox($MB_SYSTEMMODAL, "", "You chose the following folder:" & @CRLF & $sFileSelectFolder) $aFileList = _FileListToArrayRec($sFileSelectFolder, "Windows", 14, -3, $FLTAR_SORT) If @error = 1 Then MsgBox($MB_SYSTEMMODAL, "", "Path was invalid or Windows folder not found.") _SelectFolder() EndIf If @error = 9 Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.") _SelectFolder() EndIf EndIf $CurrentMonthFolder = $sFileSelectFolder _ShowGui() EndFunc ;==>_SelectFolder Func _ShowGui() Local $startline = 20 If @error Then MsgBox(4096, "ERROR", "Error occured, no INI or incorrect Sections") Else For $i = 1 To $aFileList[0] $Current_Radiou = GUICtrlCreateRadio($aFileList[$i], 25, $startline + 20 * $i, 400) GUICtrlSetOnEvent(-1, "myEvent") ; This will tell us which radio was clicked Next EndIf GUISetState(@SW_SHOW, $Current_Gui) EndFunc ;==>_ShowGui Func BackPressed() ; MsgBox($MB_SYSTEMMODAL, "Go Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle & " CtrlHandle=" & @GUI_CtrlHandle) GUISetState(@SW_HIDE, $Current_Gui) _SelectFolder() EndFunc ;==>BackPressed Func myEvent() For $x = 1 To $aFileList[0] + 6 ; this is broken If GUICtrlRead($x) = 1 Then MsgBox(0, GUICtrlRead($x), GUICtrlRead($x, 1)) ;this is the path to the profile Next EndFunc ;==>myEvent Func SpecialEvents() Select Case @GUI_CtrlId = $GUI_EVENT_CLOSE ; MsgBox($MB_SYSTEMMODAL, "Close Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle) Exit Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE ; MsgBox($MB_SYSTEMMODAL, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle) Case @GUI_CtrlId = $GUI_EVENT_RESTORE ; MsgBox($MB_SYSTEMMODAL, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle) EndSelect EndFunc ;==>SpecialEvents  
    • Kovacic
      By Kovacic
      Semi experienced developer here.. I have never fully gotten the handle of using GUIOnEventMode, so i thought I would try it with a small simple program that I can paste some text into, then have it count down, and type it out for me.. I use this for when I am iLO into a server and I cant paste. For those of you not familiar with iLO, it stands for Integrated Lights Out. in short, it lets you remote into a server even if its having BIOS issue and wont start.. more details here: http://www8.hp.com/us/en/products/servers/ilo/
      Anyway, when in iLO, you cant paste anything. Sometimes I need to copy bash scripting from my computer to the server, so I drop it in the little app, then use it to type out the script in the iLO screen. I have it so it does a countdown before sending the keys, and a delay between each key ( because iLO will sometimes hang up a little ). 
      The app I am trying to write will count down, then send whatever is in the edit box. I also have a stop button in case you want to stop the app, but that doesn't seem to do anything. I feel like I'm using GUIOnEventMode improperly. Can someone take a peek?
      #include <GUIConstantsEx.au3> #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;Opt                                     ("GUICoordMode", 2) Opt                                      ("GUIResizeMode", 1) Opt                                      ("GUIOnEventMode", 1) GUISetOnEvent                            ($GUI_EVENT_CLOSE, "SpecialEvents") GUISetOnEvent                            ($GUI_EVENT_MINIMIZE, "SpecialEvents") GUISetOnEvent                            ($GUI_EVENT_RESTORE, "SpecialEvents") $Form1 = GUICreate                       ("Sender", 615, 438, 192, 124) $edit = GUICtrlCreateEdit                ("", 8, 128, 601, 305) $stop = GUICtrlCreateButton              ("STOP", 496, 60, 100, 25) GUICtrlSetOnEvent                        (-1, "stop") $SEND = GUICtrlCreateButton              ("SEND", 496, 88, 100, 25) GUICtrlSetOnEvent                        (-1, "sendit") $SecsToWaitB4Sending = GUICtrlCreateInput("10", 200, 32, 145, 25) GUICtrlCreateLabel                       ("Seconds to wait before sending", 40, 32, 153, 17) GUICtrlCreateLabel                       ("Delay between char sent in ms.", 40, 64, 152, 17) $MSBetweenLEtters = GUICtrlCreateInput   ("100", 200, 64, 145, 25) GUISetState                              (@SW_SHOW) While 1     Sleep(5) WEnd Func stop()     GUICtrlSetData($SEND, "SEND") EndFunc   ;==>stop Func sendit()     Local $h = Int(GUICtrlRead($SecsToWaitB4Sending))     GUICtrlSetData($SEND, "Waiting")     For $i = 1 To Int(GUICtrlRead($SecsToWaitB4Sending))         GUICtrlSetData($SEND, $h)         For $c = 1 To 100             Sleep(1)         Next         $h -= 1     Next     GUICtrlSetData($SEND, "Typing")     Opt("SendKeyDelay", Int(GUICtrlRead($MSBetweenLEtters)))     Send(GUICtrlRead($edit), 1)     GUICtrlSetData($SEND, "SEND") EndFunc   ;==>sendit Func SpecialEvents()     Select         Case @GUI_CtrlId = $GUI_EVENT_CLOSE             Exit         Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE             ;         Case @GUI_CtrlId = $GUI_EVENT_RESTORE             ;     EndSelect EndFunc   ;==>SpecialEvents  
      Thanks in advance!