gpt4 book ai didi

c# - 如何从一个面板拖放(复制)到另一个面板

转载 作者:太空狗 更新时间:2023-10-30 01:06:14 24 4
gpt4 key购买 nike

我在一个表单上有两个面板。
一个面板有一些控件,如按钮或图像,第二个面板是空的。我想从面板 1 拖动一个控件并将其放到面板 2,但它应该创建控件的副本,并且在拖动矩形时应显示与控件相同的大小,而当拖放到面板 2 中时,拖动的形状应该出现在那里在鼠标位置
其实我想创建一个模拟器之类的东西。在面板 1 中有一些工具,当有人将工具拖放到面板 2 上时,它应该出现在鼠标位置。

语言无关紧要可能是 C#VB.NET

最佳答案

你试过这样的事情吗?

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

Source

关于c# - 如何从一个面板拖放(复制)到另一个面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16165400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com