Owner Draw Button Step-by-Step

Environment: VC++6

I think some of you may not like the buttons in Windows. Sometimes, I think they're ugly. Fortunately, we can change the appearance of our buttons by overriding the DrawItem function of the CButton class. I 'm going to demonstrate the steps of the owner drawing button. In this article, I will make a class, which inherits from CButton class.

Let 's take a dialog-based MFC application as an example. In the File Menu, click New to add a new project. Then, choose MFC Application Wizard (exe). In the project name, type in OwnerDrawButton (just an example), and then click OK. In step one of the MFC App Wizard, choose Dialog Based. After pressing Finish, you are brought to the "New Project Information" page. You'll ignore this page, so press OK.

The edit window is displaying the IDD_OWNERDRAWBUTTON_DIALOG. We have to make our class first, so we don't have time to look at this dialog. Go to the ClassView, right-click "OwnerDrawButton classes", and choose "New class". For the class type, just leave the default "MFC Class". In the "Name" editbox, type "CMyButton" (just an example). Choose "CButton" from the "Base Class".

You have added a new class; it's time to override the DrawItem function. Right-click "CMyButton" in ClassView and choose "Add Virtual Function". The "New Virtual Override for class CMyButton" page opens. Double-click "DrawItem" in the left "New Virtual Functions" listbox. Afterward, 'DrawItem" will jump to the right "Existing virtual function overrides" listbox. Finally, press OK to add a new virtual override.

Go to the implementation of CMyButton::DrawItem, which is in MyButton.cpp. Add draw code in this function. Here is my drawing code to demonstrate how to use CDC to draw it.

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  CDC dc;
  dc.Attach(lpDrawItemStruct->hDC);     //Get device context object
  CRect rt;
  rt = lpDrawItemStruct->rcItem;        //Get button rect

  dc.FillSolidRect(rt, RGB(0, 0, 255)); //Fill button with blue color

  UINT state = lpDrawItemStruct->itemState; //Get state of the button
  if ( (state & ODS_SELECTED) )            // If it is pressed
    dc.DrawEdge(rt,EDGE_SUNKEN,BF_RECT);    // Draw a sunken face
    dc.DrawEdge(rt,EDGE_RAISED,BF_RECT);    // Draw a raised face

                        // Set the color of the caption to be yellow
  CString strTemp;
                        // Get the caption which have been set
                              // Draw out the caption
  if ( (state & ODS_FOCUS ) )       // If the button is focused
    // Draw a focus rect which indicates the user 
    // that the button is focused
    int iChange = 3;
    rt.top += iChange;
    rt.left += iChange;
    rt.right -= iChange;
    rt.bottom -= iChange;

Your job isn't finished yet. Go back to the ResourceView and click "IDD_OWNERDRAWBUTTON_DIALOG". Yes, you are right! You are going to edit your dialog box. Drag a button to the dialog box. Now, modify its properties by right-clicking it and choosing "Properities". For its ID, call it "IDC_COLOREDBUTTON" and caption it "Colored Button". You also have to allow it to be owner draw. Go to the Styles tab and check "Owner Draw". Then, close the Properties dialog box.

You have to link this button to the CMyButton class. Press Ctrl+W to open the MFC Class Wizard. Under the "Member variables" page, double-click "IDC_COLOREDBUTTON", which is in the "Control IDs" listbox. You are then brought to the "Add Member Variable" dialog. For the "Member variable name", type "m_MyColoredButton". For the "Variable Type", choose "CMyButton", and then press OK. VC++ will inform you to check if there is an include statement in "OwnerDrawButtonDlg.h". You won't find it, so add #include "MyButton.h" at the beginning of OwnerDrawButtonDlg.h.

Press F7 to build your project. Afterward, run it and you will see the same as the picture at the beginning of this article.

This is the first time for me to write an English article. Feel free to give some bad comments to me. I am glad to read them.


Download file - 21 kb


  • Thank you.

    Posted by Goldstone on 05/12/2013 05:02pm

    Thank you for very helpful information

  • km7jbg,nike free online,ki7tqo

    Posted by di8rel on 03/28/2013 05:30am

    Hybrid golf clubs have exploded nike free run billig onto the golf scene over the last two to three years. It seems that every time you are out on the golf course, you see a golfer who has made the transition from traditional long irons and drivers to the hybrid drivers and irons. Why the big switch? Hybrid golf clubs are nike free run danmark much more forgiving than a traditional club and they are also much lighter than your old irons nike free run 3.0 or drivers because of the composite material used in creating these clubs.

  • cf8gmp,nike free dame,jc0yor

    Posted by fp3rpf on 03/28/2013 12:49am

    Hybrid golf clubs have exploded nike free 3.0 women onto the golf scene over the last two to three years. It seems that every time you are out on the golf course, you see a golfer who has made the transition from traditional long irons and drivers to the hybrid drivers and irons. Why the big switch? Hybrid golf clubs are nike free 3.0 women much more forgiving than a traditional club and they are also much lighter than your old irons billige nike free run or drivers because of the composite material used in creating these clubs.

  • Higher education regarding increasing marijuana with Los angeles

    Posted by NeleAstence on 02/18/2013 03:37pm

    Marijuana Seeds Magazine is a online cannabis plant alongside vigilance and Marijuana its and growing marijuana is permissible. The lack of support group is sometimes a big factor and drug in the most powerful country in the world. Smoking weed, marijuana or cannabis was once considered county still the debate on the marijuana plants are going on. [url=http://vaporizerworld.org/best-vaporizer/]vaporizerworld[/url] It's also use in many criminal activities, they use it for the effects of marijuana then we are ready to aware about all key points of marijuana.

  • Great. A good and nice simple solution.

    Posted by vladovidiu on 11/08/2005 06:27am

    I just love simple solutions. And if you create the button dynamically it works like a charm.

  • Bitmapped Button

    Posted by Legacy on 02/26/2004 12:00am

    Originally posted by: BIGBUBBA

    I love your example on the colored button. Its easy to understand. I was wondering if you could explain how to do the same thing but for a button that is an icon? I looked on here for bitmapped button. I simply could not understand the guy cause evidently some steps were skipped.

  • What about a CFormView based Classe?

    Posted by Legacy on 10/14/2003 12:00am

    Originally posted by: Lslx

    I try almost all exemples on this thread and I'm still not able to use a custom button on a CFormView class.

    So I add a button in my CFormView ressource, set the ownerdraw to true and give a new ID. After, I add a variable in my CFormView with class wizard( So the DDX stuff appear).

    When I start my program, I have an assertion faillure in the CButton::DrawItem function.

    Have an hint?

    I also tried that: don't put a control button in the ressource editor and "Create" my button in the CMyFormView::OnCreate(...) fonction. like:
    CRect rect(80,80,14,14);
    m_myButton.Create("C",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, rect, this, IDC_MYBUTTON);

    But, nothing appear!



  • Thanks a lot!!

    Posted by Legacy on 09/30/2003 12:00am

    Originally posted by: Louis

    Thanks for the detail instruction.
    It does help me very much.

  • Didnt work for me

    Posted by Legacy on 09/22/2003 12:00am

    Originally posted by: Jellow

    Didnt work for me. Simply followed what you said. No errors. Nothing. But no result. But I didnt add a new button. But used for a button that was already existing and was functional. Any problem??
    Then modified the project to use the class with a newly added button resource. Even then no expected result obtained. What can be the reason?

  • Thank you

    Posted by Legacy on 07/09/2003 12:00am

    Originally posted by: HyunMin

    Thank you for very helpful information

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Organizations are increasingly gravitating toward mobile-first application development as they assess the need to revamp their application portfolios to support touch computing and mobility. Consumerization has brought higher expectations for application usability along with the mobile devices themselves. Enterprises are increasingly shifting their new application acquisitions and development efforts toward mobile platforms. With this backdrop, it is natural to expect application platform vendors to invest in …

  • Cloud computing isn't new, but it's still a hot topic in technology circles. Moving to the cloud has done great things for many businesses. When it comes to handling a basic business necessity, cloud computing has brought many improvements to overall business continuity. Using the cloud for DR makes a lot of sense for many, but it's not exactly the most simple concept. Read this eBook to find answers to all the key questions IT professionals have about cloud-based disaster recovery, and helpful info around the …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date