XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Question Drag and Drop Functionality

Last post 11/25/2008 4:33 AM by Rupe. 2 replies.
  • 11/22/2008 8:18 AM

    Question Drag and Drop Functionality

    The image is my editor currently

    From XNA Forum

    I want to be able to
    • Drag the selected item from the list to the map screen
    • Drop the associated Sprite/Model onto the draw screen (the draw code isn't the issue)

    After that I pretty much got a handle on how it should work.
  • 11/22/2008 8:49 AM In reply to

    Re: Question Drag and Drop Functionality

    Drag and drop is a fairly simple functionality:

    1) You need a system for your mouse to track position and watch for mouse being pressed down and let up.
    2) When it's pressed down check if it was pressed on a control which contains something that can be dragged, perhaps an interface IDragTarget with a method GetDragObject.
    3) When that GetDragObject (or whatever your preferred implimentaiton is) is called by the mouse tracker system, attach it to the mouse cursor through a tag property of the type object (to provide some universal functionality).
    4) Set up in the area you want to allow it to be dropped an interface similar to the one described earlier, IDropTarget which contains a method GiveDropObject to accept or reject the drop based on the object type.

    That's just off the top of my head so I'm unsure how well it'd really work, but sounds sensible and doable.
  • 11/25/2008 4:33 AM In reply to

    Re: Question Drag and Drop Functionality

    Most Windows Forms objects in C# have an AllowDrop property, this must be set in order for that to work. You must also provide methods for the Drag Drop events, ie - DragEnter and DragDrop.
    Here's an example for dragging an image file onto a PictureBox (I apologize for the formatting):
    private void pictureBox1_DragEnter( object sender, DragEventArgs e )
    {
    // check drop data type
    if ( e.Data.GetDataPresent( DataFormats.FileDrop, false )
    {
    e.Effect = DragDropEffects.All;
    }
    }
    private void pictureBox1_DragDrop( object sender, DragEventArgs e )
    {
    // get dropped items
    string[ dropped = (string[)e.Data.GetData( DataFormats.FileDrop );
    // only inspect the first item
    FileInfo fileInfo = new FileInfo( dropped[0] );
    // make sure it's a valid file
    if ( fileInfo.Exists )
    {
    // make sure it's a bitmap
    if ( fileInfo.Extension.ToLower() == ".png" )
    {
    // details elided
    }
    }
    }

    You'll also need to include an implementation for MouseDown for your control (in this case, your list)
    // this is a bad example for you, but works for selecting the background image of a panel for drag/drop
    private void panel_MouseDown(object sender, MouseEventArgs e)
    {
    Panel source = (Panel)sender;
    DoDragDrop( source.BackgroundImage, DragDropEffects.Copy );
    }
Page 1 of 1 (3 items) Previous Next