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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read