Solved

collision detection in c#

Posted on 2007-04-11
10
3,363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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