在鼠标移动时动态绘制线GDI C#

时间:2017-12-02 13:36:29

标签: c# graphics line gdi

我有一个图形程序,它是一个简单的基本形状(线条,圆形,矩形)绘图。我有一个线类。该线条绘制在面板上绘制的位图上。我有两个行列表(行,行添加到它,DelLines,此列表中的行不在Paint事件中绘制),每个绘制的行都添加到其中。然后在paint事件中,失效会导致绘制的线条列表在面板上绘制。没有使用鼠标移动点击时没有问题。我得到的是enter image description here

doSomethingLong()
   .then(function() {
      console.log(pased)
   },

   function(err) {
     console.log(err)
   }

)

我也尝试过Lines.RemoveAt(Lines.Count-1)给出相同的结果。

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))
        {
            Matrix m = new Matrix();

            G.DrawLine(pen, Start, End);

        }
    }
    public bool SelectLine(Point Pt)
    {
        if ((Pt.X < Start.X && Pt.X < End.X) || (Pt.X > Start.X && Pt.X > End.X) ||
 (Pt.Y < Start.Y && Pt.Y < End.Y) || (Pt.Y > Start.Y && Pt.Y > End.Y))

            return false;
        // now we calculate the distance:
        float dy = End.Y - Start.Y;
        float dx = End.X - Start.X;
        float Z = dy * Pt.X - dx * Pt.Y + Start.Y * End.X - Start.X * End.Y;
        float N = dy * dy + dx * dx;
        float dist = (float)(Math.Abs(Z) / Math.Sqrt(N));
        // done:
        return dist < width / 2f;

    }
private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        Graphics g = Graphics.FromImage(Bitmap);
        if (snap == true)
        {
            SnapToGrid(new System.Drawing.Point(e.X, e.Y));
        }

        //Cursor Location
        Matrix m = new Matrix();
        m.Translate(panel1.Width / 2, panel1.Height / 2);
        m.Invert();
        System.Drawing.Point[] pa = new System.Drawing.Point[] { new System.Drawing.Point(e.X, e.Y) };
        m.TransformPoints(pa);
        lbl_x.Text = pa[0].ToString().Substring(1, pa[0].ToString().Length - 2);
        //--------------------------------------------------------------------------------------------
        if (leftdown)
        {
            Point p = PointsList[0];

            //Line
            Line line = new Line(Color.White, 2, p, e.Location);
            line.Draw(g);

            Lines.Add(line);

            if (Lines.Count > 0)
            {
                                    DelLines.Add(line);

我想在鼠标移动时绘制线条,以便显示从开始点击和光标当前位置绘制的线条。不是以前的行。 任何想法将不胜感激。 感谢

0 个答案:

没有答案