Custom Button Background

I hope I'm just missing something basic.

Is it possible to have a custom background (say, gradient) for a System.Windows.Forms.Button without having to do *ALL* the drawing yourself.

OnPaintBackground does not appear to be called; the paint code for Button appears to be integrated with it's background logic.


Answer this question

Custom Button Background

  • Sanjoy Bhowal

    No, you can't. You need to paint the button completly. The Button paints itself completly in OnPaint. If you draw something in OnPaintBackground or in OnPaint before calling base.OnPaint the button will draw itself over it so it does not have any effect. If you are drawing the background after you call base.OnPaint you will draw over the text painted by the Button class.

    What's so big deal in drawing it yourself It's just the background (that you want to change anyway) and some text after all.


  • GSK_phili

    Thank you very much for confirming what I'd feared by digging through the .NET code (Reflector is fun).

    Sadly the big deal is it's more than just a background and some text. There is an image property, and the twisted relationship between TextAlign, ImageAlign, and TextImageRelation. Try experimenting with those 3 to see the fun.

    The routines to determine WHERE the text and image belong in all the permutations are beyond what I want to duplicate, and our users require no change to the core SWF.Button functionality.


  • TheresaKad

    Well, you could have a gradient bitmap that you could pass along to the BackgroundImage property and you wouldn't have to do any painting. You'd just have to create the gradient bitmaps with a paint tool and add them to your application.



  • Custom Button Background