I have been working on a graph editor for the university in which I have to constantly redraw several figures, when I do it it seems that the figures are blinking, I was reading and I found a control called BoubleBuffered but I did not understand how to use it. used before? Or are there other options to solve this problem? I leave a small fragment of code as an example of how to draw.
private void Form1_Load(object sender, EventArgs e)
{
grafo = new Grafo();
opcion = 0;
g = CreateGraphics();
bmp1 = new Bitmap(ClientSize.Width, ClientSize.Height);
band = false;
bandI = false;
bandF = false;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics gaux = CreateGraphics();
gaux.Clear(BackColor);
if (band)
{
bmp2 = (Bitmap)bmp1.Clone();
g = Graphics.FromImage(bmp2);
switch (opcion)
{
case 2:
g.DrawLine(grafo.penA, grafo.BuscaInterseccion(nodoP.centro, p2), p2);
break;
}
gaux.DrawImage(bmp2, 0, 0);
bmp2.Dispose();
band = false;
}
else
{
g = Graphics.FromImage(bmp1);
if (bandF)
{
switch (opcion)
{
case 1:
g.FillEllipse(grafo.brushN, p1.X - grafo.radio, p1.Y - grafo.radio, grafo.radio * 2, grafo.radio * 2);
g.DrawEllipse(grafo.penN, p1.X - grafo.radio + (grafo.penN.Width /2), p1.Y - grafo.radio + (grafo.penN.Width / 2), grafo.radio * 2 - (grafo.penN.Width / 2), grafo.radio * 2 - (grafo.penN.Width / 2));
if (grafo.numN >= 28 || grafo.edoNom)
g.DrawString(nodoP.nombre.ToString(), grafo.font, grafo.brushF, p1.X - 6, p1.Y - 6);
else
g.DrawString(((char)(nodoP.nombre + 64)).ToString(), grafo.font, grafo.brushF, p1.X - 6, p1.Y - 6);
break;
case 2:
g.DrawLine(grafo.penA, grafo.BuscaInterseccion(nodoP.centro, nodoAux.centro), grafo.BuscaInterseccion(nodoAux.centro, nodoP.centro));
break;
}
bandF = false;
}
if(bandI)
{
g.Clear(BackColor);
grafo.ImprimirGrafo(g);
bandI = false;
}
if(opcion == 6 || opcion == 7)
{
g.Clear(BackColor);
if (opcion == 7)
{
grafo.Clear();
grafo.numN = 1;
grafo.edoNom = false;
}
}
gaux.DrawImage(bmp1, 0, 0);
}
gaux.Dispose();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
p1 = e.Location;
textBox1.Text = p1.ToString();
switch(opcion)
{
case 1:
nodoP = new NodoP(grafo.numN++, p1);
grafo.Add(nodoP);
band = false;
bandF = true;
if (grafo.numN == 28)
{
bandI = true;
grafo.edoNom = true;
}
else
bandI = false;
Invalidate();
break;
case 2:
nodoP = grafo.BuscaNodo(p1);
if (nodoP != null)
{
band = true;
bandA = true;
}
else
{
band = false;
bandI = false;
}
break;
case 3:
nodoP = grafo.BuscaNodo(p1);
if (nodoP != null)
bandA = true;
else band = false;
break;
case 8:
p1 = e.Location;
break;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(e.Button.Equals(MouseButtons.Left) && opcion == 2 && bandA)
{
p2 = e.Location;
band = true;
Invalidate(false);
}
if (e.Button.Equals(MouseButtons.Left) && opcion == 3 && bandA)
{
band = false;
bandF = false;
bandI = true;
nodoP.centro = e.Location;
Invalidate();
}
}
The previous fragment exemplifies how (according to me) I manage the page, with g, gaux and the bmp to draw, thanks for your help:)