Button onPaint event doesn't fire?!?!

  • Thread starter Thread starter mrabie
  • Start date Start date
M

mrabie

Hi all,

I am writing a class that inherets from the Button control to draw a
custom image on it. I override the OnPaint event but it never gets
called and the button draws the default shape n color... any ideas why
it might be doing that?

Thank

here's the code:

public partial class OSImageButton : Button
{
Bitmap _Image = null;
Bitmap offScreen;
Graphics gxBuffer;

public OSImageButton()
{
InitializeComponent();
offScreen = new Bitmap(this.Width, this.Height);
gxBuffer = Graphics.FromImage(offScreen);
}

public OSImageButton(IContainer container)
{
container.Add(this);

InitializeComponent();
offScreen = new Bitmap(this.Width, this.Height);
gxBuffer = Graphics.FromImage(offScreen);
}

public Bitmap Image
{
get { return _Image; }
set { _Image = value; }
}

protected override void OnPaint(PaintEventArgs e)
{
if (_Image != null)
{
gxBuffer.DrawImage(_Image, 0, 0);
e.Graphics.DrawImage(offScreen, 0, 0);
}
else
{
base.OnPaint(e);
}

}
}
 
You might need to draw the control completely yourself. there are tons of
articles on how to do that. If you want to keep really simple then you can
just use the PictureBox control and handle the Click event.

Anyway, here are a few articles on the net.

Here's an old one from Alex Yakhnin called "How to create a Microsoft .NETCF
based Image Button"
http://msdn2.microsoft.com/en-us/library/aa446518.aspx

And one from GotDotNet
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/picturebutton.aspx

I have a very simple example as well on my blog:
http://christian-helle.blogspot.com/2007/09/buttonex-owner-drawn-button-control.html
 
Back
Top