Click here to Skip to main content
6,597,576 members and growing! (23,900 online)
Email Password   helpLost your password?
Desktop Development » Static & Panel Controls » General     Intermediate

A Gradient Static Control

By Irek Zielinski

A Static control will gradient
VC6, VC7Win2K, WinXP, MFC, Dev
Posted:26 Sep 2002
Updated:26 Apr 2004
Views:88,443
Bookmarked:49 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 5.64 Rating: 4.14 out of 5
1 vote, 5.6%
1

2
2 votes, 11.1%
3
4 votes, 22.2%
4
11 votes, 61.1%
5

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:

  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.

    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


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

    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 standard font & centered text

    m_cExample2.SetWindowText("This text is in the center !");

    m_cExample2.SetColor(RGB(255,0,0));
    m_cExample2.SetGradientColor(RGB(0,0,0));

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.

A list of licenses authors might use can be found here

About the Author

Irek Zielinski


Member
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: http://www.ireksoftware.com
Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Static & Panel Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
Generalunresolved externals Pinmembercjpm10010:23 7 Apr '05  
GeneralRe: unresolved externals Pinmembercjpm10013:15 7 Apr '05  
GeneralI'm wondering. PinmemberKimjuhoon16:20 25 Feb '04  
GeneralRe: I'm wondering. PinmemberIrek Zielinski7:51 26 Feb '04  
Generalhow to implement this with text scroll PinmemberCodeFlatter7:41 10 Dec '03  
GeneralA few notes PinmemberChopper3:29 21 May '03  
GeneralRe: A few notes PinmemberIrek Zielinski13:05 8 Oct '03  
GeneralAlternative PinsussDave_9:04 16 May '03  
GeneralIs something missing? PinmemberWREY12:28 27 Sep '02  
GeneralReplacement for the DLL call PinmemberGregor S.4:04 27 Sep '02  
GeneralRe: Replacement for the DLL call PinmemberEd Gadziemski4:11 27 Sep '02  
GeneralRe: Replacement for the DLL call PinmemberGregor S.4:11 27 Sep '02  
GeneralRe: Replacement for the DLL call PinsussAnonymous22:34 3 Oct '02  
GeneralOpening statement PinsussAmita Buch1:09 27 Sep '02  
GeneralRe: Opening statement Pinmemberirekz@yahoo.com5:46 31 Jan '03  
GeneralI am curious PinmemberJörgen Sigvardsson0:40 27 Sep '02  
GeneralRe: I am curious PinmemberEd Gadziemski4:07 27 Sep '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Apr 2004
Editor: Nishant Sivakumar
Copyright 2002 by Irek Zielinski
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project