Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

collision detection in c#

Posted on 2007-04-11
10
Medium Priority
?
3,372 Views
Last Modified: 2012-06-27
in panel1; an arrayList or some object to keep track of the image and it's position. if the user drop an
image on top of another image, the is a messegeBox that shows."there is a preexisting image in the
desire location, do you wich to delete the preexisting image; Yes or No". if yes, the old image is deleted
and the new image is place at that location. if No, the old image remains. you can't put a pictureBox on
the top of another in the drop area.

this the program;  you insert pictureBoxs in panel2 and load them with image. you can drag and drop the
image in panel1

bool pbDown = false;
        PictureBox selectedPB;
        Rectangle prevClip;
        PictureBox pb;
        int startX, startY;
        ContextMenu cm = new ContextMenu();

private void DesignForm_Load(object sender, EventArgs e)
        {
            this.Reset();
            prevClip = Cursor.Clip;
            foreach (Control ctl in panel2.Controls)
            {
                if (ctl is PictureBox)
                {
                    ctl.MouseDown += new MouseEventHandler(ctl_MouseDown);
                    ctl.MouseMove += new MouseEventHandler(ctl_MouseMove);
                    ctl.MouseUp += new MouseEventHandler(ctl_MouseUp);
                }
            }
            MenuItem delete = new MenuItem("Delete");
            delete.Click += new EventHandler(delete_Click);
            cm.MenuItems.Add(delete);
            panel1.Click += new EventHandler(panel1_Click);
            panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);

        }

        private void panel1_Click(object sender, EventArgs e)
        {
            if (selectedPB != null)
            {
                selectedPB.BorderStyle = BorderStyle.None;
                selectedPB = null;
            }

        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (selectedPB != null && pbDown)
            {
                System.Diagnostics.Debug.Print("Too Fast!...panel1_MouseMove(" + e.X.ToString() + ", " + e.Y.ToString() + ")");
                Point pt = panel1.PointToScreen(new Point(e.X, e.Y));
                pt = selectedPB.PointToClient(pt);
                MouseEventArgs mea = new MouseEventArgs(MouseButtons.Left, 0, pt.X, pt.Y, 0);
                pb_MouseMove(selectedPB, mea);
            }
        }
       

        void delete_Click(object sender, EventArgs e)
        {
            MenuItem mi = (MenuItem)sender;
            ContextMenu cm = (ContextMenu)mi.GetContextMenu();
            PictureBox pb = (PictureBox)cm.SourceControl;
            pb.Dispose();
        }

        void ctl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Cursor.Clip = this.RectangleToScreen(this.ClientRectangle);
                startX = e.X;
                startY = e.Y;
                PictureBox source = (PictureBox)sender;
                pb = new PictureBox();
                pb.Size = source.Size;
                pb.Location = this.PointToClient(panel2.PointToScreen(source.Location));
                pb.Image = source.Image;
                this.Controls.Add(pb);
                pb.BringToFront();
            }
        }

        void ctl_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (pb != null)
                {
                    pb.Location = new Point(pb.Location.X + (e.X - startX), pb.Location.Y + (e.Y - startY));
                    startX = e.X;
                    startY = e.Y;
                }
            }
        }

        void ctl_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (pb != null)
                {
                    Cursor.Clip = prevClip;
                    if (pb.ClientRectangle.IntersectsWith(panel1.ClientRectangle))
                    {
                        pb.Hide();
                        pb.Location = panel1.PointToClient(this.PointToScreen(pb.Location));
                        panel1.Controls.Add(pb);
                        pb.ContextMenu = cm;
                        pb.Show();

                        pb.MouseDown += new MouseEventHandler(pb_MouseDown);
                        pb.MouseMove += new MouseEventHandler(pb_MouseMove);
                        pb.MouseUp += new MouseEventHandler(pb_MouseUp);
                    }
                    else
                    {
                        pb.Dispose();
                    }
                    pb = null;
                }
            }
        }

        void pb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pbDown = true;
                if (selectedPB != null)
                {
                    selectedPB.BorderStyle = BorderStyle.None;
                    selectedPB = null;
                }
                selectedPB = (PictureBox)sender;
                selectedPB.BorderStyle = BorderStyle.FixedSingle;
                selectedPB.BringToFront();
                Rectangle pnl = this.RectangleToScreen(new Rectangle(panel1.Location, panel1.Size));
                pnl.X = pnl.X + 1;
                pnl.Width = pnl.Width - 2;
                pnl.Y = pnl.Y + 1;
                pnl.Height = pnl.Height - 2;
                Cursor.Clip = pnl;
                startX = e.X;
                startY = e.Y;
            }
        }

        void pb_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBox localPB = (PictureBox)sender;
                if (localPB == selectedPB)
                {
                    localPB.Location = new Point(localPB.Location.X + (e.X - startX), localPB.Location.Y + (e.Y - startY));
                }
            }
        }

        void pb_MouseUp(object sender, MouseEventArgs e)
        {
            pbDown = false;
            Cursor.Clip = prevClip;
        }

       
       
    }
}
0
Comment
Question by:sayeth
  • 5
  • 4
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18891187
Where is the question?  What are you having a problem with--the MouseUp event code?

Bob
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18891189
Very nice presentation of a scenario, but you forgot to form a QUESTION.
0
 
LVL 1

Author Comment

by:sayeth
ID: 18891583
this program only drag and drop, i want it to also do collision detection.

I want the program to  also do this ( in panel1; an arrayList or some object to keep track of the image and it's position. if the user drop an image on top of another image, the is a messegeBox that shows."there is a preexisting image in the desire location, do you wich to delete the preexisting image; Yes or No". if yes, the old image is deleted and the new image is place at that location. if No, the old image remains. you can't put a pictureBox on the top of another in the drop area)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 18891689
You have the tools, you just need to put them together:

pb.ClientRectangle.IntersectsWith(panel1.ClientRectangle)

Loop through the controls for the panel, and determine if any of the controls are PictureBox, and the if the PictureBox control intersects with the rectangle referenced by 'pb'.

Bob
0
 
LVL 1

Author Comment

by:sayeth
ID: 18891895
can you please show me in the code, because i been messing with this thing all night and i can't get it to work right
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18893525
Untested attempt:

private bool PictureBoxHitTest(PictureBox pb1)
{
  foreach (Control ctl in panel2.Controls)
  {
     if (typeof(ctl) == typeof(PictureBox))
     {
        pb2 = (PictureBox)ctl;
        if (pb.ClientRectangle.IntersectsWith(pb2.ClientRectangle))
          return true;
     }
  }
  return false;
}

Bob
0
 
LVL 1

Author Comment

by:sayeth
ID: 18894268
there is one error on the commented line;
Error: The type or namespace name 'ctl' could not be found (are you missing a using directive or an assembly reference?)      
private bool PictureBoxHitTest(PictureBox pb1)
{
  foreach (Control ctl in panel2.Controls)
  {
    // if (typeof(ctl) == typeof(PictureBox))
     {
        pb2 = (PictureBox)ctl;
        if (pb.ClientRectangle.IntersectsWith(pb2.ClientRectangle))
          return true;
     }
  }
  return false;
}
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 18894401
Try this:

private bool PictureBoxHitTest(PictureBox pb1)
{
  foreach (Control ctl in panel2.Controls)
  {
    if (ctl.GetType() == typeof(PictureBox))
     {
        pb2 = (PictureBox)ctl;
        if (pb.ClientRectangle.IntersectsWith(pb2.ClientRectangle))
          return true;
     }
  }
  return false;
}

Bob
0
 
LVL 1

Author Comment

by:sayeth
ID: 18906400
i am sorry, i had a family problem i needed to take care of. but i am back. your code look perfect, sweet and it is right ; however, when i call it in the program above it don't do nothing.
0
 
LVL 1

Author Comment

by:sayeth
ID: 18906715
ok, it do something, it don't allow any drop.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question