www.pudn.com > mfc×ÊÔ´´óÈ«1.rar > color_button.shtml


 
 
 
 
    
    
   Controls - Colored buttons 
 
 
 
 
 
 

Colored buttons


This article was contributed by Bob Ryan.

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 isn’t 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:

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 dialog’s 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:

How to use CColorButton

 
   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:

Last updated: 9 May 1998


Goto HomePage
© 1998 Zafir Anjum 
Contact me: zafir@home.com