Owner Draw Button Step-by-Step

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.

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.

Downloads

Comments

Thank you.

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.

Bitmapped Button

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.
Thanks
Tony

What about a CFormView based Classe?

Hi,
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);

Didnt work for me

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?

Top White Papers and Webcasts

Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT
The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility.
Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

The mobile market is white hot. Building a well-crafted product development plan that addresses market research, strategy, design, and development will provide the greatest chance for success. Each phase of an app's lifecycle is critical to the its overall success and feeds into the next step of the process of product development for the app. This white paper examines the five key phases of mobile app creation, one by one, to understand how they work together to help create a successful mobile app.