Visual Studio c# .net贝塞尔曲线,绘制速度更快

qrjkbowd  于 2022-11-25  发布在  C#
关注(0)|答案(1)|浏览(329)

我正在尝试用.net framework用C#做一个面板。我有一个面板,我在里面画了一些线和线。我想用它来控制de bezier曲线。我得出了这个结果:``
命名空间Bezier {公共分部类FormMain:表格{ Pen lapisLinhas =新笔(颜色.白色,3);笔lapisPontos =新笔(颜色.黑色,2); Pen lapisControllos =新笔(颜色.红色,3);随机rnd;

private int frame = 40;
    private int sele = 0, ct = 0;
    private Point p1, p2, p3, p4, p5, p6, p7, p8, p9, p10;

    public FormMain()
    {
        InitializeComponent();
        
    }
    private void PanelDraw_Paint(object sender, PaintEventArgs e)
    {
        desenhaBezier(e.Graphics);
    }
    /* protected virtual void OnLoad(EventArgs e)
    {
        gerarPontos();
    }*/

    private void FormMain_Load(object sender, EventArgs e)
    {
        gerarPontos();
    }
    private void desenhaBezier(Graphics g)
    {
        Point[] pontos = { p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 };
        g.Clear(PanelDraw.BackColor);

        int w = PanelDraw.DisplayRectangle.Width;
        int h = PanelDraw.DisplayRectangle.Height;

        rnd = new Random();

        g.DrawBeziers(lapisLinhas, pontos);

        int r = 3;
        for (int i = 0; i < pontos.Length; i++)
        {
            g.DrawEllipse(lapisPontos, pontos[i].X - r, pontos[i].Y - r, r * 2, r * 2);
        }

        g.DrawLine(lapisControlos, p1, p2);
        g.DrawLine(lapisControlos, p3, p4);
        g.DrawLine(lapisControlos, p5, p6);
        g.DrawLine(lapisControlos, p7, p8);
        g.DrawLine(lapisControlos, p9, p10);
    }

    private void gerarPontos()
    {
        int w = PanelDraw.DisplayRectangle.Width;
        int h = PanelDraw.DisplayRectangle.Height;

        StatusLabelScreen.Text = "Screen: (w: " + w + "|h: " + h + ")";

        rnd = new Random();

        p1 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p2 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p3 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p4 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p5 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p6 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p7 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p8 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p9 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p10 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));

        PanelDraw.Invalidate();

    }
    private void StatusLabelPosicao_Click(object sender, EventArgs e)
    {

    }

    private void novosPontosToolStripMenuItem_Click(object sender, EventArgs e)
    {
        rnd = new Random();

        int w = PanelDraw.DisplayRectangle.Width;
        int h = PanelDraw.DisplayRectangle.Height;

        StatusLabelScreen.Text = "Screen: (w: " + w + "| h: " + h + ")";

        p1 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p2 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p3 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p4 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p5 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p6 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p7 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p8 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p9 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));
        p10 = new Point(rnd.Next(frame, w - frame), rnd.Next(frame, h - frame));

        PanelDraw.Invalidate();
    }

    private void corDosPontosToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (colorDialog.ShowDialog() == DialogResult.OK)
        {
            Color pontosCor = colorDialog.Color;
            lapisPontos.Color = pontosCor;
            PanelDraw.Invalidate();
        }
    }

    private void pontosToolStripMenuItem_Click(object sender, EventArgs e)
    {
        pontosToolStripMenuItem.Checked = !pontosToolStripMenuItem.Checked;

        if (!pontosToolStripMenuItem.Checked)
        {
            Color trans = Color.Transparent;
            lapisPontos = new Pen(trans);
        }
        else
        {
            lapisPontos = new Pen(Color.Red);
        }
        PanelDraw.Invalidate();
    }

    private void controlosToolStripMenuItem_Click(object sender, EventArgs e)
    {
        controlosToolStripMenuItem.Checked = !controlosToolStripMenuItem.Checked;

        if (!controlosToolStripMenuItem.Checked)
        {
            Color trans = Color.Transparent;
            lapisControlos = new Pen(trans);
        }
        else
        {
            lapisControlos = new Pen(Color.Black);
        }
        PanelDraw.Invalidate();
    }

    private void corDosControlosToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (colorDialog.ShowDialog() == DialogResult.OK)
        {
            Color controlosCor = colorDialog.Color;
            lapisControlos.Color = controlosCor;
            PanelDraw.Invalidate();
        }
    }

    private void PanelDraw_MouseMove(object sender, MouseEventArgs e)
    {
        StatusLabelPosicao.Text = "Rato = (" + e.X + "|" + e.Y + ")";

        if (sele == 1)
        {

            for (int i = 0; i < 10; i++)
            {
                if (ct - 1 == i)
                {
                    switch (i)
                    {
                        case 0:
                            p1.X = e.X;
                            p1.Y = e.Y;
                            break;

                        case 1:
                            p2.X = e.X;
                            p2.Y = e.Y;
                            break;

                        case 2:
                            p3.X = e.X;
                            p3.Y = e.Y;
                            break;

                        case 3:
                            p4.X = e.X;
                            p4.Y = e.Y;
                            break;

                        case 4:
                            p5.X = e.X;
                            p5.Y = e.Y;
                            break;

                        case 5:
                            p6.X = e.X;
                            p6.Y = e.Y;
                            break;

                        case 6:
                            p7.X = e.X;
                            p7.Y = e.Y;
                            break;

                        case 7:
                            p8.X = e.X;
                            p8.Y = e.Y;
                            break;

                        case 8:
                            p9.X = e.X;
                            p9.Y = e.Y;
                            break;

                        case 9:
                            p10.X = e.X;
                            p10.Y = e.Y;
                            break;

                    }
                }
            }
            PanelDraw.Invalidate();
        }
    }

    private void sairToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void PanelDraw_Resize(object sender, EventArgs e)
    {

        int w = PanelDraw.DisplayRectangle.Width;
        int h = PanelDraw.DisplayRectangle.Height;
        StatusLabelScreen.Text = "Screen: (w: " + w + "| h: " + h + ")";
        PanelDraw.Invalidate();
    }

    private void corDasLinhasToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (colorDialog.ShowDialog() == DialogResult.OK)
        {

            Color linhasCor = colorDialog.Color;
            lapisLinhas.Color = linhasCor;
            PanelDraw.Invalidate();
        }
    }

    private void PanelDraw_click(object sender, MouseEventArgs e)
    {
        Point[] pontos = { p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 };
        if (sele == 1)
        {

            for (int i = 0; i < 10; i++)
            {
                if (ct - 1 == i)
                {
                    switch (i)
                    {
                        case 0:
                            p1.X = e.X;
                            p1.Y = e.Y;
                            break;

                        case 1:
                            p2.X = e.X;
                            p2.Y = e.Y;
                            break;

                        case 2:
                            p3.X = e.X;
                            p3.Y = e.Y;
                            break;

                        case 3:
                            p4.X = e.X;
                            p4.Y = e.Y;
                            break;

                        case 4:
                            p5.X = e.X;
                            p5.Y = e.Y;
                            break;

                        case 5:
                            p6.X = e.X;
                            p6.Y = e.Y;
                            break;

                        case 6:
                            p7.X = e.X;
                            p7.Y = e.Y;
                            break;

                        case 7:
                            p8.X = e.X;
                            p8.Y = e.Y;
                            break;

                        case 8:
                            p9.X = e.X;
                            p9.Y = e.Y;
                            break;

                        case 9:
                            p10.X = e.X;
                            p10.Y = e.Y;
                            break;

                    }

                    PanelDraw.Update();
                    sele = 0;

                }
            }
        }
        else
        {
            ct = 0;
            foreach (Point point in pontos)
            {
                ct++;
                if (e.X >= point.X - 3 && e.X <= point.X + 3 && e.Y >= point.Y - 3 && e.Y <= point.Y + 3)
                {
                    sele = 1;
                    return;
                }
            }
        }
        PanelDraw.Invalidate();
    }
}

} ``
有没有办法让线条画得更快?每次我移动鼠标,它们都会更新,我也想这样。但是它们一直在闪烁。我试过update()而不是validate,但是线条在点击后更新,所以我看不到它。
有没有办法让它平稳过渡?

bnl4lu3b

bnl4lu3b1#

我用过这个,发现在其他职位,它的工作相当不错:

typeof(Panel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty
            | BindingFlags.Instance | BindingFlags.NonPublic, null,
            YourPanelName, new object[] { true });

职位:https://stackoverflow.com/a/31562892/16282563

相关问题