Creating the CGradientStatic Control



Click here for a larger image.

Environment:

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 (http://www.codeproject.com/menu/titlemenu.asp) 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
m_pBoldFont->CreateFont(25,0,0,0,900,0,0,0,0,0,0,
                        ANTIALIASED_QUALITY,0,"Arial");

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

//Use font of dialog controls, centered text
m_cExample2.SetWindowText("This text is in the center !");
m_cExample.SetFont(m_cOrdinaryButton.GetFont());
      //this is a small trick to obtain default font of this dialog
m_cExample2.SetColor(RGB(255,0,0));
m_cExample2.SetGradientColor(RGB(0,0,0));

Limitations

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.

Author

Ireneusz Zielinski, author@trayhelper.com, POLAND (author of Tray Helper: www.trayhelper.com).

Updates

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.

Downloads

Download source - 18 Kb


Comments

  • Thanks!

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

    Originally posted by: Maris

    Thanks for a nice piece of reliable code!

    Reply
  • 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
    SetRect(&stepR,
    r.left+(int)(iOnBand * fStep),
    r.top,
    r.left+(int)((iOnBand+1)* fStep),
    r.bottom);
    // set current color
    color = RGB((GetRValue(cRight)-GetRValue(cLeft))*((float)iOnBand)/255.0f+GetRValue(cLeft),
    (GetGValue(cRight)-GetGValue(cLeft))*((float)iOnBand)/255.0f+GetGValue(cLeft),
    (GetBValue(cRight)-GetBValue(cLeft))*((float)iOnBand)/255.0f+GetBValue(cLeft));
    // fill current band
    pDC->FillSolidRect(stepR,color);
    }
    }

    Reply
  • 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.

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

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Recently, Forrester Consulting conducted a Total Economic Impact™ (TEI) study to better understand the impact of incorporating Polycom voice solutions with Microsoft Lync. Forrester interviewed a company that had already deployed the Lync/Polycom combination to determine its potential return on investment (ROI). Microsoft Lync provides presence information and promotes voice and video collaboration. Polycom extends the benefits of Lync with voice endpoints and further enables collaboration in multiple …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds