The OnPaintBackground method is never invoked for control derived from Button

≡放荡痞女 提交于 2019-11-29 17:03:13

This is because the Button class has ControlStyles.Opaque flag set, which according to the documentation:

If true, the control is drawn opaque and the background is not painted.

You can turn it off in your class constructor

SetStyle(ControlStyles.Opaque, false);

and your OnPaintBackground override will be invoked.

However, it would not help a lot - there is a reason the flag to be set to true - the OnPaint draws both background and face of the button, so whatever you do in OnPaintBackground will not have any affect of the button appearance. Unfortunately there is no option to paint just the background, so you need to override the OnPaint and actually draw everything yourself.

You need to set the style of the form in the constructor ...

this.SetStyle(ControlStyles.UserPaint, true);

to ensure the OnPaint method is overridden. There are many settings for the ControlStyle which you can combine

I would do this instead.

Firstly, change your constructor to this:

    public GradientButton()
   {
        Color1 = Color.YellowGreen;
        Color2 = Color.LightGreen;
        Angle = 30;
        Paint += new PaintEventHandler(GradientButton_Paint);
    }

And then add the below procedure:

    private void GradientButton_Paint(object sender,PaintEventArgs e)
    {
        Debug.WriteLine("This never prints");
        using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,Color1,Color2,Angle))
        {
            e.Graphics.FillRectangle(brush, this.ClientRectangle);
        }
    }

I'm not entirely sure why your code doesn't work, but the way I've described always works for me. Hope that's good enough.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!