本文介绍了当拖动移动控件和在C#砸板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要拖上面板的控制,当拖着我想移动的控制,并得到它的位置下降到面板上。我已经尝试了mouseUp事件,鼠标按下,control.But的mousemove事件时,是不是我所期待的。我想火DragDrop事件的面板,然后将控制权。我能做到这一点?如果你可以给我一个想法,这将是巨大的。下面是我的code部分。请指正。非常感谢。

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;

命名空间DragnDrop
{
    公共部分类Form1中:形态
    {

        公共Form1中()
        {
            的InitializeComponent();
        }
        控制mycontrol;
        INT X,Y;
        // Form1中F =新Form1中();
        私人无效Form1_Load的(对象发件人,EventArgs的)
        {

            的foreach(在this.panel1.Controls控制C)
            {
                c.MouseMove + =新MouseEventHandler(lblDragger_MouseMove);
                c.MouseUp + =新MouseEventHandler(lblDragger_MouseUp);
                c.MouseDown + =新MouseEventHandler(pictureBox1_MouseDown);
                c.MouseDoubleClick + =新MouseEventHandler(pictureBox1_MouseDown);
            }
            panel2.AllowDrop = TRUE;
            的foreach(在this.panel2.Controls控制C)
            {
                c.MouseDown + =新MouseEventHandler(pictureBox1_MouseDown);
            }
            panel2.DragOver + =新DragEventHandler(panel2_DragOver);
            panel2.DragDrop + =新DragEventHandler(panel2_DragDrop);
        }

        布尔isDragging;
        INT clickOffsetX;
        INT clickOffsetY;

        私人无效pictureBox1_MouseDown(对象发件人,发送MouseEventArgs E)
        {
            // this.Cursor = Cursors.SizeAll;
            // pictureBox1 =(图片框)发送;
            控制C =发送者作为对照;

            //DoDragDrop(pictureBox1.Image,DragDropEffects.Copy);
            //验证= TRUE;
            isDragging = TRUE;
            clickOffsetX = e.X;
            clickOffsetY = e.Y;
            // c.DoDragDrop(C,DragDropEffects.Move);
        }

        私人无效lblDragger_MouseUp(System.Object的发件人,System.Windows.Forms.MouseEventArgs E)
        {
            isDragging = FALSE;
        }

        私人无效panel2_DragEnter(对象发件人,DragEventArgs E)
        {
            如果(e.Data.GetData present(typeof运算(位图)))
            {
                e.Effect = DragDropEffects.Copy;
            }
            其他
            {
                e.Effect = DragDropEffects.None;
            }
        }

        私人无效panel2_DragOver(对象发件人,DragEventArgs E)
        {
            e.Effect = DragDropEffects.Move;
        }

        私人无效panel2_DragDrop(对象发件人,DragEventArgs E)
        {
            控制C = e.Data.GetData(e.Data.GetFormats()[0])的控制;
            mycontrol = C;
            如果(C!= NULL)
            {
                c.Location = this.panel2.PointToClient(新点(例如,EY));
                this.panel2.Controls.Add(C);
            }
        }

        私人无效lblDragger_MouseMove(System.Object的发件人,
          System.Windows.Forms.MouseEventArgs五)
        {
            控制C =发送者作为对照;
            //布尔isDragging = TRUE;
            如果(isDragging ==真)
            {
                c.Left = e.X + c.Left  -  clickOffsetX;
                c.Top = e.Y + c.Top  -  clickOffsetY;
            }
        }

        私人无效panel1_MouseLeave(对象发件人,EventArgs的)
        {
            控制C =发送者作为对照;

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

解决方案

如果您的控制已经是板上,你只是移动它在同一面板中,然后使用鼠标事件可能是做的最简单的方法。我的理解是,拖放更多的是输送的控制,甚至应用程序之间的数据。拖放将是一个不错的选择,如果你想允许一个控制面板之间的转移,例如。


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

  1. 执行招用你的鼠标事件在同一面板中拖动。

  2. 当你在面板上mouseLeave事件,开始的DragDrop操作(一些例子<一href="http://$c$cbetter.com/blogs/peter.van.ooijen/archive/2007/07/07/basic-drag-and-drop-in-winforms.aspx">here)您可以从面板上的控制或添加某种灰色出效果,表明控制可能会离开。

  3. 在处理的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. Perform move dragging within the same panel using your Mouse events.

  2. 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.

  3. 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#砸板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:41