简体   繁体   English

单击按钮后如何绘制?

[英]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: 如果我没看错,您可能会有一个布尔变量,并在单击按钮时将其设置为true ,例如:

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 不要忘记将myButton_Click分配给按钮的Click事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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