5

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

语言无所谓可能是C#VB.NET

4

3 回答 3

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

资源

于 2013-06-01T12:27:42.210 回答
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;  
}
于 2014-10-28T11:27:00.413 回答
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 组件,但用于标准

开发快递
XtraScrollableControl
简单按钮

微软
控制板
按钮
于 2015-06-10T13:20:46.083 回答