question

James-1709 avatar image
0 Votes"
James-1709 asked JackJJun-MSFT commented

C# Custom display arrangement settings

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



dotnet-csharp
goal.png (14.7 KiB)
current.png (9.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT commented

@James-1709, 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.




4444.gif (69.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Jack,
Thanks for the reply :), however I already have accomplished moving and detecting if the bounds are intersecting, what I need now is for the panels to not be able to overlap, and slides along the edge of the colliding panel.

0 Votes 0 ·

@James-1709, I have updated my answer, you could have a look.

0 Votes 0 ·