Click here to Skip to main content
13,190,994 members (56,960 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I have a relatively simple question and would be nice if someone could answer it.

In MFC I have heard there exists concept of owner drawn controls, I was wondering
how they were implemented, how that functionality was achieved? using virtual functions and abstract base classes for example?

I have such brief idea, I was thinking if abstract base class draws a button for example, then it could ask its children to override the function which draws the button (by declaring it as a virtual for instance). This way we would be able to achieve functionality of custom controls right?

Answer on one out of the two paragraphs would be fine for me.
Posted 6-Dec-12 1:01am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The concept of owner drawn controls is not MFC specific. For some Windows controls it is already implemented (buttons, combo boxes, list boxes, and menus). When setting the corresponding owner draw style flag for these controls, the normal drawing is suppressed and a WM_DRAWITEM message is send to the window. MFC supports owner drawing for these controls (CButton, CComboBox, CListBox, CMenu) by implementing an empty virtual DrawItem() member function which is called upon the WM_DRAWITEM message. With owner drawn controls, this function must be implemented in the drived class. See CButton::DrawItem[^] for an example.

Implementing owner draw for other controls can be done by handling the WM_PAINT and WM_ERASEBKGND messages. With MFC, derive a class and add handlers for these messages performing your own drawing without calling the corresponding base class functions.
Espen Harlinn 6-Dec-12 9:50am
Nice answer :-D
Jochen Arndt 6-Dec-12 9:55am
Thank you, Espen.
dmcr_ 8-Dec-12 8:54am
Hi thanks I was also thinking that custom drawing had to be implemented using virtual functions.. thanks i will inquire more on this topic..
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

To elaborate on Jochens response for buttons, since that was your emphasis, the API provides functions for drawing the button, during the handling of DrawItem. You piece them together to get the result you want and/or draw in your own stuff to create a unique look and feel. DrawFrameControl is one function. When you are implementing your own controls you should also detect when the UxTheme.dll is present so that you can use themes. Themes have different methods for drawing the control, like DrawThemeBackground. The reason why both need to be supported is because your end users system may be in classic mode or in Aero mode or some other theme. Supporting themes allows you to model the look and feel of the system with your application so it doesn't stand out like a sore thumb.

In my project Visualizing the Mandelbrot set[^] exists 4 source code files, ImageButton.h/.cpp and BitmapIcon.h/.cpp. The ImageButton source creates an owner draw button which heeds whether themes are loaded or not. The BitmapIcon is a class that implements a DIB as well as converting from DIBs to Icons.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171016.2 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100