How to provide file drag-and-drop functionality in a Visual C++ .NET application or in a Visual C++ 2005 application

For a Microsoft Visual Basic .NET version of this article, see
306969 .
For a Microsoft Visual C# .NET version of this article, see
307966 .
This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System::ComponentModel
  • System::Collections
  • System::Windows::Forms
  • System::Data
  • System::Drawing

IN THIS TASK

Summary

This step-by-step article describes how to provide file drag-and-drop functionality in a Microsoft Visual C++ .NET application. A ListBox control is used as the destination of the file drag-and-drop procedure.

Back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005
  • Microsoft .NET Framework 1.1
This article assumes that you are familiar with the following topics:
  • Windows Forms ListBox control
  • Windows Forms event handling
Back to the top

Steps to build the sample

The ListBox control provides two drag-and-drop events: DragEnter and DragDrop. The DragEnter event occurs when you drag an object in the bounds of the control and is used to determine whether the object that is being dragged is one that you can drop on the control. You use the DragEnter event when you drag a file or files to the control. The Windows form displays the appropriate icon when the object is dragged over the control, depending on the object that is being dragged. The DragDrop event occurs when you release the object that is being dragged on the control. You use the DragDrop event to retrieve the object. You use the Data object to retrieve the data.

The GetData method of the Data object returns an array of strings that contain the full path names of the files that were dragged to the ListBox control. You can use this file path information to perform operations on the files. For example, you can use classes in the System::IO namespace to open and read the files, to move the files, or to copy the files to a new location. In this example, you add the full path of the files that are dragged to the ListBox control.

To provide file drag-and-drop functionality in a Visual C++ .NET or Visual C++ 2005 application, follow these steps:
  1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
  2. On the File menu, point to
    New, and then click Project.
  3. Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.

    Note In Visual Studio 2005, Visual C++ Projects is changed to Visual C++ and Windows Forms Application (.NET) is changed to Windows Forms Application.
  4. In the Name box, type
    DragDrop, and then click OK. By default, the Form1 form is created, and is opened in Design mode.
  5. Add a ListBoxcontrol to Form1.
  6. Right-click listBox1, and then click
    Properties.
  7. Change the AllowDrop property to
    True.
  8. Click the Events button.
  9. Double-click the DragEnter event to add a
    listBox1_DragEnter event handler to the code window.
  10. Add the following code in the
    listBox1_DragEnter event handler.
    if(e->Data->GetDataPresent(DataFormats::FileDrop))
    e->Effect = DragDropEffects::All;
    else
    e->Effect = DragDropEffects::None;
  11. On the View menu, click
    Designer to switch to Design mode.
  12. Right-click listBox1, and then click
    Properties.
  13. Click the Eventsbutton.
  14. Double-click the DragDrop event to add a
    listBox1_DragDrop event handler to the code window.
  15. Add the following code in the
    listBox1_DragDrop event handler.
    String *s[] = (String *[]) e->Data->GetData(DataFormats::FileDrop, false);
    int i;
    for(i = 0; i < s->Length; i++)
    listBox1->Items->Add(s[i]);
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:

    1. Click Project, and then click <ProjectName> Properties.



      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click

      General.

    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the

      Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx
  16. Press the CTRL+SHIFT+S key combination to save the project.
  17. Press the CTRL+SHIFT+B key combination to build the solution.
  18. Press the CTRL+F5 key combination to run the project.
  19. Drag one or more files from the desktop or from another folder to the ListBox control. Notice that the full path of the files is added to the ListBox control.
Back to the top

References

Propriedades

ID do Artigo: 815667 - Última Revisão: 7 de dez de 2007 - Revisão: 1

Comentários