Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

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.
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
Espen Harlinn 6-Dec-12 9:50am    
Nice answer :-D
Jochen Arndt 6-Dec-12 9:55am    
Thank you, Espen.
[no name] 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..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900