【问题标题】:Draw a tracer line between mouse pointer and a point of a line在鼠标指针和线的一点之间绘制一条跟踪线
【发布时间】:2018-03-04 11:43:55
【问题描述】:

我有一个 CAD 应用程序。我有课。

public class Line
{
    public float width { get; set; }
    public bool selected { get; set; }
    public Color color { get; set; }
    public Point Start { get; set; }
    public Point End { get; set; }

    public Line(Color col, float w, Point s, Point e)
    {

        color = col;
        Start = s;
        End = e;
        width = w;

    }
    public void Draw(Graphics G)
    {
        using (Pen pen = new Pen(color, width))
        {
            G.DrawLine(pen, Start, End);
        }
    }

我有一个名为 Lines 的列表。 线条绘制在面板上

private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics grp = e.Graphics;
foreach (Line L in Lines.Except(DelLines))
            {
                L.Draw(grp);
            }
 if (mouseisdown && Line == true)
            {

                grp.DrawLine(Pens.White, DsP1, new Point(MousePosition.X, MousePosition.Y - 158));
                //     double d = Math.Abs((DsP1.X - MousePosition.X) + (DsP1.Y - (MousePosition.Y - 158)));
                if (snapON == true)
                {
                    grp.DrawLine(Pens.Yellow, snapPoint, mousePoint);

                }
           }

这里 mousePoint 是 Point(mouseposition.X,mouseposition.Y-158)。 158 是因为我的面板位于窗口顶部下方 158 Y 处。 然后我有一个 mouseUp 事件

   private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        mouseisdown = false;
        if (Line == true)
        {

            PointsList.Add(new Point(e.X, e.Y));
            Lines.Add(new PolygonArea.Line(Color.White, 1, DsP1, new Point(MousePosition.X, MousePosition.Y - 158)));
            Line = false;

        } }

现在我想在鼠标移动时画一条线,它会告诉鼠标位置它的 x 轴或 y 轴与行列表中的一条线的点匹配。我尝试的是:

在 Panel1_MouseMove 事件中

 if(Line==true && Lines.Count>1)
        {
                                if (Lines[0].Start.X == e.Location.X || 
Lines[0].Start.Y==e.Location.Y)
                {
                snapON = true;
                    snapPoint = Lines[0].Start;
                panel1.Invalidate();
                }
                else if(Lines[0].End.X==e.Location.X || Lines[0].End.Y == e.Location.Y)
                {
                snapON = true;
                    snapPoint = Lines[0].End;
                panel1.Invalidate();
                }
            }

只是为了测试,我指的是行中的第一行,即 Lines[0]。但是当条件满足时,我无法在鼠标指针和线的点之间画一条线。 有什么想法吗??

【问题讨论】:

    标签: c# graphics line point


    【解决方案1】:

    我解决了这个问题。只需在 Paint Event 中绘制线条后添加 SnapON=false 即可。而且我必须从 mouse_move 事件中删除 panel1.invalidate ...... 谢谢大家

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 2019-08-15
      • 2014-03-20
      相关资源
      最近更新 更多