Jump to content

Drag & Drop then copy file (Solved)


Recommended Posts

I'm trying to create an interface where users drag a file (a picture) into the window, and the file is copied to a new location for further use in the main program.

Func drag() ;window for receiving drag and drop pictures.
    $winhand2 = GUICreate("Drag Photo Here",200,200,0,-1,-1, $WS_EX_ACCEPTFILES)
    $file=0
    $edit1=GUICtrlCreateEdit("", 10,30,180,160,$ES_AUTOVSCROLL)
    GUICtrlSetState(-1,$GUI_DROPACCEPTED)
    
    GUISetState()
    
    while (1)
        
        $msg=Guigetmsg()
        
        switch $msg
            
        case $GUI_EVENT_CLOSE
            ExitLoop
            
        case $GUI_EVENT_DROPPED
            MsgBOx(64,"",@GUI_DRAGFILE) ;shows me that the correct filepath was imported
            $ee=FileCopy(@GUI_DRAGFILE,@MyDocumentsDir & "\tempscr.bmp",9)
            MsgBOx(64,"",$ee) ;tells me if the filecopy fails, it always does
            ExitLoop
        EndSwitch
    Wend
    
        GUIDelete() 
EndFunc

The problem is, the script will not copy the file. It has the correct filepath but the copy always fails. It's as if the file is being used and can't be copied when you drag and drop the file.

Solution: @GUI_DRAGFILE contains a @CRFL which needed to be removed before FileCopy can find the source.

FileCopy(StringReplace(@GUI_DRAGFILE,@CRLF,""),@MyDocumentsDir & "\tempscr.bmp",9)

Thanks Aipion!

Edited by edthedestroyer
Link to comment
Share on other sites

When you Drag & Drop a file, the @GUI_DRAGFILE has the file path with a @CRLF. so you have to just remove it then it will work.

This should Work:

Func drag() ;window for receiving drag and drop pictures.
    $winhand2 = GUICreate("Drag Photo Here",200,200,0,-1,-1, $WS_EX_ACCEPTFILES)
    $file=0
    $edit1=GUICtrlCreateEdit("", 10,30,180,160,$ES_AUTOVSCROLL)
    GUICtrlSetState(-1,$GUI_DROPACCEPTED)
   
    GUISetState()
   
    while (1)
       
        $msg=Guigetmsg()
       
        switch $msg
           
        case $GUI_EVENT_CLOSE
            ExitLoop
           
        case $GUI_EVENT_DROPPED
            MsgBOx(64,"",@GUI_DRAGFILE) ;shows me that the correct filepath was imported
            $ee=FileCopy(StringReplace(@GUI_DRAGFILE,@CRLF,""),@MyDocumentsDir & "tempscr.bmp",9)
            MsgBOx(64,"",$ee) ;tells me if the filecopy fails, it always does
            ExitLoop
        EndSwitch
    Wend
   
        GUIDelete()
EndFunc
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...