在 C# 中的面板上拖放时移动控件

Move controls when Drag and drop on panel in C#(在 C# 中的面板上拖放时移动控件)
本文介绍了在 C# 中的面板上拖放时移动控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想拖动面板上的控件,拖动时我想移动控件并将其位置放到面板上.我已经尝试过 mouseUp、mouseDown、MouseMove 控制事件.但这不是我想要的.我想在面板上触发 DragDrop 事件并移动控件.我可以这样做吗?如果你能给我一个想法,那就太好了.以下是我的代码的一部分.请纠正我.非常感谢.

I want to drag controls on panel and when dragging I want to move the control and get its location to drop on to panel. I have tried out mouseUp, mouseDown, MouseMove events of control.But that is not what I am looking for. I want to fire DragDrop event on panel and move control. Can I do this? If you can give me an idea it will be great. Below is part of my code. Please correct me. Thanks a lot.

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 DragnDrop
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        Control mycontrol;
        int x, y;
        //Form1 f = new Form1();
        private void Form1_Load(object sender, EventArgs e)
        {

            foreach (Control c in this.panel1.Controls)
            {
                c.MouseMove += new MouseEventHandler(lblDragger_MouseMove);
                c.MouseUp += new MouseEventHandler(lblDragger_MouseUp);
                c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
                c.MouseDoubleClick += new MouseEventHandler(pictureBox1_MouseDown);
            }
            panel2.AllowDrop = true;
            foreach (Control c in this.panel2.Controls)
            {
                c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            }
            panel2.DragOver += new DragEventHandler(panel2_DragOver);
            panel2.DragDrop += new DragEventHandler(panel2_DragDrop);  
        }

        bool isDragging ;
        int  clickOffsetX ;
        int  clickOffsetY ;

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //  this.Cursor = Cursors.SizeAll;
            //pictureBox1 = (PictureBox)sender;
            Control c = sender as Control;

            //DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
            // validation = true;
            isDragging = true;
            clickOffsetX = e.X;
            clickOffsetY = e.Y;
            //  c.DoDragDrop(c, DragDropEffects.Move);  
        }

        private void lblDragger_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDragging = false;
        }

        private void panel2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Bitmap)))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void panel2_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;  
        }

        private void panel2_DragDrop(object sender, DragEventArgs e)
        {
            Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
            mycontrol = c;
            if (c != null)
            {
                c.Location = this.panel2.PointToClient(new Point(e.X, e.Y));
                this.panel2.Controls.Add(c);
            }  
        }

        private void lblDragger_MouseMove(System.Object sender,
          System.Windows.Forms.MouseEventArgs e)
        {
            Control c = sender as Control;
            // bool isDragging = true;
            if (isDragging == true)
            {
                c.Left = e.X + c.Left - clickOffsetX;
                c.Top = e.Y + c.Top - clickOffsetY;
            }
        }

        private void panel1_MouseLeave(object sender, EventArgs e)
        {
            Control c = sender as Control;

            c.DoDragDrop(c, DragDropEffects.Move); 
        }
    }
}

推荐答案

如果您的控件已经在面板上并且您只是在同一个面板中移动它,那么使用鼠标事件可能是最简单的方法.我的理解是拖放更多的是关于在控件甚至应用程序之间传递数据.例如,如果您试图允许控件在面板之间传输,拖放将是一个很好的选择.

If your control is already on the panel and you're simply moving it within the same panel, then using the Mouse events is probably the easiest way to do this. My understanding is that Drag and Drop is more about conveying data between controls or even applications. Drag and drop would be a good fit if you're trying to allow a control to transfer between panels, for example.

如果你想两者都做,那么这里有一个可能的想法:

If you want to do both, then here's one possible idea:

  1. 使用鼠标事件在同一面板内执行移动拖动.

  1. Perform move dragging within the same panel using your Mouse events.

当您在面板上收到 MouseLeave 事件时,开始 DragDrop 操作(一些示例 here) 您可以从面板中删除控件或添加某种灰显"效果来表明控件可能要离开了.

When you get a MouseLeave event on the panel, begin a DragDrop operation (some examples here) You can either remove the control from the panel or add some sort of 'gray out' effect to indicate that the control may be leaving.

处理目标面板上的 DragDrop 并将控件放置在拖放的鼠标位置.

Handle the DragDrop on your target panel and place the control at the mouse location of the drop.

这结合了拖动控件的直观感觉,同时还提供了一种越过"面板并拖动到新表面的方法.

This combines the intuitive feel of dragging the control around, while also providing a way to drag 'past' the panel and on to a new surface.

这篇关于在 C# 中的面板上拖放时移动控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)