C#的winform点长方形的上下左右三角形,画三角形,画四边形,是否在内部。

C#的winform点长方形的上下左右三角形,画三角形,画四边形,是否在内部。_第1张图片

问题:画出一个长方形,判断鼠标点击长方形的上下左右哪个位置。

思路:先将长方形分成4个三角形,然后判断鼠标点击的点在哪个三角形上面。

 

代码:

判断的方法

public void ClickNa(Point yPoint, Size ySize, Point e)
        {
            //左
            Region san = new System.Drawing.Region();
            GraphicsPath gp = new GraphicsPath();
            List drawpathtemp = new List();
            drawpathtemp.Add(new PointF(yPoint.X, yPoint.Y));
            drawpathtemp.Add(new PointF(yPoint.X, yPoint.Y + ySize.Height));
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width / 2, yPoint.Y + ySize.Height / 2));
            gp.AddPolygon(drawpathtemp.ToArray());
            san.MakeEmpty();
            san.Union(gp);
            if (san.IsVisible(e))
            {
                MessageBox.Show("左");
            }

            gp.Reset();
            drawpathtemp.Clear();

            //右
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width, yPoint.Y));
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width, yPoint.Y + ySize.Height));
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width / 2, yPoint.Y + ySize.Height / 2));
            gp.AddPolygon(drawpathtemp.ToArray());
            san.MakeEmpty();
            san.Union(gp);
            if (san.IsVisible(e))
            {
                MessageBox.Show("右");
            }
            
            gp.Reset();
            drawpathtemp.Clear();

            //上
            drawpathtemp.Add(new PointF(yPoint.X, yPoint.Y));
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width, yPoint.Y));
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width / 2, yPoint.Y + ySize.Height / 2));
            gp.AddPolygon(drawpathtemp.ToArray());
            san.MakeEmpty();
            san.Union(gp);
            if (san.IsVisible(e))
            {
                MessageBox.Show("上");
            }

            gp.Reset();
            drawpathtemp.Clear();

            //下
            drawpathtemp.Add(new PointF(yPoint.X, yPoint.Y+ySize.Height));
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width, yPoint.Y + ySize.Height));
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width / 2, yPoint.Y + ySize.Height / 2));
            gp.AddPolygon(drawpathtemp.ToArray());
            san.MakeEmpty();
            san.Union(gp);
            if (san.IsVisible(e))
            {
                MessageBox.Show("下");
            }
        }

 

界面代码

        Region r = new System.Drawing.Region();
        Point yPoint = new Point(120, 120);
        Size ySize = new Size(250, 150);
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Pen pen = new Pen(Color.Red);
            e.Graphics.DrawRectangle(pen, new Rectangle(yPoint, ySize));

            GraphicsPath gp = new GraphicsPath();
            List drawpathtemp = new List();
            drawpathtemp.Add(new PointF(yPoint.X, yPoint.Y));
            drawpathtemp.Add(new PointF(yPoint.X, yPoint.Y + ySize.Height));
            drawpathtemp.Add(new PointF(yPoint.X + ySize.Width, yPoint.Y + ySize.Height));
            gp.AddPolygon(drawpathtemp.ToArray());
            r.MakeEmpty();
            r.Union(gp);
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //if (r.IsVisible(e.Location))
                //{
                //    MessageBox.Show("在选中区域内");
                //}
                //else
                //{
                //    MessageBox.Show("不在选中区域内");
                //}
                ClickNa(yPoint, ySize, e.Location);
            }
        }

 

解决方案源代码

https://download.csdn.net/download/qq373591361/11099782

 

 

你可能感兴趣的:(C#,WinForm)