简体   繁体   English

单击相交点将两条线拖在一起

[英]drag two lines together by clicking on intersection point

I have a mainPicture box that contains Two picture boxes (_pic1 , _pic2) . 我有一个mainPicture框,其中包含两个图片框(_pic1,_pic2)。 I can drag _pic1 and drag _pic2 too. 我也可以拖动_pic1和_pic2。 But I want to when I do mouse down on intersection of two picture boxes, I could drag two lines together . 但是我想当我将鼠标放在两个图片框的交点上时,可以将两条线拖在一起 So I should get intersection position . 所以我应该得到交叉点的位置。 by below code I could not. 通过下面的代码我不能。

_pic2.MouseUp += new MouseEventHandler(_pic1_MouseUp);
 _pic2.MouseDown += new MouseEventHandler(_pic1_MouseDown);
 Point p = new Point();
 bool interSection;
        private void _pic1_MouseDown(object sender, MouseEventArgs e)
         {
        p = e.Location;

        dragging = true;
        dragPoint = new Point(e.X, e.Y);
        this.Cursor = Cursors.SizeAll;


        Rectangle p1 = this._pic1.ClientRectangle;
        p1.Offset(this._pic1.Location);

        Rectangle p2 = this._pic2.ClientRectangle;
        p2.Offset(this._pic2.Location);


            //bool z = p1.Contains(p)      it returns false
            //bool zz = p2.Contains(p)      it returns false too



        if (p1.Contains(p) && p2.Contains(p))
        {
            interSection = true;
        }
           }
        private void _pic_MouseMove(object sender, MouseEventArgs e)
        {

        if (interSection)
        {
         //drag two lines together
            _pic1.Location = new Point(_pic1.Location.X + e.X - dragPoint.X, _pic1.Location.Y + e.Y - dragPoint.Y);
            _pic2.Location = new Point(_pic2.Location.X + e.X - dragPoint.X, _pic2.Location.Y + e.Y - dragPoint.Y);
            return; 
        }

        if (dragging)
        {
            _pic_1.Location = new Point(_pic1.Location.X + e.X - dragPoint.X, _pic1.Location.Y + e.Y - dragPoint.Y);
        }
    }
           private void _pic1_MouseUp(object sender, MouseEventArgs e)
           {

        dragging = false;
        this.Cursor = Cursors.Default;
            }
          private void _pic2_MouseMove(object sender, MouseEventArgs e)
            {
        if (dragging)
        {

            _pic2.Location = new Point(_pic2.Location.X + e.X - dragPoint.X, _pic2.Location.Y + e.Y - dragPoint.Y);
        }
    }

image : 图片 :

     http://tinypic.com/view.php?pic=ix8bab&s=8

You can test two Rectangles for Intersection like this: 您可以像这样测试两个矩形的相交:

if (_pic1.Bounds.IntersectsWith(_pic2.Bounds) ) // do stuff

And you can get the intersection like this: 你可以得到这样的交集:

Rectangle R = _pic1.Bounds;
R.Intersect(_pic2.Bounds);

Now you can test it again: 现在,您可以再次对其进行测试:

if (R != Rectangle.Empty)

Or find the Center: 或找到中心:

Point center = new Point(R.X + R.Width / 2, R.Y + R.Height / 2);

Or test whether the Mouse was clicked on the intersection: 或测试是否在相交处单击了鼠标:

if (R.Contains(e.Location) // ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM