Colored buttons

Download Source Code (3Kb) and Demo Project (13Kb)


One of the problems with the Windows architecture is in the inability to draw buttons in a color other than gray. While the class offered here isnt cutting-edge technology, it is something that a lot of people ask for and use (especially in multi-media applications). The only way to draw the buttons in color is to use the owner-draw capabilities (or create a bitmap button which is a horrible solution). CColorButton does all this behind the scene and allows the developer to:

  • Draw a button in DevStudio's editor for placement, size, and setting button captions.
  • Set the foreground color (text color), background (button color), and disabled color (Note: the focus rectangle is drawn in the text color).
  • Set the bevel width

Consider this simple dialog, displayed in the first example with light blue buttons and blue text and in the second example, with multiple colored buttons and different text colors: Note: CTLCOLOR changes the background but not the buttons):

Operations

There is only one function, Attach(), which initializes an owner-draw button usually in a dialogs OnInitDialog() function.

BOOL Attach(const UINT nID, CWnd* pParent, 
            const COLORREF BGColor = RGB(192, 192, 192), 
            const COLORREF FGColor = RGB(1, 1, 1), 
            const COLORREF DisabledColor = RGB(128, 128, 128), 
            const UINT nBevel = 2);

Where:

  • nID is your button's control ID and
  • pParent is the parent window.

    Only the first two arguments are required; background, foreground, and disabled colors default to a gray button, black text, and dark gray disabled text.

  • nBevel (bevel width) defaults to 2).

How to use CColorButton

  • Include colorBtn.h and colorBtn.cpp in your project.
  • Draw a button in Developer Studio; make sure the button's Owner-Draw property is checked.
  • In your program's .h file, include "#include colorbtn.h"
  • Also in your program's .h file, declare as many variables as you have buttons

    CColorButton m_btn1;
    CColorButton m_btn2;
    etc.

  • In your program's OnInitDialog function, initialize the colors:
   VERIFY(m_btn1.SetColors(IDOK, this, CLOUDBLUE, DKBLUE, WHITE));
   VERIFY(m_btn2.SetColors(IDCANCEL, this, DKBLUE, WHITE));

Note: the colors used in this example - BLACK, WHITE, BLUE, DKGRAY, etc. - are COLORREF constants that you define in your own program via the RGB() macro:

	const COLORREF CLOUDBLUE = RGB(128, 184, 223);
	const COLORREF WHITE = RGB(255, 255, 255);
	const COLORREF BLACK = RGB(1, 1, 1);
	const COLORREF DKGRAY = RGB(128, 128, 128);
	const COLORREF etc...

That's it. It's pretty easy to use and very useful for multi-media applications. This is a very simple class to work with and anyone with even the slightest GDI background can add or modify functions.

Limitations:

  • At this time, you cannot reset the button's colors once the window has been realized.
  • It doesn't support a dynamic Create() function - yet.
  • It doesn't support 256 Color palette - yet.
  • It doesn't allow the font to be overridden for the button text - yet.
  • The bevel color isn't customizable - yet.
  • Final caveat: this class wasn't designed to be industrial strength and was written over a year ago - it has always been one of my own home-grown classes, so you are welcome to make it more robust.

Last updated: 9 May 1998



Comments

  • There are no comments yet. Be the first to comment!

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

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds