Tom Knowlton
asked on
Detecting overlap
I have code now, thanks to IdleMind that allows me to drag a Picture Box around on a form.
I have another PictureBox on the form.
When I stop dragging the PictureBox...I want to be able to detect if the border of the one PictureBox is overlapping the border of other PictureBox.
My current code should work with a new C# Windows App project, for that is what I am using to test these concepts with.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Pictu reBox pictureBox1;
private System.Windows.Forms.Pictu reBox pictureBox2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont ainer components = null;
private int startX, startY;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.Pictu reBox();
this.pictureBox2 = new System.Windows.Forms.Pictu reBox();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BorderSty le = System.Windows.Forms.Borde rStyle.Fix edSingle;
this.pictureBox1.Location = new System.Drawing.Point(104, 80);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.BorderSty le = System.Windows.Forms.Borde rStyle.Fix edSingle;
this.pictureBox2.Location = new System.Drawing.Point(328, 216);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
this.pictureBox2.MouseMove += new System.Windows.Forms.Mouse EventHandl er(this.pi ctureBox2_ MouseMove) ;
this.pictureBox2.MouseDown += new System.Windows.Forms.Mouse EventHandl er(this.pi ctureBox2_ MouseDown) ;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 334);
this.Controls.Add(this.pic tureBox2);
this.Controls.Add(this.pic tureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void pictureBox2_MouseMove(obje ct sender, System.Windows.Forms.Mouse EventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox2.Location = new Point(pictureBox2.Left + e.X - startX, pictureBox2.Top + e.Y - startY);
}
}
private void pictureBox2_MouseDown(obje ct sender, System.Windows.Forms.Mouse EventArgs e)
{
if (e.Button == MouseButtons.Left)
{
startX = e.X;
startY = e.Y;
}
}
}
}
I have another PictureBox on the form.
When I stop dragging the PictureBox...I want to be able to detect if the border of the one PictureBox is overlapping the border of other PictureBox.
My current code should work with a new C# Windows App project, for that is what I am using to test these concepts with.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Pictu
private System.Windows.Forms.Pictu
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
private int startX, startY;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.Pictu
this.pictureBox2 = new System.Windows.Forms.Pictu
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BorderSty
this.pictureBox1.Location = new System.Drawing.Point(104, 80);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.BorderSty
this.pictureBox2.Location = new System.Drawing.Point(328, 216);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
this.pictureBox2.MouseMove
this.pictureBox2.MouseDown
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 334);
this.Controls.Add(this.pic
this.Controls.Add(this.pic
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void pictureBox2_MouseMove(obje
{
if (e.Button == MouseButtons.Left)
{
pictureBox2.Location = new Point(pictureBox2.Left + e.X - startX, pictureBox2.Top + e.Y - startY);
}
}
private void pictureBox2_MouseDown(obje
{
if (e.Button == MouseButtons.Left)
{
startX = e.X;
startY = e.Y;
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER