简体   繁体   中英

How to draw after button click?

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    var cp = new Point(Width / 2, Height / 2);
    DrawGradientCircle(e.Graphics, cp, 100);
}

private void DrawGradientCircle(Graphics gr, Point cp, float radius)
{
    var path = new GraphicsPath();
    path.AddEllipse(cp.X - radius, cp.Y - radius, 2 * radius, 2 * radius);
    using (var brush = new PathGradientBrush(path))
    {
        var blends = new ColorBlend(7);
        blends.Colors[0] = Color.Violet;
        blends.Positions[0] = 0;
        blends.Colors[1] = Color.Blue;
        blends.Positions[1] = 0.16f;
        blends.Colors[2] = Color.Aqua;
        blends.Positions[2] = 0.32f;
        blends.Colors[3] = Color.Lime;
        blends.Positions[3] = 0.48f;
        blends.Colors[4] = Color.Yellow;
        blends.Positions[4] = 0.64f;
        blends.Colors[5] = Color.Orange;
        blends.Positions[5] = 0.82f;    
        blends.Colors[6] = Color.Red;
        blends.Positions[6] = 1;
        brush.InterpolationColors = blends;
        gr.FillPath(brush, path);
    }
}

There is my code - i just want to draw the circle after button click, but how to do it? But I don't know how make a link

If I understood you right, you could have a boolean variable and set it to true when you click the button... something like:

private bool _buttonClicked = false;

void myButton_Click(object sender, EventArgs e)
{
   _buttonClicked = true;
   this.Invalidate(); // <-- invalidate the form so it's repainted
   this.Update(); // <-- optional: force a synchronous repaint
}

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    if(!_buttonClicked) return;

    // this will only happen after button is clicked
    var cp = new Point(Width / 2, Height / 2);
    DrawGradientCircle(e.Graphics, cp, 100);
}

Don't forget to assign myButton_Click to the button's Click event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM