Implementing Drag and Drop in Your Windows Application (Cont.) Implementing Drag and Drop for Custom Objects Most Windows Forms controls support the set (or subset) of events that I described in this article for drag and drop operations. However, what happens if the control you want to enable for drag and drop does not support the list of events that I have just described? A good example is the Windows Media Player ActiveX control. You might want to embed the Windows Media Player control in a Windows application so that users can simply drag and drop media files onto it to play. The Windows Media Player ActiveX control by itself does not support events like DragEnter and DragDrop, and hence there is no easy way to implement drag and drop. A workaround is to wrap the ActiveX control using a User control. First, add a new User Control item (right-click the project name in Solution Explorer and select Add > New Item… > select User Control) to the existing project. Name the file as MediaPlayer.vb. Right-click the Toolbox and select Choose Items…. In the Choose Toolbox Items dialog box, click the COM Components tab and check the Windows Media Player object (Figure 10). Click OK to add the Windows Media Player control onto the Toolbox. .png) Figure 10: Adding the Windows Media Player ActiveX control to the Toolbox.Drag the Windows Media Player control from the Toolbox and drop it onto the MediaPlayer.vb design surface (Figure 11). .png) Figure 11: Populate the user control with the Windows Media Player control.In the code behind of MediaPlayer.vb, code the following: Public Class MediaPlayer Private _URL As String Public Property URL() As String Get Return _URL End Get Set(ByVal value As String) _URL = value AxWindowsMediaPlayer1. _ URL = _URL End Set End Property End Class
Essentially you expose the URL property to let the user of this control set the URL of the media file to play. Right-click the project name in Solution Explorer and select Build. The MediaPlayer control should now appear in the toolbox (Figure 12). .png) Figure 12: The MediaPlayer control in the Toolbox.Drag and drop the MediaPlayer user control onto Form1 (Figure 13). .png) Figure 13: Adding the MediaPlayer control to Form1.Switch to the code behind of Form1 and handle the DragEnter event of the MediaPlayer user control (Listing 14). Finally, handle its DragDrop event so that you can play the media file dropped by the user (Listing 15). Note that since the user can drop multiple files onto the control, you will only load the first file using the MediaPlayer control. Figure 14 shows the MediaPlayer control hosted in a Windows Form playing the file dropped onto it. .png) Figure 14: Dragging and dropping a media file onto Form1.Dragging and Dropping Custom Objects So far in this article I’ve shown you how to use the various data types as specified in the DataFormats class: Bitmap, CommaSeparatedValue, Dib, Dif, EnhancedMetafile, FileDrop, Html, Locale, MetafilePict, OemText, Palette, PenData, Riff, Rtf, Serializable, StringFormat, SymbolicLink, Text, Tiff, UnicodeText, and WaveAudio. What happens if you want to drag and drop data of a specific type? For example, you might want to drag an item in a ListView control. In this case, the DragEnter event will look something like this: If (e.Data.GetDataPresent _ ("System.Windows.Forms. _ ListViewItem()")) Then '---determine if this is a copy '---or move--- If (e.KeyState And CtrlMask) = CtrlMask Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.Move End If
The MouseDown event handler will now look like this: Control.DoDragDrop(New _ DataObject("System.Windows.Forms. ListViewItem()", _ Items), DragDropEffects.Move Or _ DragDropEffects.Copy)
Summary It is not really difficult to implement drag and drop functionality in your Windows application. All you need is to understand the type of data you want to support and make the necessary provisions for dealing with that particular data type. Wei-Meng Lee |