C#:拖放表面上的拖放控件

时间:2009-11-26 10:10:43

标签: c# silverlight user-controls drag-and-drop

有没有办法可以在C#中创建控件,例如文本框,可拖放?

我希望用户能够使用鼠标单击并按住控件并将其拖动到其表面上并将其放在该表面内的任何位置。

任何人都知道如何实现这个目标?

4 个答案:

答案 0 :(得分:2)

This answer帮了我很多忙。它适用于任何类型的Control和Container。

答案 1 :(得分:1)

如果您的控件在一个容器(例如面板)中移动,则可以覆盖OnMouseDown / OnMouseMove事件,并调整控件的Location属性。

根据您的问题,您似乎不需要完全拖放(在不同控件甚至应用程序之间移动数据)。

答案 2 :(得分:0)

如果您尝试从silverlight容器外部拖动项目,那么最好的办法是查看silverlight 4 beta

public MainPage()
 {
     InitializeComponent();
     Loaded += new RoutedEventHandler(MainPage_Loaded);   
     // wire up the various Drop events
     InstallButton.Drop += new DragEventHandler(InstallButton_Drop);
     InstallButton.DragOver += new DragEventHandler(InstallButton_DragOver);
     InstallButton.DragEnter += new DragEventHandler(InstallButton_DragEnter);
     InstallButton.DragLeave += new DragEventHandler(InstallButton_DragLeave);
 }

 void InstallButton_Drop(object sender, DragEventArgs e)
 {
     IDataObject foo = e.Data; // do something with data
 }

答案 3 :(得分:0)

以前在VB6中这么容易。但现在我们真的只有以前称为OleDrag的东西。

无论如何,以下代码应该告诉你如何。您只需要一个标签( dragDropLabel ),并将表单的 AllowDrop 属性( DragDropTestForm )设置为True。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DragDropTest
{
    public partial class DragDropTestForm : Form
    {
        // Negative offset to drop location, to adjust for position where a drag starts
        // on a label.
        private Point _labelOffset;

        // Save the full type name for a label, since this is used to test for the control type.
        private string labelTypeName = typeof(Label).FullName;

        public DragDropTestForm()
        {
            InitializeComponent();
        }

        private void dragDropLabel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _labelOffset = new Point(-e.X, -e.Y);
            }
        }

        private void dragDropLabel_MouseMove(object sender, MouseEventArgs e)
        {
            const double minimumDragDistance = 4;
            const double minimumDragDistanceSquared = minimumDragDistance * minimumDragDistance;

            if (e.Button == MouseButtons.Left)
            {
                // Minimum n pixel movement before drag starts.
                if (((Math.Pow(_labelOffset.X - e.X, 2)) + Math.Pow(_labelOffset.Y - e.Y, 2)) >= minimumDragDistanceSquared)
                {
                    dragDropLabel.DoDragDrop(dragDropLabel, DragDropEffects.Move);
                }
            }       
        }

        private void DragDropTestForm_DragOver(object sender, DragEventArgs e)
        {
            IDataObject data = e.Data;

            string[] formats = data.GetFormats();

            if (formats[0] == labelTypeName)
            {
                e.Effect = DragDropEffects.Move;
            }
        }

        private void DragDropTestForm_DragDrop(object sender, DragEventArgs e)
        {
            IDataObject data = e.Data;

            string[] formats = data.GetFormats();

            if (formats[0] == labelTypeName)
            {
                Label label = (Label) data.GetData(formats[0]);
                if (label == dragDropLabel)
                {
                    Point newLocation = new Point(e.X, e.Y);
                    newLocation.Offset(_labelOffset);
                    dragDropLabel.Location = this.PointToClient(newLocation);
                }
            }
        }
    }
}