Creating the CGradientStatic Control

Click here for a larger image.


I was looking trough this site (and a few others) to find a simple, easy-to-use control that looks better than the ordinary CStatic. Unfortunely, I was not successful. There are a lot of cool controls around, but I needed something different.

In one of the articles, I read about the CTitleMenu ( 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.

Here's how to add this control to your project:

  1. Copy the GradientStatic.h and GradientStatic.cpp files to your project directory; 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:

  • On the left side is a color of the active application's caption (COLOR_ACTIVECAPTION).
  • On the right side is a color of the button face (COLOR_BTNFACE).
  • The text is painted using a color of the text of the application's caption.

void SetColor(COLORREF cl);

Use this function to change the left color of the gradient. For example:

SetColor(RGB(255,0,0));                        //this will set a
                                               //RED color.
SetColor(GetSysColor(COLOR_ACTIVECAPTION));    //this will use one
                                               //of the system-
                                               //defined colors

void SetGradientColor(COLORREF cl);

Use this function to change the right gradient color.

void SetTextColor(COLORREF cl);

Use this function to change the text color.

void SetLeftSpacing(int iNoOfPixels);

If the text isn't centered, you can use this function to set a point where CGradientStatic should start painting text. On the default control, leave 10 pixels from the right side.

void SetCenterText(BOOL bCenter = TRUE);

Use this function to center text in the control.

Example of Use

m_pBoldFont = new CFont;      //delete it in desctructor

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

//Use font of dialog controls, centered text
m_cExample2.SetWindowText("This text is in the center !");
      //this is a small trick to obtain default font of this dialog


This control has some limitations. They were not a problem for me, so I didn't fix them.

  1. You have to set the text of the control in run-time (for example, in OnInitdialog()), using the SetWindowText() member function. Text set in resources is ignored.
  2. If you don't set a font for the control, the default font would be used (yup...this ugly one).
  3. There is no way to right-align the text right now. That will be easy to fix.


Ireneusz Zielinski,, POLAND (author of Tray Helper:


October 9, 2002

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


Download source - 18 Kb


  • Thanks!

    Posted by Legacy on 01/07/2003 12:00am

    Originally posted by: Maris

    Thanks for a nice piece of reliable code!

  • gradient function for NT

    Posted by Legacy on 09/24/2002 12:00am

    Originally posted by: Paolo Adami

    i've build a gradient static using the gradient function 
    described in "Programming Microsoft Windows with MFC - Jeff Prosise.
    2nd edition".(i've modify the example code for making more general function)
    It works on NT, too.

    ::DrawGradRect(CDC *pDC, CRect r, COLORREF cLeft, COLORREF cRight)
    CRect stepR; // rectangle for color's band
    COLORREF color; // color for the bands
    float fStep = ((float)r.Width())/255.0f; // width of color's band

    for (int iOnBand = 0; iOnBand < 255; iOnBand++)
    // set current band
    r.left+(int)(iOnBand * fStep),,
    r.left+(int)((iOnBand+1)* fStep),
    // set current color
    color = RGB((GetRValue(cRight)-GetRValue(cLeft))*((float)iOnBand)/255.0f+GetRValue(cLeft),
    // fill current band

  • Nice, but not available on NT

    Posted by Legacy on 09/23/2002 12:00am

    Originally posted by: Gabriel

    Nice, but it uses GradientFill which is only available on Windows 2000 - 98 and later.
    This means that it won't work on 95 and NT.
    95 is not used any more, but NT is still used.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds