C# Custom display arrangement settings

James 21 Reputation points
2021-09-13T03:15:30.477+00:00

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: 131415-goal.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.

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 131451-current.png

current code:

   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;  
            }  
        }  
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2021-09-13T10:19:28.483+00:00

    @James , Based on my test, I make a sample code for you to check the collision for three panels.

    Code:

       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;  
                if (pan.Left <= panel1.Right && pan.Bottom > panel1.Top && panel1.Bottom > pan.Bottom && pan.Right > panel1.Right)  
                {  
                    pan.Left = panel1.Right;  
      
                }  
    
            }  
              
             
        }  
    

    Result:

    133101-4444.gif


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.