sayeth
asked on
collision detection in c#
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_Mous eDown);
ctl.MouseMove += new MouseEventHandler(ctl_Mous eMove);
ctl.MouseUp += new MouseEventHandler(ctl_Mous eUp);
}
}
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_M ouseMove);
}
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.P rint("Too Fast!...panel1_MouseMove(" + e.X.ToString() + ", " + e.Y.ToString() + ")");
Point pt = panel1.PointToScreen(new Point(e.X, e.Y));
pt = selectedPB.PointToClient(p t);
MouseEventArgs mea = new MouseEventArgs(MouseButton s.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.GetContext Menu();
PictureBox pb = (PictureBox)cm.SourceContr ol;
pb.Dispose();
}
void ctl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor.Clip = this.RectangleToScreen(thi s.ClientRe ctangle);
startX = e.X;
startY = e.Y;
PictureBox source = (PictureBox)sender;
pb = new PictureBox();
pb.Size = source.Size;
pb.Location = this.PointToClient(panel2. PointToScr een(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.Inters ectsWith(p anel1.Clie ntRectangl e))
{
pb.Hide();
pb.Location = panel1.PointToClient(this. PointToScr een(pb.Loc ation));
panel1.Controls.Add(pb);
pb.ContextMenu = cm;
pb.Show();
pb.MouseDown += new MouseEventHandler(pb_Mouse Down);
pb.MouseMove += new MouseEventHandler(pb_Mouse Move);
pb.MouseUp += new MouseEventHandler(pb_Mouse Up);
}
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;
}
}
}
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_Mous
ctl.MouseMove += new MouseEventHandler(ctl_Mous
ctl.MouseUp += new MouseEventHandler(ctl_Mous
}
}
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_M
}
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.P
Point pt = panel1.PointToScreen(new Point(e.X, e.Y));
pt = selectedPB.PointToClient(p
MouseEventArgs mea = new MouseEventArgs(MouseButton
pb_MouseMove(selectedPB, mea);
}
}
void delete_Click(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
ContextMenu cm = (ContextMenu)mi.GetContext
PictureBox pb = (PictureBox)cm.SourceContr
pb.Dispose();
}
void ctl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor.Clip = this.RectangleToScreen(thi
startX = e.X;
startY = e.Y;
PictureBox source = (PictureBox)sender;
pb = new PictureBox();
pb.Size = source.Size;
pb.Location = this.PointToClient(panel2.
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.Inters
{
pb.Hide();
pb.Location = panel1.PointToClient(this.
panel1.Controls.Add(pb);
pb.ContextMenu = cm;
pb.Show();
pb.MouseDown += new MouseEventHandler(pb_Mouse
pb.MouseMove += new MouseEventHandler(pb_Mouse
pb.MouseUp += new MouseEventHandler(pb_Mouse
}
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
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;
}
}
}
Very nice presentation of a scenario, but you forgot to form a QUESTION.
ASKER
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)
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)
You have the tools, you just need to put them together:
pb.ClientRectangle.Interse ctsWith(pa nel1.Clien tRectangle )
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
pb.ClientRectangle.Interse
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
ASKER
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
Untested attempt:
private bool PictureBoxHitTest(PictureB ox pb1)
{
foreach (Control ctl in panel2.Controls)
{
if (typeof(ctl) == typeof(PictureBox))
{
pb2 = (PictureBox)ctl;
if (pb.ClientRectangle.Inters ectsWith(p b2.ClientR ectangle))
return true;
}
}
return false;
}
Bob
private bool PictureBoxHitTest(PictureB
{
foreach (Control ctl in panel2.Controls)
{
if (typeof(ctl) == typeof(PictureBox))
{
pb2 = (PictureBox)ctl;
if (pb.ClientRectangle.Inters
return true;
}
}
return false;
}
Bob
ASKER
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(PictureB ox pb1)
{
foreach (Control ctl in panel2.Controls)
{
// if (typeof(ctl) == typeof(PictureBox))
{
pb2 = (PictureBox)ctl;
if (pb.ClientRectangle.Inters ectsWith(p b2.ClientR ectangle))
return true;
}
}
return false;
}
Error: The type or namespace name 'ctl' could not be found (are you missing a using directive or an assembly reference?)
private bool PictureBoxHitTest(PictureB
{
foreach (Control ctl in panel2.Controls)
{
// if (typeof(ctl) == typeof(PictureBox))
{
pb2 = (PictureBox)ctl;
if (pb.ClientRectangle.Inters
return true;
}
}
return false;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
ok, it do something, it don't allow any drop.
Bob