Jump to content

MessageLoop/OnEvent mode


Recommended Posts

I managed to get a wee bit of code that allows user to drag files unto a

"GUICtrlCreateInput" control using $GUI_ACCEPTFILES.

It has been written in MessageLoop mode.

I understand it ok! but i'm a wee bit confused about how it would work in OnEvent mode.

Could some one tell me or point me in the right direction as to how this could be re-written in "OnEvent mode"

(I'm writing a gui script that allows user to drag files which will then be ftp'd)

Thanks in advance

Link to comment
Share on other sites

Anyone feel free to step in and correct or add to what i say.

The main difference between MessageLoop and OnEvent is how they recieve the messages.

MessageLoop:

While 1

$msg = GUIGetMsg()

...

...

WEnd

OnEvent

While 1

Sleep(1000) ; Just idle around

WEnd

You can look on the help file under GUI Reference but i'll give you a brief summary. The MessageLoop polls for a $msg (interaction) and when it receives it it this goes into either switch or if/else block which determines what the message is and how it should be handled. OnEvent on the other hand usually doesn't (but can) have anything in the main loop. As soon as an event or interaction with the program happens the loop breaks and it goes to a predefined function to handle that type of event. I have noticed on my programs that OnEvent uses less processor but its not that much to begin with.

You can set which mode you want to use by the Opt("GUIOnEventMode", 1/0). All of this is in the section i told you about earlier. Please look throught the help file before posting next time. It is a very useful guide that took some time to create and it was designed for people like you who had questions about the code. I use it everytime i code as a reference guide.

Also as to converting your code. If you posted it, someone might be nice enough to do it for you or at least get you started in the right direction.

Hope that helps.

-Aaron

Edited by dumdum8684
Link to comment
Share on other sites

yes thanks i have seen the help file under GUI Reference and i do understand

both events. BBBBut for this particular piece of code i'm a weee bit confused!

#include <GUIConstants.au3>

Dim $answer = ""

GUICreate(" My GUI input acceptfile", 320,120)

$file = GUICtrlCreateInput ( "", 10, 25, 300, 20)

GUICtrlSetState(-1,$GUI_ACCEPTFILES)

GUISetState ()

$msg = 0

While 1

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then

Exit

EndIf

$answer = GUICtrlRead($file); reads the drag & drop box every second

If $answer <> "" Then

Sleep(200); give time for mouse to actually let-go of the dropped file

If FileExists($answer) Then;checks that the file exists

MsgBox (4096, "drag drop file", $answer)

; call "read file" function

Sleep(1000); remove this sleep when you have your read function working

Else

MsgBox (4096, "drag drop file", "file was not found")

EndIf

EndIf

Sleep(1000)

Wend

Link to comment
Share on other sites

yes thanks i have seen the help file under GUI Reference and i do understand

both events. BBBBut for this particular piece of code i'm a weee bit confused!

#include <GUIConstants.au3>

Dim $answer = ""

GUICreate(" My GUI input acceptfile", 320,120)

$file = GUICtrlCreateInput ( "", 10,  25, 300, 20)

GUICtrlSetState(-1,$GUI_ACCEPTFILES)

GUISetState ()

$msg = 0

While 1

    $msg = GUIGetMsg()

   

    If $msg = $GUI_EVENT_CLOSE Then

        Exit

    EndIf

   

    $answer = GUICtrlRead($file); reads the drag & drop box every second

    If $answer <> "" Then

        Sleep(200); give time for mouse to actually let-go of the dropped file

        If FileExists($answer) Then;checks that the file exists

            MsgBox (4096, "drag drop file", $answer)

        ; call "read file" function

            Sleep(1000); remove this sleep when you have your read function working

        Else

            MsgBox (4096, "drag drop file", "file was not found")

        EndIf

    EndIf  

   

    Sleep(1000) 

Wend

<{POST_SNAPBACK}>

here ya go... fyi, i couldn't get the drag and drop functionality to work, but i didn't troubleshoot that, just switched it over to on-event mode, moved the bulk of your statements to a function called by any event from your input box, and made a close function and event...

#include <GUIConstants.au3>
OPT("GUIOnEventMode", 1)
Global $answer = ""
GUICreate(" My GUI input acceptfile", 320,120)

$file = GUICtrlCreateInput ( "", 10,  25, 300, 20)
GUICtrlSetOnEvent($file,"Dropped")
GUICtrlSetState(-1,$GUI_ACCEPTFILES)
GUISetState () 
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

While 1
SLEEP(1000)
WEnd
$msg = GUIGetMsg()
    
Func Dropped()
 
    $answer = GUICtrlRead($file); reads the drag & drop box every second
    If $answer <> "" Then
        Sleep(200); give time for mouse to actually let-go of the dropped file
        If FileExists($answer) Then;checks that the file exists
            MsgBox (4096, "drag drop file", $answer)
       ; call "read file" function
            Sleep(1000); remove this sleep when you have your read function working
        Else
            MsgBox (4096, "drag drop file", "file was not found")
        EndIf
    EndIf   
    
    Sleep(1000)  
EndFunc
Func CLOSEClicked()
    Exit
    EndFunc
Edited by cameronsdad
Link to comment
Share on other sites

This should do what you want it to do. This should help more than my earlier advice.

:)

#include <GUIConstants.au3>
OPT("GUIOnEventMode", 1)
GUICreate(" My GUI input acceptfile", 320,120,-1,-1, -1, $WS_EX_ACCEPTFILES)
$file = GUICtrlCreateInput ( "", 10, 25, 300, 20)
GUICtrlSetState(-1, $GUI_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUISetState () 


While 1
    If GUICtrlRead($file) <> "" Then
        Dropped()
    EndIf 
    SLEEP(1000)
WEnd

Func Dropped()
    Sleep(200); give time for mouse to actually let-go of the dropped file
    MsgBox (4096, "drag drop file", GUICtrlRead($file))
; call "read file" function
EndFunc

Func CLOSEClicked()
    Exit
EndFunc

Also its a little cleaner than the earlier example

Edited by dumdum8684
Link to comment
Share on other sites

in my defense, i didn't write that code, all i did was move his code arround because he asked how to make it event mode, not for suggestions on the actual code.

<{POST_SNAPBACK}>

Its ok i wasn't attacking you and i guess it did seem like that a little. I just spent a little time troubleshooting your code and cleaning it up a little.

-Aaron

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...