Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
So I am trying to make a little utility for managing my screens, I want to add a screen rearrangement system like in windows, see here: https://i.imgur.com/eUzlWdr.png, I already have code worked out for getting and setting the exact screen coordinates I need, however I need to work out some sort of rearrangement dragging system that "snaps" to position like the windows implementation.

What I have tried:

so far I have made some basic strides towards this, before coming to the realization the snapping will be a bit challenging, so here is the code I have so far, and here is an attached screenshot of the program as is. It consists of a panel with 3 panels inside it. For now I would like tips on when the mouse is released and the collision checks are run to snap similarly to windows display rearangment, Just a test bed for now https://i.imgur.com/EtixiWh.png

C#
 private Point MouseDownLocation;
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
     var pan = (Panel)sender;
     if (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
          pan.Left = e.X + pan.Left - MouseDownLocation.X;
          pan.Top = e.Y + pan.Top - MouseDownLocation.Y;
     }
}

        private void panel2_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                var pan = (Panel)sender;
                List<Panel> allpanels = new List<Panel>();
                foreach (Panel pancomp in panel1.Controls)
                {
                    if (pancomp == pan)
                    {
                        continue;
                    }      
                    allpanels.Add(pancomp);
                }


                //var pan = (Panel)sender;
                List<Panel> collisions = new List<Panel>();

                foreach (Panel pancomp in allpanels)
                {
                    if (pancomp == pan)
                    {
                        continue;
                    }

                    if (pan.Bounds.IntersectsWith(pancomp.Bounds))
                    {
                        //PUSH AWAY
                        collisions.Add(pancomp);
                        //listBox1.Items.Add("collided!");//panel2.Location.ToString());
                    }
                }

                collisions = collisions;

                if (collisions.Count > 0)
                {
                    //WE HAVE COLLISIONS LETS SNAP TO VALID PLACE
                    //MouseDownLocation



                }
                else
                {
                    //WE HAVE NO COLLISIONS LETS SNAP TO PLACE
                }


            }
        }

        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                MouseDownLocation = e.Location;
            }
        }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900