Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Color Button

1.15/5 (26 votes)
18 Oct 20061 min read 1   3.1K  
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