表格上有两个面板
一个面板有一些控件,如按钮或图像,第二个面板是空的。我想从面板1拖动一个控件并将其拖放到面板2,但是它应该创建一个控件的副本,并且拖动一个矩形应该显示与控件相同的大小,当在面板2中放置时,拖动的形状应该出现在那里在鼠标位置
其实我想创建一个像东西一样的模拟器。在面板1中有一些工具,当有人在面板2上拖放工具时,它应该出现在鼠标位置。
语言无关紧要可能是C#
或VB.NET
答案 0 :(得分:2)
你尝试过这样的事吗?
private void Form5_Load(object sender, EventArgs e)
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
if (c != null)
{
c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(c);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
VB.NET
Private Sub Form5_Load(sender As Object, e As EventArgs)
Me.panel1.AllowDrop = True
For Each c As Control In Me.panel1.Controls
c.MouseDown += New MouseEventHandler(AddressOf c_MouseDown)
Next
Me.panel1.DragOver += New DragEventHandler(AddressOf panel1_DragOver)
Me.panel1.DragDrop += New DragEventHandler(AddressOf panel1_DragDrop)
End Sub
Private Sub c_MouseDown(sender As Object, e As MouseEventArgs)
Dim c As Control = TryCast(sender, Control)
c.DoDragDrop(c, DragDropEffects.Move)
End Sub
Private Sub panel1_DragDrop(sender As Object, e As DragEventArgs)
Dim c As Control = TryCast(e.Data.GetData(e.Data.GetFormats()(0)), Control)
If c IsNot Nothing Then
c.Location = Me.panel1.PointToClient(New Point(e.X, e.Y))
Me.panel1.Controls.Add(c)
End If
End Sub
Private Sub panel1_DragOver(sender As Object, e As DragEventArgs)
e.Effect = DragDropEffects.Move
End Sub
答案 1 :(得分:2)
我改变了一些@Shim的代码。 以下是更新后的代码,其中您的控件副本将放置在另一个面板中
Random rnd = new Random();
private void Form5_Load(object sender, EventArgs e)
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
// Here, you get a copy of your drag drop button and dynamically new button is created
Button btn = new Button();
btn.Name = "Button" + rnd.Next();
btn.Size = c.Size;
if (c != null)
{
// Add the newly created button to you Panel
btn.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(btn);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
答案 2 :(得分:1)
此解决方案将在移动鼠标的同时拖动按钮(或所选的任何其他组件)并将其放置在放置鼠标的位置
private SimpleButton selectedButton;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
xtraScrollableControl2.AllowDrop = true;
xtraScrollableControl2.DragEnter += XtraScrollableControl_DragEnter;
xtraScrollableControl2.DragDrop += XtraScrollableControl_DragDrop;
xtraScrollableControl2.DragOver += XtraScrollableControl_DragOver;
}
private void XtraScrollableControl_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent(typeof(Bitmap)) ? DragDropEffects.Copy : DragDropEffects.None;
}
private void XtraScrollableControl_DragDrop(object sender, DragEventArgs e)
{
var simpleButton = e.Data.GetData(e.Data.GetFormats()[0]) as SimpleButton;
if (simpleButton == null) return;
if (simpleButton.Parent != sender)
{
var btn = new SimpleButton
{
Dock = DockStyle.None,
Size = new Size(125, 50),
Text = simpleButton.Text,
Location = ((XtraScrollableControl) sender).PointToClient(new Point(e.X, e.Y)),
ImageList = simpleButton.ImageList,
ImageIndex = simpleButton.ImageIndex,
ImageLocation = simpleButton.ImageLocation,
Parent = ((XtraScrollableControl)sender)
};
btn.MouseDown += simpleButton_MouseDown;
((XtraScrollableControl)sender).Controls.Add(btn);
}
else
{
((XtraScrollableControl)sender).Controls.Remove(simpleButton);
simpleButton.Location = ((XtraScrollableControl)sender).PointToClient(new Point(e.X, e.Y));
((XtraScrollableControl)sender).Controls.Add(simpleButton);
}
selectedButton = null;
}
private void XtraScrollableControl_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
selectedButton.Location = ((XtraScrollableControl)sender).PointToClient(new Point(e.X, e.Y));
}
private void simpleButton_MouseDown(object sender, MouseEventArgs e)
{
var btn = sender as SimpleButton;
if (btn == null) return;
selectedButton = btn;
btn.DoDragDrop(btn, DragDropEffects.Copy);
}
希望这会有所帮助
我使用了DevExpress组件 但对于标准
DevExpress XtraScrollableControl SimpleButton Microsoft Panel Button