能够拖动动态创建的面板

时间:2017-07-31 18:49:58

标签: c# events panel

我一直在看有关添加事件的帖子,以便能够在面板上拖动。但是我如何通过动态创建的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 };
    }

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

总结一下,我有一个面板,它持有多个动态创建的面板。我希望能够在主面板内移动动态创建的面板。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这是一个允许你移动任何控件的类。

只需注册:

MoveController.RegisterCtl( button1 );

现在你可以移动控件..

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

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;
    }
}

<强>更新

这是一个更容易理解的版本

  • 设置Tag值以限制移动到垂直或水平
  • ActionsMoving事件添加Moved ..
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;
    }

}