拖动多个选定的控件 [英] Drag multiple selected controls

查看:95
本文介绍了拖动多个选定的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms 表格 A 用户控件,其中多个实例可以动态创建。如果我选择它,我可以拖动它。现在,我想,如果我选择了多个用户控件(用 CTRL +点击按钮),以能够拖动所有的人都在同一时间。

我可以这样做我做一个用户控件?

我已经试过至今:

  //对于拖着我用这个方法,也是我有
//覆盖的MouseUp,的MouseDown,的MouseMove从.NET
公共无效的startDrag()
{
    拖着= TRUE;
    点p = PointToClient(Cursor.Position);
    的dragstart =新的点(Math.Max​​(0,PX),Math.Max​​(0,PY));
    buttondrag.Capture = TRUE;
}

私人无效Usercontrol1_SelectedChanged(对象发件人,EventArgs的)
{
    如果(((用户控件)发送方).Selected)
    {
        如果(SelectedUserControl.Count→1)
        {
            的foreach(在panel1.Controls用户控件C)
            {
                c.StartDrag();
            }
        }
    }
}
 

解决方案

//把你的控制面板中,并使用这个类。

 类ControlMover
{
    公共枚举方向
    {
        任何,
        水平,
        垂直
    }

    公共静态无效初始化(控制控制)
    {
        初始化(控制,Direction.Any);
    }

    公共静态无效初始化(控制控制,指示方向)
    {
        初始化(控制,控制,方向);
    }

    公共静态无效初始化(调节控制,控制箱,方向方向)
    {
        布尔拖移= FALSE;
        点的dragstart = Point.Empty;
        control.MouseDown + =委托(对象发件人,发送MouseEventArgs E)
        {
            拖动= TRUE;
            的dragstart =新的点(e.X,e.Y);
            control.Capture = TRUE;
        };
        control.MouseUp + =委托(对象发件人,发送MouseEventArgs E)
        {
            拖动= FALSE;
            control.Capture = FALSE;
        };
        control.MouseMove + =委托(对象发件人,发送MouseEventArgs E)
        {
            如果(拖动)
            {
                如果(方向!= Direction.Vertical)
                    container.Left = Math.Max​​(0,前+ container.Left  -  DragStart.X);
                如果(方向!= Direction.Horizo​​ntal)
                    container.Top = Math.Max​​(0,EY + container.Top  -  DragStart.Y);
            }
        };
    }
}


    公共Form1中()
    {
        的InitializeComponent();

        ControlMover.Init(this.panel1);

        ControlMover.Init(this.panel1,ControlMover.Direction.Vertical);
    }
 

I have on a WinForms Form a UserControl, of which multiple instances can be created dynamically. If I select it, I can drag it. Now, I want, if I select multiple UserControls (with ctrl + button click), to be able to drag all of them at the same time.

Can I do that like I did for one UserControl?

What I have tried until now:

// For dragging I use this method and also I have
// overridden  MouseUp,MouseDown,MouseMove from .net
public void StartDrag()  
{
    dragging = true;
    Point p = PointToClient(Cursor.Position);
    dragStart = new Point(Math.Max(0, p.X), Math.Max(0, p.Y));
    buttondrag.Capture = true;
}

private void Usercontrol1_SelectedChanged(object sender, EventArgs e)
{
    if (((UserControl)sender).Selected)
    {
        if (SelectedUserControl.Count > 1)
        {
            foreach (UserControl c in panel1.Controls)
            {
                c.StartDrag();
            }
        }
    }
}

解决方案

// put your controls in the panel, and use this class.

  class ControlMover
{
    public enum Direction
    {
        Any,
        Horizontal,
        Vertical
    }

    public static void Init(Control control)
    {
        Init(control, Direction.Any);
    }

    public static void Init(Control control, Direction direction)
    {
        Init(control, control, direction);
    }

    public static void Init(Control control, Control container, Direction direction)
    {
        bool Dragging = false;
        Point DragStart = Point.Empty;
        control.MouseDown += delegate(object sender, MouseEventArgs e)
        {
            Dragging = true;
            DragStart = new Point(e.X, e.Y);
            control.Capture = true;
        };
        control.MouseUp += delegate(object sender, MouseEventArgs e)
        {
            Dragging = false;
            control.Capture = false;
        };
        control.MouseMove += delegate(object sender, MouseEventArgs e)
        {
            if (Dragging)
            {
                if (direction != Direction.Vertical)
                    container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                if (direction != Direction.Horizontal)
                    container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
            }
        };
    }
}


    public Form1()
    {
        InitializeComponent();

        ControlMover.Init(this.panel1);

        ControlMover.Init(this.panel1, ControlMover.Direction.Vertical);
    }

这篇关于拖动多个选定的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆