能够拖动动态创建的面板 [英] Being able to drag around Dynamically created Panels

查看:65
本文介绍了能够拖动动态创建的面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直看到有关添加事件的帖子,以便能够在面板上拖动.但是如何通过动态创建的面板来实现这一目标?

I keep seeing posts about adding Events to be able to drag around the panel. But how would i achieve this through a dynamically created Panel?

Panel pn = wch.GenerateWorkspaceControl(space.Name, space.Name, p);
            PanelTest.Controls.Add(pn);

        public Panel GenerateWorkspaceControl(string gbTitle, string gbName, Point gbPos)
    {
        Panel pnl = GeneratePanelContainer(gbPos, new Size(300, 200));
        pnl.Controls.Add(GenerateLabel(gbName,new Point(100,1),new Size(135,115)));
        return pnl;
    }

private Panel GeneratePanelContainer(Point loc, Size size)
    {
        return new Panel() { BackColor = Color.Transparent, BorderStyle = BorderStyle.FixedSingle, Location = loc, Size = size };
    }

我是否在生成面板容器中添加事件处理程序?

Do i add an event handler in the generate panel container?

总结一下,我有一个面板,其中包含多个动态创建的面板.我希望能够在主面板内移动创建的面板.

to summarize i have a panel that is holding multiple panels that are dynamically created. I want to be able to move around the dynmaically created panels INSIDE the main panel.

有什么想法吗?

推荐答案

这是一个使您可以移动任何控件的类.

Here is a class that lets you make any control movable.

只需注册:

MoveController.RegisterCtl( button1 );

现在您可以移动控件了.

Now you can move the control..

完成后,您还可以取消注册控件:

When done you can also unregister a control:

MoveController.UnRegisterCtl( button1 );

这是控制器类:

static class MoveController
{
    static List<Control> Controls = new List<Control>();
    static Control curCtl = null;
    static Point curStart = Point.Empty;

    static public void RegisterCtl(Control ctl)
    {
        Controls.Add(ctl);
        ctl.MouseDown += ctl_MouseDown;
        ctl.MouseMove += ctl_MouseMove;
        ctl.MouseUp += ctl_MouseUp;
    }

    static public void UnRegisterCtl(Control ctl)
    {
        if (Controls != null && Controls.Contains(ctl) )
        {
            Controls.Remove(ctl);
            ctl.MouseDown -= ctl_MouseDown;
            ctl.MouseMove -= ctl_MouseMove;
            ctl.MouseUp -= ctl_MouseUp;
        }
    }

    static void ctl_MouseDown(object sender, MouseEventArgs e)
    {
        curCtl = (Control)sender;
        curStart = curCtl.Location;
    }

    static void ctl_MouseMove(object sender, MouseEventArgs e)
    {
        if (curCtl != null)
        {
            curCtl.Left +=  e.Location.X - curCtl.Width / 2;
            curCtl.Top  +=  e.Location.Y - curCtl.Height / 2;
        }
    }

    static void ctl_MouseUp(object sender, MouseEventArgs e)
    {
        curCtl = null;
    }
}

更新

这是一个涉及更多的版本,它允许

Here is a more involved version that allows

  • 设置 Tag 值以将移动限制为垂直或水平
  • Moving Moved 事件添加 Actions .
  • to set a Tag value to restrict movement to vertical or horizontal
  • adding Actions for Moving and Moved events..
class MoveController
{
    static List<Control> Controls = new List<Control>();
    static Control curCtl = null;
    static Point curStart = Point.Empty;

    static Dictionary<Control, Tuple<Action, Action>> 
        actions = new Dictionary<Control, Tuple<Action, Action>>();

    static public void RegisterCtl(Control ctl)
    {
        RegisterCtl(ctl, null, null);
    }

    static public void RegisterCtl(Control ctl, Action moveAction, Action movedAction)
    {
        Controls.Add(ctl);
        ctl.MouseEnter += Ctl_MouseEnter;
        ctl.MouseLeave += Ctl_MouseLeave;
        ctl.MouseDown += ctl_MouseDown;
        ctl.MouseMove += ctl_MouseMove;
        ctl.MouseUp += ctl_MouseUp;
        if (moveAction != null)
            if (actions.Keys.Contains(ctl)) actions[ctl] = new Tuple<Action, Action>(moveAction, movedAction);
            else actions.Add(ctl, new Tuple<Action, Action>(moveAction, movedAction));
    }

    private static void Ctl_MouseEnter(object sender, EventArgs e)
    {
        ((Control)sender).Cursor = Cursors.Hand;
    }

    private static void Ctl_MouseLeave(object sender, EventArgs e)
    {
        ((Control)sender).Cursor = Cursors.Default;
    }

    public static void UnRegisterCtl(Control ctl)
    {
        if (Controls != null && Controls.Contains(ctl) )
        {
            Controls.Remove(ctl);
            ctl.MouseDown -= ctl_MouseDown;
            ctl.MouseMove -= ctl_MouseMove;
            ctl.MouseUp -= ctl_MouseUp;
        }
        if (actions.ContainsKey(ctl)) actions.Remove(ctl);
    }



    static public void RegisterMovingAction(Control ctl, Action action)
    {

    }

    static void ctl_MouseDown(object sender, MouseEventArgs e)
    {
        curCtl = (Control)sender;
        curStart = curCtl.Location;
    }

    static void ctl_MouseMove(object sender, MouseEventArgs e)
    {
        int t = 0;
        if (curCtl != null)
        {
            if (curCtl.Tag != null) t = Convert.ToInt32(curCtl.Tag);
            if ((t&1) != 1) curCtl.Left +=  e.Location.X - curCtl.Width / 2;
            if ((t&2) != 2) curCtl.Top  +=  e.Location.Y - curCtl.Height / 2;

            if (actions.ContainsKey(curCtl) && actions[curCtl] != null && actions[curCtl].Item1 != null)
                actions[curCtl].Item1();
        }
    }

    static void ctl_MouseUp(object sender, MouseEventArgs e)
    {
        if (curCtl == null) return;  ///
        if (actions.ContainsKey(curCtl) && actions[curCtl] != null && actions[curCtl].Item2 != null)
            actions[curCtl].Item2();
        curCtl = null;
    }

}

这篇关于能够拖动动态创建的面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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