Creating a drag & drop system
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Creating a drag & drop system

  1. #1
    Join Date
    Feb 2006
    Posts
    89

    Creating a drag & drop system

    How can I, in WPF, create an ability to drag a marker onto another window? If I can use the standard drag and drop, then I need a way to handle an event when the drag is completed. (No actual data will be copied, the drag and drop would actually fail in terms of standard usage, but at the ending point I would use a library to record the mouse's position and other information, which is all I need.) If that isn't possible, then how can I intercept mouse events once the mouse has left the window to know when the user has released the button?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Creating a drag & drop system

    In .NET you know when the user has released the mouse because you have assigned a delegate to the Control's DragDrop event.

  3. #3
    Join Date
    Feb 2006
    Posts
    89

    Re: Creating a drag & drop system

    Quote Originally Posted by BigEd781 View Post
    In .NET you know when the user has released the mouse because you have assigned a delegate to the Control's DragDrop event.
    Okay, I've made a WPF window and I've placed several types of controls on it. None of them have a DragDrop event. That only seems to apply to controls in the System.Windows.Forms namespace. Is there an equivalent for WPF?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Creating a drag & drop system

    Ahhh, I was unsure if that would be the case. I do not know WPF, but here is an article:

    http://msdn.microsoft.com/en-us/library/ms742859.aspx

  5. #5
    Join Date
    Feb 2006
    Posts
    89

    Re: Creating a drag & drop system

    Well, it's not at all intuitive, but I think I've found a way to make it work:

    Code:
    public partial class Window1 : Window
    {
            public Window1()
            {
                InitializeComponent();
                this.ellipse1.MouseDown += new MouseButtonEventHandler(ellipse1_MouseDown);
                this.ellipse1.QueryContinueDrag += new QueryContinueDragEventHandler(ellipse1_QueryContinueDrag);
            }
    
            void ellipse1_MouseDown(object sender, MouseEventArgs e)
            {
                DataObject data = new DataObject();
                DragDrop.DoDragDrop(
                    this.ellipse1,
                    data,
                    DragDropEffects.All);
            }
    
            void ellipse1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
            {
                if ((e.KeyStates & DragDropKeyStates.LeftMouseButton) != DragDropKeyStates.LeftMouseButton)
                {
                    // This will happen at the end of the drag
                }
            }
    }
    You'll have to be careful to anticipate other things like mouse clicks (which would also trigger MouseDown), right-drags, and other such things. This is a lot less clean than the DragDrop event, but I guess it gets the job done.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center