Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

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.PictureBox pictureBox1;
            private System.Windows.Forms.PictureBox pictureBox2;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container 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.PictureBox();
                  this.pictureBox2 = new System.Windows.Forms.PictureBox();
                  this.SuspendLayout();
                  //
                  // pictureBox1
                  //
                  this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                  this.pictureBox1.Location = new System.Drawing.Point(104, 80);
                  this.pictureBox1.Name = "pictureBox1";
                  this.pictureBox1.TabIndex = 0;
                  this.pictureBox1.TabStop = false;
                  //
                  // pictureBox2
                  //
                  this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                  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.MouseEventHandler(this.pictureBox2_MouseMove);
                  this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDown);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(504, 334);
                  this.Controls.Add(this.pictureBox2);
                  this.Controls.Add(this.pictureBox1);
                  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(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        pictureBox2.Location = new Point(pictureBox2.Left + e.X - startX, pictureBox2.Top + e.Y - startY);
                  }

            }

            private void pictureBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  if (e.Button == MouseButtons.Left)
                  {
                        startX = e.X;
                        startY = e.Y;
                  }

            }
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tom Knowlton

ASKER

Thanks!