Solved

collision detection in c#

Posted on 2007-04-11
10
3,343 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Chat Room 1 43
DateTimepicker 4 33
scanning dentists xray (the small ones) 3 43
Connection String 16 43
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now