Introduction

I was looking through this site (and a few others) in order to find a simple, easy to use control that looks better than the ordinary CStatic. Unfortunately, I was not successful - there are a lot of cool controls around but I needed something different. One of the articles I read entitled CTitleMenu contains a class that creates a menu with a nice gradient title - that was it!. I decided to incorporate some of the code to create a nice static control.... and here it is.

How to add this control to your project:

Copy files GradientStatic.h and GradientStatic.cpp to your project directory, and add those files to your project.

Put #include "GradientStatic.h" in files where you plan to use this class.

Change CStatic definitions to CGradientStatic.

Use the API described below.

CGradientStatic API

This control is derived from CStatic and adds only a few new functions. Please note that this class is not perfect. If you need some other features - just modify it!

By default CGradientStatic uses the following colors:

The left side is the color of the active application's caption (COLOR_ACTIVECAPTION)

The right side is the color of the button face (COLOR_BTNFACE)

The text is painted used the color of the text on the caption of the application.

void SetColor(COLORREF cl);
// Use this function to change left color of gradient.
//For example this will set a RED color.
SetColor(RGB(255,0,0));
//this will use one of system defined colors
SetColor(GetSysColor(COLOR_ACTIVECAPTION));
void SetGradientColor(COLORREF cl);
//Use this function to changes right color of gradient.
void SetTextColor(COLORREF cl);
//Use this function to changes color of text.
void SetLeftSpacing(int iNoOfPixels);
//If text isn't centered you can use this function to set a point where
//CGradientStatic should start painting text.
//On default control leaves 10 pixels from the right side.
void SetTextAlign(int iAlign )
//Use this function to change text aligment.
//Acceptable values: 0 - left, 1 - center, 2 -right
voidvoid SetVerticalGradient(BOOL a_bVertical = TRUE)
//Use this function to set vertical gradient

Notes

This control loads the GradientFill function dynamically from msimg32.dll. If this load fails, for whatever reason, no gradient fill will be performed, but no crash should occur either. This control is free to use

Updates

26th April 2004

Added support of vertical gradients, new improved demo application.

6th May 2003

Removed few limitations of previous version. Now code is smaller, simpler and works a bit better.

6th Octber 2002

Paolo Adami suggested a small addition to this control. Thanks to function DrawGradRect(CDC *pDC, CRect r, COLORREF cLeft, COLORREF cRight) - control will draw gradient background also on systems that don't have msimg32.dll library installed. This code is not such a fancy algorithm as msimg32.dll library and produces worse effect - but it works fine.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Comments and Discussions

Hi, it is a very useful CStatic control. However, the imported DLL library is not necessary if you do the gradient drawing yourself.

I don't know if I'm allowed to use the drawing code used by Matt Weagle in his terrific gradient progress control. I simply replaced the DLL call with his gradient drawing code and now the DLL is not necessary anymore.

I would like to post the code you need to replace, but need to ask Matt for permission first

Does it use the same trivertex algorithm as the DLL GradientFill? If so, then the quality should be excellent unlike algorithms that just step through color bands and (perhaps) do a little dithering, thus leaving a visible banding effect.

I have just posted an update to CGradientStatic article and code. Thise new code will try to load dll library and use GradientFill function. If it fails it will use simple gradient fill algorithm - and produce a bit worse effect that this one form dll but it works fine :;P