0

I want to draw a figure that will change shape when I move the track bar to a different value. For example for value=1 pic1

Next I draw for second figure for value=2

pic2

I want only one shape. What should I do? Shape should change in every value of Track Bar. Here some my code for DrawTickedCircle:

private void DrawTickedCircle(Graphics gr, Pen circle_pen, Pen tick_pen,float cx, float cy, float rx, float ry,
                float num_theta, float num_ticks, float tick_fraction)
{
    List<PointF> points = new List<PointF>();

    float dalpha = (float)(2 * Math.PI / num_theta);
    float alpha = 0;

    for (int i = 0; i < num_theta; i++)
    {
        float x = (float)(cx + rx * Math.Cos(alpha));
        float y = (float)(cy + ry * Math.Sin(alpha));
        points.Add(new PointF(x, y));
        alpha += dalpha;
    }
    gr.DrawPolygon(circle_pen, points.ToArray());

    dalpha = (float)(2 * Math.PI / num_ticks);
    alpha = 0;

    float rx1 = rx * (1 - tick_fraction);
    float ry1 = ry * (1 - tick_fraction);

    for (int k = 0; k < num_ticks; k++)
    {
        float x1 = (float)(cx + rx * Math.Cos(alpha));
        float y1 = (float)(cy + ry * Math.Sin(alpha));
        float x2 = (float)(cx + rx1 * Math.Cos(alpha));
        float y2 = (float)(cy + ry1 * Math.Sin(alpha));
        gr.DrawLine(tick_pen, x1, y1, x2, y2);
        alpha += dalpha;
    }
}

For trackBarScroll function:

private void trackBar1_Scroll(object sender, EventArgs e)
{
    var g = pictureBox1.CreateGraphics();

    float cx = ClientRectangle.Width / 2;
    float cy = ClientRectangle.Height / 2;
    float rx = Math.Min(cx, cy);
    float ry = rx;

    rx *= 0.8f;
    ry *= 0.6f;

    if (trackBar1.Value == 1)
    {
        DrawTickedCircle(g, Pens.Black, Pens.Black, cx, cy, rx, ry, 8, 8, 0.1F);
    }
    else if (trackBar1.Value == 2)
    {
        DrawTickedCircle(g, Pens.Red, Pens.Red, cx, cy, rx, ry, 255, 255, 0.1f);
    }
    g.Dispose();
}

I don't want draw these in PaintEvent. Thanks for help!

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
Jola
  • 31
  • 5
  • You need to repaint the figure when the calculations are completed. So do the calculations and then repaint the g object. – jdweng Aug 11 '20 at 12:45
  • 1
    Use the paint event of the container to do your drawing. Don't use CreateGraphics. `var g = pictureBox1.CreateGraphics();` is a temporary drawing, and will be erased by simply minimizing and restoring the form. Use `e.Graphics.Clear(pictureBox1.BackColor);` at the beginning of your paint routine to clear the control. – LarsTech Aug 19 '20 at 13:53

1 Answers1

-1

This answer might be a few days late, but to answer your question: you should invalidate the control and force it to repaint it immediately when the value is changed. This can be done by the Refresh method. Add the following line into your Scroll event of your trackbar before you create the Graphics object.

pictureBox1.Refresh();
keco
  • 136
  • 9