Reputation: 18068
I've made a class GradientButton
which suppose to be a Button which is filled with gradient background.
I draw gradient filling in the OnPaintBackground()
method. Unfortunately it is never invoked, of course I added a GradientButton
to a Form
via toolbox:
public class GradientButton : Button {
public Color Color1 { get; set; }
public Color Color2 { get; set; }
public float Angle { get; set; }
public GradientButton() {
Color1 = Color.YellowGreen;
Color2 = Color.LightGreen;
Angle = 30;
}
protected override void OnPaintBackground(PaintEventArgs e) {
base.OnPaintBackground(e);
Debug.WriteLine("This never prints");
using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,
Color1,
Color2,
Angle)) {
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
Invalidate();
}
}
Question: How fill the button's background with the gradient? Why OnPaintBackground
is not invoked? As far as I know it should be calledbefore OnPaint
method.
Upvotes: 1
Views: 3107
Reputation: 205629
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.
Upvotes: 5
Reputation: 24382
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
Upvotes: 1
Reputation: 1006
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.
Upvotes: -2