Creating a Drop Handler

We need to add a new class to the RadEx project that will implement IPersistFile and IDropTarget. Call the file clsDropHandler.cls. Its declarations section begins as follows:

'clsDropHandler.cls

Implements IDropTarget
Implements IPersistFile

Implementing IPersistFile

The only method that we need to implement on this interface is Load. The code is the same as the code we used to create the icon handler back in Chapter 5. Example 7.1 contains the implementation.

Example 7-1. IPersistFile::Load Implementation

'clsDropHandler.cls

Private m_sTargetFile As String

Private Sub IPersistFile_Load(
    ByVal pszFileName As VBShellLib.LPCOLESTR, _ 
    ByVal dwMode As VBShellLib.DWORD)

    m_sTargetFile = Space(255)
    CopyMemory ByVal StrPtr(m_sTargetFile), _ 
               ByVal pszFileName, _ 
               Len(m_sTargetFile)
    
End Sub

Implementing IDropTarget

The most interesting aspect of implementing the IDropTarget interface (and, in particular, its DragEnter method) concerns the POINTL parameter to the DragEnter method. Notice from our earlier presentation of the method’s syntax that it is an [in] parameter; therefore, it is not a pointer. We have a slight problem here, because Visual Basic does not allow UDTs to be passed ByVal, which is what is going on here. POINTL is a structure that contains the location of the mouse in the drop area. It is defined like this:

typedef struct _POINTL { 
    LONG x; 
    LONG y; 
} POINTL;

Fortunately, we do not need this point information, so there is a simple workaround. Instead ...

Get VB Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.