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

3 篇文章 0 订阅

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

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

 

代码:

判断的方法

public void ClickNa(Point yPoint, Size ySize, Point e)
        {
            //左
            Region san = new System.Drawing.Region();
            GraphicsPath gp = new GraphicsPath();
            List<PointF> drawpathtemp = new List<PointF>();
            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<PointF> drawpathtemp = new List<PointF>();
            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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值