| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| Author: | TX Text Control Support Department |
| Language: | Visual Basic |
| Version: | 1.1 |
| Released: | October 02, 2002 |
| Last modified: | January 11, 2008 |
| Requirements: | TX Text Control ActiveX with Visual Basic |
| Download code: | drag_drop_files.zip |
Drag and Drop is an often-used feature in windows environments. Dragging a file from the Explorer window and dropping it into Word is the easiest way to load a document. Here is how you can do that with TX Text Control, too.
It's bit harder to do than other Tips and Tricks we presented. That's because you must intercept a Windows message that the Explorer sends to the TX window. This process is called sub-classing.
The source code shows you how it works, but if you don't understand the whole thing it is strongly advised not to alter the code!
Furthermore, if sub-classing is active and the program causes an error or something similar then VB likes to crash. So save your code often.
The actual drop handling happens in the following handler:
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
It is called when a WM_DROPFILES message is trapped and it capsulates the system calls, so you don't have to bother with these.
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. 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 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 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 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 internal values for different file types. These are 1 for text, 5 for RTF and so on. If an image is dropped, the function returns 10 and not TX's Load function is called but the ObjectInsertAsChar method.