Visual Basic User's Guide
File Drag and Drop is an often-used feature in windows environments. Dragging a file from the Explorer window and dropping it onto a window is the easiest way to load a document in any text editor or to insert images into a document. To implement the functionality for TX Text Control, a Windows message must be intercepted that the Explorer sends to the TX Text Control window when a file is dropped on a window. This process is called sub-classing and the following sample project shows you how to do it.
The actual drop handling happens in the following handler:
[Visual Basic 6] 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 handler is called when a WM_DROPFILES message is trapped and it capsulates the system calls.
The first parameter of the OnDropFiles procedure is an array of strings. It holds the file names of all the files which have been dropped onto the TX Text Control window. The Count parameter tells how many files were dropped and the X and Y parameters hold the coordinates of the drop event.
The first thing that happens in the handler is that the current input position of the TX Text Control window is set to the location were the drop event occured. This can be done using the InputPosFromPoint method. It is then checked how many files are being dropped. If only one file is dropped we can offer the user the choice to insert the file being dropped into the existing text at the current input position or to load the file and replace the current contents of the document. A popup menu is shown to let the user decide.
But before that we must determine the type of the file being dropped. This is done by the GetFileType function. It returns 0, if the file type is not known or the TX Text Control internal values for different file types. These are for example 1 for text, 5 for RTF, 13 for DOCX as shown in the Save method. If an image is dropped, the function returns 10 and instead of TX Text Control's Load function the ObjectInsertAsChar method is called to insert the image.