按钮点击后如何绘制?

时间:2016-06-02 08:32:10

标签: c# winforms

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);
    }
}

有我的代码 - 我只想在点击按钮后绘制圆圈,但该怎么做? 但我不知道如何建立链接

1 个答案:

答案 0 :(得分:1)

如果我理解你的话,你可以有一个布尔变量,并在你点击按钮时将其设置为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);
}

不要忘记将myButton_Click分配给按钮的Click活动