Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
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 6-Dec-12 2:01am
Rate this: bad
good
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.
  Permalink  
v2
Comments
Espen Harlinn at 6-Dec-12 9:50am
   
Nice answer :-D
Jochen Arndt at 6-Dec-12 9:55am
   
Thank you, Espen.
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 DamithSL 445
1 Maciej Los 278
2 OriginalGriff 229
3 Zoltán Zörgő 190
4 BillWoodruff 160
0 OriginalGriff 7,934
1 DamithSL 6,084
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,174
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
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