Click here to Skip to main content
13,598,644 members
Click here to Skip to main content
Add your own
alternative version


75 bookmarked
Posted 26 Sep 2002

A Gradient Static Control

, 26 Apr 2004
Rate this:
Please Sign up or sign in to vote.
A Static control will gradient


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:

  1. Copy files GradientStatic.h and GradientStatic.cpp to your project directory, and add those files to your project.
  2. Put #include "GradientStatic.h" in files where you plan to use this class.
  3. Change CStatic definitions to CGradientStatic.
  4. 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.
    //this will use one of system defined colors

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

void void SetVerticalGradient(BOOL a_bVertical = TRUE)
    //Use this function to set vertical gradient

Example of Use

m_pBoldFont = new CFont; //delete it in destructor

//Use big font and standard colors
m_cExample.SetWindowText("This is CGradientStatic example :)");

//Use standard font & centered text
m_cExample2.SetWindowText("This text is in the center !");



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 :)


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.


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.

A list of licenses authors might use can be found here


About the Author

Irek Zielinski
Web Developer
United Kingdom United Kingdom
Irek works as a C++ senior software developer.
He runs also his own small shareware bussines (He is author of few quite popular applications like: Tray Helper or Time Adjuster).

Occasionaly he posts articles to Codeguru or Codeproject. Besides C++ he likes motorcycles and paragliding.
Check out his software at:

You may also be interested in...


Comments and Discussions

Questionbutton disappear Pin
Member 1225612226-Feb-17 15:39
memberMember 1225612226-Feb-17 15:39 
Generalunresolved externals Pin
cjpm1007-Apr-05 9:23
membercjpm1007-Apr-05 9:23 
GeneralRe: unresolved externals Pin
cjpm1007-Apr-05 12:15
membercjpm1007-Apr-05 12:15 
GeneralI'm wondering. Pin
Kimjuhoon25-Feb-04 15:20
memberKimjuhoon25-Feb-04 15:20 
GeneralRe: I'm wondering. Pin
Irek Zielinski26-Feb-04 6:51
memberIrek Zielinski26-Feb-04 6:51 
Questionhow to implement this with text scroll Pin
CodeFlatter10-Dec-03 6:41
memberCodeFlatter10-Dec-03 6:41 
GeneralA few notes Pin
Chopper21-May-03 2:29
memberChopper21-May-03 2:29 
GeneralRe: A few notes Pin
Irek Zielinski8-Oct-03 12:05
memberIrek Zielinski8-Oct-03 12:05 
GeneralAlternative Pin
Dave_16-May-03 8:04
sussDave_16-May-03 8:04 
QuestionIs something missing? Pin
WREY27-Sep-02 11:28
memberWREY27-Sep-02 11:28 
GeneralReplacement for the DLL call Pin
Gregor S.27-Sep-02 3:04
memberGregor S.27-Sep-02 3:04 
GeneralRe: Replacement for the DLL call Pin
Ed Gadziemski27-Sep-02 3:11
memberEd Gadziemski27-Sep-02 3:11 
GeneralRe: Replacement for the DLL call Pin
Gregor S.27-Sep-02 3:11
memberGregor S.27-Sep-02 3:11 
GeneralRe: Replacement for the DLL call Pin
Anonymous3-Oct-02 21:34
sussAnonymous3-Oct-02 21:34 
GeneralOpening statement Pin
Amita Buch27-Sep-02 0:09
sussAmita Buch27-Sep-02 0:09 
GeneralRe: Opening statement Pin
irekz@yahoo.com31-Jan-03 4:46
memberirekz@yahoo.com31-Jan-03 4:46 
GeneralI am curious Pin
Jörgen Sigvardsson26-Sep-02 23:40
memberJörgen Sigvardsson26-Sep-02 23:40 
GeneralRe: I am curious Pin
Ed Gadziemski27-Sep-02 3:07
memberEd Gadziemski27-Sep-02 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180621.3 | Last Updated 27 Apr 2004
Article Copyright 2002 by Irek Zielinski
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid