How to customies the button background

  • Thread starter Thread starter RA
  • Start date Start date
R

RA

Hi

I want to be able to customies (using a different brush) the background of a
Windows.Forms.Button. How can I do it? If I derive and override then there
is no paint method that I can set the brush. Which way can I do this?

Thanks
 
RA,

you can do this by creating a class derived from button and overriding the
"onPaintbackground" event. I've had to do this with a panel that I wanted
to have transparent and it worked well See code below.

hth,

Marco

private class TransparentPanel : TextBox

{

public TransparentPanel()

{

}

//This method makes sure that the background is what is directly behind the
control

//and not what is behind the form...

override protected CreateParams CreateParams

{

get

{

CreateParams cp = base.CreateParams;

cp.ExStyle |= 0x20;

return cp;

}

}

override protected void OnPaintBackground(PaintEventArgs e)

{

// do nothing

}

}
 
I want to be able to customies (using a different brush) the background of a
Windows.Forms.Button. How can I do it? If I derive and override then there
is no paint method that I can set the brush. Which way can I do this?

Override OnPaint if you want more than just changing the background color.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
using(HatchBrush br = new HatchBrush(HatchStyle.Cross, Color.Red,
Color.Bisque))
{
Rectangle rc = base.DisplayRectangle;
rc.Inflate(-2, -2);
e.Graphics.FillRectangle(br, rc);
}
}
 
Thanks,

I tried that but the OnPaintBackground never get called for the button. Any
ideas?
 
RA said:
Using this I am loosing the text - all I can see is the background color
with no text.

If you override OnPaint, you have complete control over how the text is
displayed. Call e.Graphics.DrawString. But if you just want to change the
color there are simpler ways to do it.
 
Back
Top