Click here to Skip to main content
13,353,124 members (54,622 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 18 Oct 2006

Color Button

, 18 Oct 2006
Rate this:
Please Sign up or sign in to vote.
Changing the Color of the Button control

Sample Image - btncolor.gif


This tutorial is a step by step procedure for drawing an owner-drawn button control.This tutorial is about the redrawing the button control.

1.Open up the AppWizard and create a new project titled btncolor.Just create a basic dialog box without any document / view architecture.

Sample Image - maximum width is 600 pixels

2.Then click on the ResourceVew tab in the 'Workspace' window. Proceed to edit the dialog box IDD_BTNCOLOR_DLG. It will already contain the Buttons 'Ok' and 'Cancel'. Delete the 'TODO :' message and cancel button.

3.Just drag n' drop the buttons needed, in this example 'color' . Then select and right click to edit their properties. The MFC keeps track of these buttons by their unique ID, a macro located in the "Resource.h" file. For code clarity, change the name of the ID to IDC_BUTTON_COLOR and modify the caption of the button.

4.Change the style of the push button to OwnerDraw.

Sample Image - maximum width is 600 pixels

5.Now that our buttons and are in place we need to wire them into our application. We do this via the class wizard. (Control + W) Click on the Member Variables tab .

Sample Image - maximum width is 600 pixels

6.Add the Message Map Entry WM_DRAWITEM for CBtnColorDlg.And Edit the code for OnDrawItem as shown below.

void CBtncolorDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
    // TODO: Add your message handler code here and/or call default
    if(nIDCtl==IDC_BUTTON_COLOR)         //checking for the button 
    CDC dc;
    RECT rect;
    dc.Attach(lpDrawItemStruct ->hDC);   // Get the Button DC to CDC
    rect = lpDrawItemStruct->rcItem;     //Store the Button rect to our local rect.

    dc.FillSolidRect(&rect,RGB(100,100,255));//Here you can define the required color to appear on the Button.
    UINT state=lpDrawItemStruct->itemState;  //This defines the state of the Push button either pressed or not. 

    if((state & ODS_SELECTED))


    dc.SetBkColor(RGB(100,100,255));   //Setting the Text Background color
    dc.SetTextColor(RGB(255,0,0));     //Setting the Text Color

    TCHAR buffer[MAX_PATH];           //To store the Caption of the button.
    ZeroMemory(buffer,MAX_PATH );     //Intializing the buffer to zero
        ::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window 
    dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the  Caption of Button Window 
    dc.Detach();  // Detach the Button DC
    CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);

That's it Build the Code and Run it.


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

Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
Qu Dong19-Sep-13 0:31
memberQu Dong19-Sep-13 0:31 
GeneralMy vote of 5 Pin
ahonly4u5-Jan-12 6:58
memberahonly4u5-Jan-12 6:58 
GeneralMy vote of 1 Pin
Martin Richter [MVP C++]18-Aug-09 21:30
memberMartin Richter [MVP C++]18-Aug-09 21:30 
GeneralGreat! Pin
cristitomi17-Mar-07 5:11
membercristitomi17-Mar-07 5:11 
Nevertheless, I say that for a beginner is a very good article.
If you could update your article and add a little bit more explanation about the actual drawing of the button then it is perfect!
Best regards,

I am in love with VC++
GeneralRe: Great! Pin
huming1986102319-Nov-07 14:10
memberhuming1986102319-Nov-07 14:10 
GeneralThis has been done a 1000 times Pin
norm .net18-Oct-06 4:02
membernorm .net18-Oct-06 4:02 
GeneralRe: This has been done a 1000 times Pin
Kochise19-Oct-06 22:08
memberKochise19-Oct-06 22:08 
GeneralRe: This has been done a 1000 times Pin
Jim Crafton20-Oct-06 9:31
memberJim Crafton20-Oct-06 9:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 18 Oct 2006
Article Copyright 2006 by vasarlajagadish
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid