Jump to content

Close GUI and Stop Script


 Share

Recommended Posts

Hello guys. So i'm in need of a native or not function that when i press the "X" (Close button on the top right of the window/GUI) stops the script. I already try some stuff but nothing yet works.

Edited by AzgarD
Link to comment
Share on other sites

Sounds like you either need a way to interrupt your script, or build a mechanism into your script to check for a close trigger.

Can you describe what is going on in the main loop of your script or better yet, post the script?  We can offer ideas that may suit your needs, but need more information on what your script is doing that is keeping it busy and how best to interrupt it.

Link to comment
Share on other sites

Here is a simple example to add a close event to your close button:

 

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

GUICreate("Test", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "MenuExit")
GUISetState(@SW_SHOW)

While 1
  Sleep(10)
WEnd

Exit

Func MenuExit()

  GUIDelete()
  Exit

EndFunc

You must have Opt("GUIOnEventMode", 1) in your script or the event traps will not work.

Hope that helps.

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

  • Moderators

AzgarD,

Is the GUI that contains the [X] which you press part of your own script or from an external program?

If the former, then you need to look for the $GUI_EVENT_CLOSE event (as SlackerAl has already suggested), if from an external program then you need to monitor for either the existence of the process that creates the GUI (ProcessExists) or the GUI itself (WinExists).

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

7 minutes ago, Melba23 said:

if from an external program then you need to monitor for either the existence of the process that creates the GUI (ProcessExists) or the GUI itself (WinExists).

What about sending $WM_GETTITLEBARINFOEX on mouse down and then checking is mouse inside close button coordinates?

Link to comment
Share on other sites

#NoTrayIcon
#RequireAdmin
#include <SendMessage.au3>
#include <Constants.au3>
#include <GUIConstants.au3>
#include <WinAPI.au3>

Opt("WinWaitDelay", 0)
Opt("MouseClickDelay", 0)
Opt("MouseClickDownDelay", 0)
Opt("MouseClickDragDelay", 0)
Opt("SendKeyDelay", 0)
Opt("SendKeyDownDelay", 0)
Opt("WinTitleMatchMode", 3)

Sleep(3000)
_IsMouseOnCaptionButton(WinGetHandle("[ACTIVE]"))
Exit

Func _IsMouseOnCaptionButton($handle)
   If not IsDeclared("WIN_STATE_VISIBLE") Then Global Const $WIN_STATE_VISIBLE = 2
   Local $CaptionButtonsCoordinates[1][5] = [[1]]
   $tPoint = _WinAPI_GetMousePos()
   $MouseX = DllStructGetData($tPoint, "X")
   $MouseY = DllStructGetData($tPoint, "Y")
   $tagTITLEBARINFOEX = "int cbSize;" & $tagRECT & ";dword rgstate[6];long rgrect[24]"
   $tTITLEBARINFOEX = DllStructCreate($tagTITLEBARINFOEX)
   DllStructSetData($tTITLEBARINFOEX, "cbSize", DllStructGetSize($tTITLEBARINFOEX))
   _SendMessage($handle, $WM_GETTITLEBARINFOEX, 0, DllStructGetPtr($tTITLEBARINFOEX))
   $start = 0
   For $i = 1 to 4
      $CaptionButtonsCoordinates[0][0] += 1
      ReDim $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0]][5]
      Switch $i
      Case 1
         $start = 2*4 ;Minimize
         $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0] - 1][0] = 'Minimize'
      Case 2
         $start = 3*4 ;Maximize
         $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0] - 1][0] = 'Maximize'
      Case 3
         $start = 5*4 ;Close
         $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0] - 1][0] = 'Close'
      Case 4
         $start = 4*4 ;Help
         $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0] - 1][0] = 'Help'
      EndSwitch
      $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0] - 1][1] = DllStructGetData($tTITLEBARINFOEX, "rgrect", $start + 1) ;left
      $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0] - 1][2] = DllStructGetData($tTITLEBARINFOEX, "rgrect", $start + 3) ;right
      $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0] - 1][3] = DllStructGetData($tTITLEBARINFOEX, "rgrect", $start + 2) ;top
      $CaptionButtonsCoordinates[$CaptionButtonsCoordinates[0][0] - 1][4] = DllStructGetData($tTITLEBARINFOEX, "rgrect", $start + 4) ;bottom
   Next
   $CaptionButtonsText = ''
   For $j = 1 to UBound($CaptionButtonsCoordinates) - 1
      If $CaptionButtonsCoordinates[$j][1] < $MouseX and $MouseX < $CaptionButtonsCoordinates[$j][2] and $CaptionButtonsCoordinates[$j][3] < $MouseY and $MouseY < $CaptionButtonsCoordinates[$j][4] Then
         $CaptionButtonsText = $CaptionButtonsCoordinates[$j][0]
         ExitLoop
      EndIf
   Next
   If $CaptionButtonsText <> '' Then
      MsgBox(0, '', 'Mouse is inside ' & $CaptionButtonsText & ' button.')
   Else
      MsgBox(0, '', 'Mouse is outside of caption buttons.')
   EndIf
EndFunc

 

Link to comment
Share on other sites

This is how my program looks like, and is not yet finished. But what i need is a way to the AutoIt script recognize the default "Close" button of the window, the regular one in every window on the PC, the "X" next to the minimize and maximize buttons.

 

#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <AutoItConstants.au3>
#include <StringConstants.au3>
#include <ComboConstants.au3>
Global $gui = GUICreate("MONSTRA v1.0", 300, 250)
Global $sigla = GUICtrlCreateInput("Sigla ", 10, 10, 80, 20)
Global $terminal = GUICtrlCreateInput("Terminal ", 10, 40, 120, 20)
Global $button = GUICtrlCreateButton("OK", 100, 80, 100, 40)
Global $Velox = GUICtrlCreateCombo("Velocidade", 160, 40, 120, 20, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Global $iDesktopWidth = @DesktopWidth
Global $iDesktopHeight = @DesktopHeight
Global $mouseX = ""
Global $mouseY = ""
GUICtrlSetData(-1, "300K|600K|1MB|2MB|5MB|10MB|15MB|20MB|25MB|30MB|35MB")
Global $dicas = GUICtrlCreateLabel ( "DICAS: ", 10, 150, 120, 20)
Global $dica1 = GUICtrlCreateLabel ( " - Abra o comando CA no STC ", 10, 170, 300, 20)
Global $dica2 = GUICtrlCreateLabel ( " - Ao clicar no botão OK não movimente mais ou mouse ", 10, 190, 300, 30)
Global $dica3 = GUICtrlCreateLabel ( " - A qualquer momento pressione a tecla ESC para parar a Macro ", 10, 210, 300, 30)
HotKeySet("{ESC}", "ExitProg")
Func ExitProg()
    Exit
EndFunc   ;==>ExitProg

Terminal()
Func Terminal()
    GUISetState(@SW_SHOW)
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button
                GUISetState(@SW_HIDE)
                RodarMacro()
        EndSelect
    WEnd
 EndFunc   ;==>Terminal

Func RodarMacro()
    WinActivate("1 - STC (10.31.9.100)")
    WinSetState("1 - STC (10.31.9.100)", "", @SW_MAXIMIZE)
    Send("{F4}")
    Send("x")
    Send("{ENTER}")
    Send("n")
    Send("{ENTER}")
    Sleep (1000)
    Send(GUICtrlRead($sigla))
    Send("{ENTER}")
    Send(GUICtrlRead($terminal))
    Sleep (1000)
    ApertaF4()
EndFunc   ;==>RodarMacro

Func ApertaF4()
        Send("{F4}")
        MouseClick($MOUSE_CLICK_LEFT, 518, 378, 2, 1) ;usar $mouseX e $mouseY
        Send("^c")
        $copia = ClipGet()
    While StringRegExp ( $copia, "Sigla" ) = True
        Sleep (500)
        Send("{F4}")
        MouseClick($MOUSE_CLICK_LEFT, 518, 378, 2, 1) ;usar $mouseX e $mouseY
        Send("^c")
        $copia = ClipGet()
     WEnd
   VelocidadesVelox()
EndFunc   ;==>ApertaF4

Func VelocidadesVelox()
While 1
  $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
          Exit
        Case $Velox
         If GUICtrlRead($Velox) = "300K" Then

         EndIf
         If GUICtrlRead($Velox) = "600K" Then

         EndIf
         If GUICtrlRead($Velox) = "1MB" Then

         EndIf
         If GUICtrlRead($Velox) = "2MB" Then

         EndIf
         If GUICtrlRead($Velox) = "5MB" Then

         EndIf
         If GUICtrlRead($Velox) = "10MB" Then

         EndIf
         If GUICtrlRead($Velox) = "15MB" Then

         EndIf
         If GUICtrlRead($Velox) = "20MB" Then

         EndIf
         If GUICtrlRead($Velox) = "25MB" Then

         EndIf
         If GUICtrlRead($Velox) = "30MB" Then

         EndIf
         If GUICtrlRead($Velox) = "35MB" Then

         EndIf
    EndSwitch
 WEnd
 EndFunc ;==>VelocidadesVelox

 

Link to comment
Share on other sites

16 minutes ago, AzgarD said:

This is how my program looks like, and is not yet finished. But what i need is a way to the AutoIt script recognize the default "Close" button of the window, the regular one in every window on the PC, the "X" next to the minimize and maximize buttons.

Example from my previous post should work for windows that use standard caption buttons.

Link to comment
Share on other sites

  • Developers
24 minutes ago, AzgarD said:

This is how my program looks like, and is not yet finished. But what i need is a way to the AutoIt script recognize the default "Close" button of the window, the regular one in every window on the PC, the "X" next to the minimize and maximize buttons.

You really need to read the advice given in this thread and test for the returned value by GUIGetMsg(). Open the helpfile and start reading how to do it!  ;)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 3 years later...
On 8/21/2017 at 2:08 AM, SlackerAl said:

Here is a simple example to add a close event to your close button:

 

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

GUICreate("Test", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "MenuExit")
GUISetState(@SW_SHOW)

While 1
  Sleep(10)
WEnd

Exit

Func MenuExit()

  GUIDelete()
  Exit

EndFunc

You must have Opt("GUIOnEventMode", 1) in your script or the event traps will not work.

Hope that helps.

Thank you, this was exactly what I needed to know!

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...