0

I am trying to do a Panel in c# using .net framework. I have a panel where I draw some lines and thots. And I want to control de bezier curves with it. And I came out with this: ``

namespace Bezier { public partial class FormMain : Form { Pen lapisLinhas = new Pen(Color.White, 3); Pen lapisPontos = new Pen(Color.Black, 2); Pen lapisControlos = new Pen(Color.Red, 3); Random 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();
    }
}

} ``

Is there a way to make the lines draw faster? Everytime I move the mouse, they update, and I want that. But they are just blinking all the time. I have tried update() instead of validate, but the line update after click, so I can´t see it properly.

Is there a way to make it smooth transition?

Filipe Cruz
  • 81
  • 1
  • 10
  • 1
    For the flicker part (Blinking..) you need a [`DoubleBuffered`](https://stackoverflow.com/questions/818415/how-do-i-double-buffer-a-panel) canvas to reduce the flicker. You can use `PictureBox` instead of `Panel` which is `DoubleBuffered` by default. Don't recreate the `Random` object `rnd = new Random();`. Just make it a class field and instantiate it in the constructor. Close the Form by calling `this.Close();` instead of `Application.Exit();` to exit the app the right way and not skip the termination events and methods .... – dr.null Nov 19 '22 at 23:49
  • Do you know a way to implement the doublebuffer in this situation? I have to use drawpanel for homework purpose. – Filipe Cruz Nov 20 '22 at 00:48
  • rnd fixed, exit fixed. I have to use panel and I came out with this, but I don´t know how to use it here: – Filipe Cruz Nov 20 '22 at 00:55
  • 1
    PictureBox and Label are two controls that are double-buffered out-of the box. Panel is a container and not really menat for drawing, although many use/abuse it for that.. – TaW Nov 20 '22 at 08:27

1 Answers1

0

I have used this, found in other post, and it worked pretty fine:

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

Post: https://stackoverflow.com/a/31562892/16282563

Filipe Cruz
  • 81
  • 1
  • 10
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 23 '22 at 00:26