Solved

Detecting overlap

Posted on 2006-11-17
2
438 Views
Last Modified: 2011-10-03
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;
                  }

            }
      }
}
0
Comment
Question by:Tom Knowlton
[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
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17967254
Try...

        int startX, startY;

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

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pictureBox1.Location = new Point(pictureBox1.Left + e.X - startX, pictureBox1.Top + e.Y - startY);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Rectangle pb1 = new Rectangle(pictureBox1.Location, pictureBox1.Size);
                Rectangle pb2 = new Rectangle(pictureBox2.Location, pictureBox2.Size);
                if (pb1.IntersectsWith(pb2))
                {
                    MessageBox.Show("Overlapping");
                }
                else
                {
                    MessageBox.Show("NOT Overlapping");
                }
            }
        }
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 17967400
Thanks!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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