Jump to content

Simple Text Drag and Drop


Recommended Posts

I'm trying to set up a simple drag and drop capability between two text fields. I thought this would be an easy one, but I've yet to find an example (except for one dragging List View fields). What I need is the same behavior MSWord provides when dragging text out of it to another application.

Here's the test script I'm working with:

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>

$Form1 = GUICreate("Drag and Drop Test", 600, 500, -1, -1, -1, $WS_EX_ACCEPTFILES)
$Edit1 = GUICtrlCreateEdit("", 22, 40, 240, 400, $ES_WANTRETURN + $ES_MULTILINE + $WS_VSCROLL, $WS_EX_TRANSPARENT )
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUICtrlSetFont(-1, 12, 400) 
$Edit2 = GUICtrlCreateEdit("", 322, 40, 240, 400, $ES_WANTRETURN + $ES_MULTILINE + $WS_VSCROLL, $WS_EX_TRANSPARENT )
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUICtrlSetFont(-1, 12, 400) 

GUISetState(@SW_SHOW)

While 1
   $msg = GUIGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
         ExitLoop
      Case $msg = $GUI_EVENT_DROPPED
;
;    This is where the Insert-type operation is needed <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;
EndSelect
WEnd
Exit

Below is a screen capture to clearly show what I'm trying to accomplish. One major problem is that the highlighted text won't allow itself to be dragged. Clicking on it to start the drag immediately causes a deselect.

Any help will be greatly appreciated.

post-29172-1217546771_thumb.png

Edited by qwert
Link to comment
Share on other sites

From the number of responses, it would appear that dragging text is not so simple.

My research on the MSDN site turned up this summary: MSDN link

In particular, the sections "How Drag and Drop Works" and "Dragging Text" seem to spell it out clearly for VB programmers.

Does anyone know of an equivalent AutoIt3 implementation? Or is there an some overriding reason that the same thing can't be accomplished?

At this point, I guess I'm looking for a little advice before I continue plodding along this course.

Thanks.

Link to comment
Share on other sites

You could write a hack to do it. Something like:

;Inside your GUIGetMsg() Loops
Case ($GUI_EVENT_SECONDARY_DOWN)
     If (WinActive("Your GUI's Title")) Then
          Send("^c") ;To copy highlighted text
     EndIf
Case ($GUI_EVENT_SECONDARY_UP)
     If (WinActive("Your GUI's Title")) Then
          $CopiedText = ClipGet()
          If (Not @error) Then
              $MouseInfo = GUIGetCursorInfo($Your_GUI_Handle)
              If ($MouseInfo[4] == $InputBoxOnRight_Handle) Then
                   GUICtrlSetData($InputBoxOnRight_Handle,$CopiedText)
               EndIf
          EndIf
     EndIf

Ugly, but it would work (well, check syntax cuz that's 100% memory).

-CMR

Link to comment
Share on other sites

I've done something like this before, however I just placed an "Add" and "Remove" button in the middle. This is what I have seen with most programs

this is also something in Welcome to Autoit 1-2-3

Posted Image

; includes
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <file.au3>

; create the GUI.
$win = GUICreate("File List/View Demo", 614, 370)
; set the font for the GUI
GUISetFont(9, 400, -1, "MS Sans Serif")
; create buttons.
$btnList = GUICtrlCreateButton("&List Files", 10, 330, 75, 25)
$btnView = GUICtrlCreateButton("&View File", 85, 330, 75, 25)
; create the left list.
$TutorItList = GUICtrlCreateList("", 10, 10, 150, 330)
; create the right edit.
$TutorItEdit = GUICtrlCreateEdit("Please select a tutorial from the list to your left.", 175, 10, 420, 345, $ES_AUTOVSCROLL + $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL)
; set the edit colors.
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetColor(-1, 0x000000)
; set focus to the edit.
GUICtrlSetState($TutorItList, $GUI_FOCUS)
; show the GUI.
GUISetState()

; start the loop.
While 1
    ; listen for a message
    $msg = GUIGetMsg()
    ; using select/case for the message
    Select
        Case $msg = $GUI_EVENT_CLOSE 
            Exit
        Case $msg = $btnList
            Set_tutor()
        Case $msg = $btnView
            View_tutor()
    ; end the selections        
    EndSelect
    
WEnd

; Function to populate the left list.
Func Set_tutor()
    $TutList = _FileListToArray (@HomeDrive & "\", "*.txt", 1) ; list files to an array.
    If (Not IsArray($TutList)) Or (@error = 1) Then
        MsgBox(262208, "Tutor Error", "No Files\Folders Found.   ", 5)
        Return
    EndIf
    GUICtrlSetData($TutorItList, "") ; set list to empty.
    For $x = 1 To $TutList[0] ; for loop to place the files in the list.
        GUICtrlSetData($TutorItList, (StringTrimRight($TutList[$x], 4)) & "|", 1) ; string trim the last 4 characters ( .txt )
    Next
EndFunc   

; Function to populate the right edit.
Func View_tutor()
    $s_text = GUICtrlRead($TutorItList) ; read the selected file to a variable.
    If $s_text = "" Then Return
    $s_text = @HomeDrive & "\" & $s_text & ".txt" ; set the location of the file.
    Dim $Tut_text
    If Not _FileReadToArray($s_text, $Tut_text) Then ; read the file to an array.
        MsgBox(4096, "Tutor Error", " Error reading log to Array     error:" & @error)
        Return
    EndIf
    GUICtrlSetData($TutorItEdit, "") ; set the edit to empty.
    For $x = 1 To $Tut_text[0] ; for loop to place the read file into the edit.
        GUICtrlSetData($TutorItEdit, $Tut_text[$x] & @CRLF, 1)
    Next
EndFunc   

; Note
; file read to array, reads the file
; file list to array, lists the files

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks for both responses.

I'll start working with something along the lines of detecting mouse clicks and then cut/paste accordingly. Although I'll just be emulating the Ctrl-C and Ctrl-V operations, I need the user to think they're dragging/dropping -- which is why I want to avoid having dedicated buttons.

At this point, I don't know how to detect that a block of text is already highlighted -- or how to determine the coordinates of such a block so as to know when the mouse is clicked within that block. But I'm learning as I go.

Any further suggestions will be appreciated, of course.

Link to comment
Share on other sites

Actually, I worked a bit with Martin's example a few days ago and was able to see how it can receive text dragged from an external window. I've also been able to set up a control to receive the entire contents of a text file by just dragging the file name in. So the real challenge remains on the drag side of the operation. To recap from my original post:

One major problem is that the highlighted text won't allow itself to be dragged. Clicking on it to start the drag immediately causes a deselect.

Thanks for the response.

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