Jump to content

Recommended Posts

Posted

Hi  i have this code

#include <GUIConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Sound.au3>

Opt("GUIOnEventMode", 1)

Global Const $SC_DRAGMOVE = 0xF012
Global $sIniPath = @ScriptDir & "\Config.ini"
Global $idLabel
Global $sAlertFile
Global $btnExit
Global $AllertNotify
Global $bExit = False

main()

While Not $bExit
    GUIGetMsg() ; <-- fondamentale per gestire eventi GUI in GUIOnEventMode=1
    Sleep(100)
WEnd

Func main()
    ; === Leggi percorso MT5 dal file ini ===
    Local $mt5ExchangePath = IniRead($sIniPath, "MT5_path_exchange", "MT5Pth", "")
    If $mt5ExchangePath = "" Then
        MsgBox($MB_ICONERROR, "Errore", "Percorso MT5 mancante nella sezione [MT5_path_exchange] del Config.ini")
        Exit
    EndIf

    $sAlertFile = $mt5ExchangePath & "\NoSoundAlerts.txt"

    ; === Ottieni handle finestra MT5 via CLASS ===
    Local $hMT5 = WinGetHandle("[CLASS:MetaQuotes::MetaTrader::5.00]")
    If @error Or $hMT5 = 0 Then
        MsgBox($MB_ICONERROR, "Errore", "Impossibile trovare la finestra di MT5.")
        Exit
    EndIf

    ; === Layout finestra GUI ===
    Local $btnHeight = 30
    Local $guiWidth = 583
    Local $aRes = WinGetClientSize("[CLASS:Progman]")
    Local $posX = $aRes[0] - $guiWidth - 240
    Local $posY = $aRes[1] - $btnHeight - 65

    ; === Crea GUI figlia della finestra MT5 ===
    $AllertNotify = GUICreate("Alert Viewer", $guiWidth, $btnHeight, $posX, $posY, BitOR($WS_POPUP, $WS_BORDER), -1, $hMT5)

    ; === Label dinamica ===
    $idLabel = GUICtrlCreateLabel("In attesa di segnali...", 5, 5, $guiWidth - 70, $btnHeight, $SS_LEFTNOWORDWRAP)
    GUICtrlSetFont($idLabel, 9, 700, 0, "Segoe UI")

    ; === Bottone Exit ===
    $btnExit = GUICtrlCreateButton("Exit", $guiWidth - 60, 5, 55, 20)

    ; === Mostra GUI ===
    WinSetOnTop($AllertNotify, "", 1)
    GUISetState()

    ; === Eventi GUI ===
    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")
    GUISetOnEvent($btnExit, "OnExit")

    ; === Avvia monitoraggio file ===
    AdlibRegister("AttachToMT5", 500)
EndFunc

Func AttachToMT5()
    Static $sLastMessage = ""

    If FileExists($sAlertFile) Then
        Local $sContent = FileRead($sAlertFile)
        If Not @error And StringStripWS($sContent, 3) <> "" Then
            If $sContent <> $sLastMessage Then
                GUICtrlSetData($idLabel, $sContent)
                $sLastMessage = $sContent

                ; Suono quando arriva nuova alert
                Beep(500, 1000) ; oppure usa SoundPlay per suoni personalizzati
                ; SoundPlay(@WindowsDir & "\Media\notify.wav", 1)
            EndIf

            ; Svuota il file dopo lettura
            Local $hFile = FileOpen($sAlertFile, 2)
            If $hFile <> -1 Then
                FileWrite($hFile, "")
                FileClose($hFile)
            EndIf
        EndIf
    EndIf
EndFunc

Func OnExit()
    ; Ferma il timer Adlib
    AdlibUnRegister("AttachToMT5")

    ; Chiudi la GUI
    GUIDelete($AllertNotify)

    ; Imposta flag per uscita dal ciclo
    $bExit = True
EndFunc

why if i push button close it not close app ?  ramaning in loop ?

Posted
1 hour ago, MoriceGrene said:
    GUIGetMsg() ; <-- fondamentale per gestire eventi GUI in GUIOnEventMode=1

"essential to handle GUI events in GUIOnEventMode=1". Is not needed when using GUIOnEventMode.

1 hour ago, MoriceGrene said:
Func OnExit()
    ; Ferma il timer Adlib
    AdlibUnRegister("AttachToMT5")

    ; Chiudi la GUI
    GUIDelete($AllertNotify)

    ; Imposta flag per uscita dal ciclo
    $bExit = True
EndFunc

you can just:

Func OnExit()
;~  ; Stop Adlib timer
;~  AdlibUnRegister("AttachToMT5")
;~  ; Close GUI
;~  GUIDelete($AllertNotify)
;~  ; Set flag to exit loop
;~  $bExit = True
    Exit
EndFunc

..welcome to the forum :ILA2:

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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
  • Recently Browsing   0 members

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