Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

c# - Being able to drag around Dynamically created Panels

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.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Simply register it:

MoveController.RegisterCtl( button1 );

Now you can move the control..

When done you can also unregister a control:

MoveController.UnRegisterCtl( button1 );

Here is the controller class:

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

Update

Here is a more involved version that allows

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...