File Drag and Drop
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:
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 converts the drop coordinates into a text position, and assigning it to SelStart 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 is called withFalseas its last parameter to replace the contents or withTrueto insert the file. - Several files. Each file is inserted at the input position, with
Trueas the last parameter ofLoad. - Images. An image isn't loaded with
Load; the ObjectInsertAsChar 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 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_FILEvalue, and the handler callsObjectInsertAsCharinstead ofLoad.