Click here to Skip to main content
Click here to Skip to main content

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

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

Share

About the Author

vasarlajagadish
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 PinmemberQu Dong19-Sep-13 0:31 
GeneralMy vote of 5 Pinmemberahonly4u5-Jan-12 6:58 
GeneralMy vote of 1 PinmemberMartin Richter [MVP C++]18-Aug-09 21:30 
GeneralGreat! Pinmembercristitomi17-Mar-07 5:11 
GeneralRe: Great! Pinmemberhuming1986102319-Nov-07 14:10 
GeneralThis has been done a 1000 times Pinmembernorm .net18-Oct-06 4:02 
GeneralRe: This has been done a 1000 times PinmemberKochise19-Oct-06 22:08 
GeneralRe: This has been done a 1000 times PinmemberJim Crafton20-Oct-06 9:31 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 18 Oct 2006
Article Copyright 2006 by vasarlajagadish
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid