如何检测单击 Windows 窗体中的一行 [英] How do I detect click on a line in Windows Forms

查看:29
本文介绍了如何检测单击 Windows 窗体中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 winforms 应用程序

I have a winforms application

这是我的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        Graphics gr;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gr = this.CreateGraphics();

            MyLine myline = new MyLine();
            myline.P1 = new Point(100, 0);
            myline.P2 = new Point(200, 80);

            gr.DrawLine(new Pen(Color.Red), myline.P1,myline.P2);


            Rectangle r = new Rectangle(0, 0, 50, 50);


            gr.DrawRectangle(new Pen(Color.Teal, 5), r);

            if (r.Contains(0,25)) MessageBox.Show("within");

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            gr.Clear(this.BackColor);
        }


    }
}

class MyLine
{    
    public Point P1 {get; set;}
    public Point P2 { get; set; }
}

我的问题是这个..

我可以画一个矩形,我可以看到里面是否有一个点.

I can draw a rectangle, and I can see whether a point is within it.

所以我可以将程序扩展为当单击窗体位于矩形内时说是".矩形有一个非常棒的包含功能.

So I could extend the program to say "yes" when a click on the form is within the rectangle. The Rectangle has a Contains function which is great.

但我想为 Line 做同样的事情.

But I want to do the same for Line.

问题在于,winforms 没有 Line 类.我可以编写自己的 Line 类,但问题仍然存在.. 如何确定是否有点击?

The problem, is that winforms has no Line class. I could write my own Line class, but the problem remains.. how to find whether a click landed on it?

我注意到 WPF 有这样一个类 How我能识别鼠标点击一行吗?

I notice that WPF has such a class How do I recognize a mouse click on a line?

但我使用的是 winforms.

But i'm using winforms.

推荐答案

使用 GraphicsPath.IsOutlineVisible 方法可以判断指定点绘制时是否在路径的轮廓之下.您可以设置笔的宽度.

Using GraphicsPath.IsOutlineVisible method you can determine whether the specified point is under the outline of the path when drawn with the specified Pen. You can set width of the pen.

所以你可以创建一个 GraphicsPath,然后使用 GraphicsPath.AddLine 到路径并检查路径是否包含点.

So you can create a GraphicsPath and then add a line using GraphicsPath.AddLine to the path and check if the path contains the point.

示例:

下面的方法使用指定的宽度检查 p 是否在具有端点 p1p2 的线上.

The below method, checks if the p is on the line with end points p1 and p2 using the specified width.

您可以使用更宽的宽度来增加容差,或者如果线条宽度大于 1:

You can use wider width to increase the tolerance or if the line is wider than 1:

//using System.Drawing;
//using System.Drawing.Drawing2D;
bool IsOnLine(Point p1, Point p2, Point p, int width = 1)
{
    using (var path = new GraphicsPath())
    {
        using (var pen = new Pen(Brushes.Black, width))
        {
            path.AddLine(p1, p2);
            return path.IsOutlineVisible(p, pen);
        }
    }
}

这篇关于如何检测单击 Windows 窗体中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆