C#:曲面上的拖放控件
有没有一种方法可以使控件(如文本框)在C#中可拖放 我希望用户能够用鼠标点击并按住控件,并将其拖动到其表面上,然后将其放置在该表面内的任何位置C#:曲面上的拖放控件,c#,silverlight,user-controls,drag-and-drop,C#,Silverlight,User Controls,Drag And Drop,有没有一种方法可以使控件(如文本框)在C#中可拖放 我希望用户能够用鼠标点击并按住控件,并将其拖动到其表面上,然后将其放置在该表面内的任何位置 有人知道如何实现这一点吗?如果控件在一个容器(例如面板)内移动,则可以覆盖OnMouseDown/OnMouseMove事件,并调整控件的Location属性 根据您的问题,您似乎不需要完全拖放(在不同的控件甚至应用程序之间移动数据)。如果您试图从silverlight容器外部拖动项目,那么最好的选择是签出 帮了我很多忙。它在任何类型的控件和容器上都非常
有人知道如何实现这一点吗?如果控件在一个容器(例如面板)内移动,则可以覆盖OnMouseDown/OnMouseMove事件,并调整控件的Location属性
根据您的问题,您似乎不需要完全拖放(在不同的控件甚至应用程序之间移动数据)。如果您试图从silverlight容器外部拖动项目,那么最好的选择是签出
帮了我很多忙。它在任何类型的控件和容器上都非常有效。这在VB6中曾经非常容易。但现在我们只有以前被称为OleDrag的东西 无论如何,下面的代码应该向您展示如何使用。您只需要一个标签(dragDropLabel),并将表单(DragDropTestForm)的AllowDrop属性设置为True
您使用的是asp.net还是桌面应用程序?我希望在Silverlight应用程序中实现,如果可能的话??是的,我不需要在任何控件或应用程序之间移动数据,只需在其容器中移动控件即可…好的-这是我的想象,还是这本来与Silverlight无关?在这种情况下,忽略这一点-这只是WinForms。
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
}
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);
}
}
}
}
}