# File Drag and Drop

> Handle WM_DROPFILES by subclassing: load or insert dropped files at the drop point with InputPosFromPoint and Load, and insert images with ObjectInsertAsChar.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Document Editor > File Drag and Drop
- **1424 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/drag-drop/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/drag-drop/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/drag-drop/llms-full.txt

---

Dragging a file from an Explorer window and dropping it onto a window is a common way in Windows to load a document into a text editor or to insert an image into a document. The Visual Basic sample project below adds this behavior to `TXTextControl`: it loads dropped documents and inserts dropped images at the position where they were dropped.

### How File Drop Works

When a file is dropped on the `TXTextControl` window, Explorer sends a *WM\_DROPFILES* message to it. To implement file drag and drop for TX Text Control, your application must intercept this message. This technique is called subclassing.

In the sample project, the system calls are wrapped in a handler named `OnDropFiles`, which is called whenever a *WM\_DROPFILES* message is trapped.

### Handle the Dropped Files

The dropped files are handled in the following procedure:

```vbscript
Private Sub OnDropFiles(Files() As String, Count As Integer, X As Long, Y As Long)

    Dim iFileType As Integer, i As Integer
    Form1.TXTextControl1.SelStart = Form1.TXTextControl1.InputPosFromPoint(X,Y)
    ' If only one file is dropped, the user can choose how to insert the file
    If Count = 1 Then
        iFileType = GetFileType(Files(0))
        If (iFileType = IMAGE_FILE) Then
            Form1.TXTextControl1.ObjectInsertAsChar 0, Files(0), -1, 100, 100, 0, 1, 0
        ElseIf (iFileType <> 0) Then
            ' Create a PopUp Menu
            Form1.PopupMenu Form1.mnuPopup, 0, X, Y
            ' Either append documents or load the document...
            If (g_CurMenu = 1) Then
                Form1.TXTextControl1.Load Files(0), 0, iFileType, False
            ElseIf (g_CurMenu = 2) Then
                Form1.TXTextControl1.Load Files(0), 0, iFileType, True
            End If
        End If
    Else
        For i = 0 To Count - 1
            iFileType = GetFileType(Files(i))
            If (iFileType = IMAGE_FILE) Then
                Form1.TXTextControl1.ObjectInsertAsChar 0, Files(i), -1, 100, 100, 0, 1, 0
            ElseIf (iFileType <> 0) Then
                Form1.TXTextControl1.Load Files(i), 0, iFileType, True
            End If
        Next i
    End If
    g_CurMenu = 0
End Sub
```

The `OnDropFiles` procedure has these parameters:

| Parameter | Description |
|---|---|
| `Files` | An array of strings with the file names of all files dropped onto the `TXTextControl` window. |
| `Count` | The number of files dropped. |
| `X`, `Y` | The coordinates of the drop event. |

The code works as follows:

- [InputPosFromPoint](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/inputposfrompoint-method/llms-full.txt) converts the drop coordinates into a text position, and assigning it to [SelStart](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/selstart-property/llms-full.txt) moves the current input position to where the files were dropped.
- The handler then checks how many files were dropped.
- **One file.** The user can choose whether to insert the file into the existing text at the current input position or to load it and replace the current contents of the document. A pop-up menu, `mnuPopup`, lets the user decide, and [Load](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/load-method/llms-full.txt) is called with `False` as its last parameter to replace the contents or with `True` to insert the file.
- **Several files.** Each file is inserted at the input position, with `True` as the last parameter of `Load`.
- **Images.** An image isn't loaded with `Load`; the [ObjectInsertAsChar](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/objectinsertaschar-method/llms-full.txt) method inserts it instead.

### Determine the File Type

Before a file is inserted or loaded, the `GetFileType` function of the sample determines its type:

- It returns 0 if the file type isn't known.
- Otherwise it returns the TX Text Control value for the file type, as the format parameter of the [Save](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/save-method/llms-full.txt) method lists them: for example 1 for text, 5 for RTF and 13 for DOCX.
- For an image, it returns 10, the sample's `IMAGE_FILE` value, and the handler calls `ObjectInsertAsChar` instead of `Load`.

### Related Pages

- [Working with Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/load-save/llms-full.txt): the file formats `Load` and `Save` read and write.
- [Using Images](https://docs.textcontrol.com/tx-text-control-activex/document-editor/images/llms-full.txt): inserting images and their attributes.
- [Loading, Saving and Database Support](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/load-save/llms-full.txt): the members that load and save documents.

---

## Nearby

- **Previous:** [Working With Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/load-save/llms-full.txt)
- **Next:** [Document Properties](https://docs.textcontrol.com/tx-text-control-activex/document-editor/properties/llms-full.txt)
