Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / MFC
Article

Color Button

Rate me:
Please Sign up or sign in to vote.
1.15/5 (26 votes)
18 Oct 20061 min read 111.6K   3.1K   41   9
Changing the Color of the Button control

Sample Image - btncolor.gif

Introduction

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.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0)); 

    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.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);

    }
    else
    {
        dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
    }

    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.

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


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseExcellent!!! Pin
phoenicyan26-May-21 10:24
phoenicyan26-May-21 10:24 
GeneralMy vote of 3 Pin
Qu Dong18-Sep-13 23:31
Qu Dong18-Sep-13 23:31 
GeneralMy vote of 5 Pin
ahonly4u5-Jan-12 5:58
ahonly4u5-Jan-12 5:58 
GeneralMy vote of 1 Pin
Martin Richter [rMVP C++]18-Aug-09 20:30
Martin Richter [rMVP C++]18-Aug-09 20:30 
GeneralGreat! Pin
cristitomi17-Mar-07 4:11
cristitomi17-Mar-07 4:11 
GeneralRe: Great! Pin
huming1986102319-Nov-07 13:10
huming1986102319-Nov-07 13:10 
GeneralThis has been done a 1000 times Pin
NormDroid18-Oct-06 3:02
professionalNormDroid18-Oct-06 3:02 
GeneralRe: This has been done a 1000 times Pin
Kochise19-Oct-06 21:08
Kochise19-Oct-06 21:08 
What's the problem ? It's a "beginner" article, for you it's trivial, not for beginners who comes from other horizon than MFC... I remember the days when I switched from embedded to Windows, I had to learn all that UI stuff, and beginner's article were from a precious help over advanced's article covering deeper details of the stuff :/

Kochise

In Code we trust !

GeneralRe: This has been done a 1000 times Pin
Jim Crafton20-Oct-06 8:31
Jim Crafton20-Oct-06 8: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.