CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Controls >> Static Control


Creating the CGradientStatic Control
Rating: none

Irek Zielinski (view profile)
October 14, 2002



(continued)




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

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Thanks! - Legacy CodeGuru (01/07/2003)
gradient function for NT - Legacy CodeGuru (09/24/2002)
Nice, but not available on NT - Legacy CodeGuru (09/23/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES