Visual Studio 2005 Form Gradient

  • Thread starter Thread starter skylargivens
  • Start date Start date
I've created an application for Windows XP but I'm having trouble
getting the right "look". I'd like my application to look somewhat
like the Windows Vista sidebar:
http://www.winsupersite.com/showcase/winvista_rc1_gallery_01.asp

Notice how there is a black to transparent gradient on the right side
of the screen. Does anyone know any way acheive this effect using
Visual Studio 2005?

Sure.

1) Create the appropriate gradient in the Loaded event:

private void Form1_Load(object sender, EventArgs e)
{
Rectangle Client = ClientRectangle;
Bitmap Background = new Bitmap(Client.Width, Client.Height);
using (Graphics G = Graphics.FromImage(Background))
{
using (Brush B = new LinearGradientBrush(Client,
Color.FromArgb(0, Color.Black), // left transparent
// You may want more or less transparency on the right
Color.FromArgb(200, Color.Black), // right nearly opaque
LinearGradientMode.Horizontal))
{
G.FillRectangle(B, Client);
}
}
BackgroundImage = Background;
}

2) Override OnPaintBackground to only paint the background
(ie, don't first fill with the BackgroundColor):

protected override void OnPaintBackground(PaintEventArgs e)
{
if (BackgroundImage != null)
e.Graphics.DrawImage(BackgroundImage, ClientRectangle);
}
 
Jon said:
1) Create the appropriate gradient in the Loaded event:

I *meant* the Layout event. ;-)
2) Override OnPaintBackground to only paint the background
(ie, don't first fill with the BackgroundColor):

protected override void OnPaintBackground(PaintEventArgs e)
{
if (BackgroundImage != null)
e.Graphics.DrawImage(BackgroundImage, ClientRectangle);
}

Alas, it's nowhere near this easy. This approach doesn't handle
changes to the background (ie, if you minimize a window that's under
this gradient window, the background under the gradient doesn't
change) AND the background isn't drawn properly under, say, a
transparent label.
 
Back
Top