Solved

collision detection in c#

Posted on 2007-04-11
10
3,337 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
Comment Utility
Where is the question?  What are you having a problem with--the MouseUp event code?

Bob
0
 
LVL 23

Expert Comment

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

Author Comment

by:sayeth
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ok, it do something, it don't allow any drop.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Service with UDP 2 26
Default parameter problem in C# 3 25
Trouble with References... 5 21
Calling stored proc in EDMX 11 17
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

11 Experts available now in Live!

Get 1:1 Help Now