Painting funky window backgrounds

I wanted a smooth gradient window background in the app I just posted, and it pretty much comes down to two lines of code with Windows.Forms:

    LinearGradientBrush br = new LinearGradientBrush(ClientRectangle,
       
Colors.Lavender, Colors.LightBlue, 15, true);
    e.Graphics.FillRectangle(br, 0, 0, Width, Height);

I’d like to try to base the gradient colours on variations of the colour the user has selected for his window background, just to add a subtle gradient while still keeping the theme the user has for the rest of the apps on the desktop.

Note that your Paint handler will be called before the window’s size has been determined, so you’re going to get a Width and Height of 0.  This messes up FillRectangle, so check for a Width or Height > 0 doing this.

This method of background painting requres that all the controls on the forms be transparent (set their colour to Transparent, from the list of Web colours).  Unfortunately some controls just can’t be made transparent this way, including the Tab control.  That’s why in the SMS Chat app the Options dialog is the only dialog that doesn’t have the transparent background.