Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Controls-in-controls: An edit box with an icon

Rate me:
Please Sign up or sign in to vote.
4.50/5 (12 votes)
16 Jun 2004Public Domain3 min read 139.4K   2.9K   55   30
Adding controls to a CEdit, an edit box with an icon

Sample Image - editwithicon.gif

Introduction

While adding controls to other controls might be a commonly desired task, and edit box with an icon is perhaps not the most exciting or useful class. It presents a few enjoyable challenges, however, and usefulness is certainly not the only criteria for diving into control subclassing in MFC.

CIconEdit is a small CEdit-derived class with an attached small icon, where the multi line edit box edit rectangle is exploited, instead of the normal handling of non-client areas.

Using the code

Include the cpp- and h-file to the project. CIconEdit can be instantiated either dynamically, by a call to Create, or from a dialog template. In the latter case, a control variable can be created and bound to the control with the class wizard, or SubclassDlgItem can be used. Call SetIcon with either a HICON or a resource id to set the icon to display.

CIconEdit uses the edit rectangle of multi line edit boxes. Basically, it modifies the edit rectangle to add a left margin wide enough to accommodate a small icon. An instance of CIconWnd - a CStatic-derived class - is used to paint the icon. Icons can be set by calling CIconEdit::SetIcon with either a HICON handle to the desired icon, or a UINT resource id. If a resource id is used, the icon is destroyed by CIconEdit, otherwise the caller will have to destroy it.

The class overrides PreSubclassWindow. This call checks the edit box style. If ES_MULTILINE is not set, SetRect will not work, so the function ASSERTs. Sadly, it is not possible to add this style after the control is created - and it is too early to destroy and recreate the it. Then the edit rectangle is established. The width of a small icon is retrieved by a call to GetSystemMetrics, and the current edit rectangle is fetched, updated and set back to the control.

As soon as an icon is set by a call to either of the two SetIcon:s, the icon window is created.

CIconWnd is a very simple CStatic-derived class, handling WM_PAINT and there drawing the icon with a call to ::DrawIconEx. The icon will be scaled to the system small icon size if necessary.

Points of interest

Instantiating this control, doing a few common operations on CEdits, displayed an understandable but annoying quirk - the edit rectangle is reset. As the rectangle is absolute for a CEdit (RTF-controls can use an offset), WM_SIZE will have to be handled anyway. More surprising was that WM_SETFONT also killed the edit rectangle. For this reason, CIconEdit handles WM_SIZE and WM_SETFONT. As SetFont is not virtual, the message itself will have to be handled.

In this case, using a separate control for the icon might seem a bit byzantine, why not just draw it in the non-client area paint handling? Well, now I got myself a small-icon control if I should ever need one (which I doubt), and the edit control and icon is thoroughly separated codewise.

But the more exciting implication is the possibility to use the edit rectangle to put other controls and/or visuals in the edit box, buddy-buttons, line-numbers etc. And as the editing rectangle is not only used by the plain vanilla edit box - the RTF-control also has one - there is the possibility to add rulers etc. this way.

History

15/6 2004

In an explosion of activity, this article is finally updated thanks to the feedback from David Pritchard . I'm getting the system background color using GetSysColor(COLOR_WINDOW) when clearing the background behind the icon, and SetIcon with the HICON parameter will now also create the icon control... *blushing with embarrassment*

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) Abstrakt Mekanik AB
Sweden Sweden
45 years old, married, three kids.

Started with computers more than 20 years ago on a CBM-64.

Read Theoretical Philosophy at the University of Lund.

Working as a C++ consultant developer.

Science-fiction freak. Enjoy vintage punkrock.

Comments and Discussions

 
Questionsource code for Buddy Button "404 - File or directory not found" in http://www.gipsysoft.com/articles/BuddyButton Pin
ApacheHema17-Mar-14 13:05
ApacheHema17-Mar-14 13:05 
AnswerRe: source code for Buddy Button "404 - File or directory not found" in http://www.gipsysoft.com/articles/BuddyButton Pin
_Flaviu14-Jan-15 22:07
_Flaviu14-Jan-15 22:07 
QuestionScrolling ? Pin
Mattthe cooldude9-Mar-05 13:58
Mattthe cooldude9-Mar-05 13:58 
AnswerRe: Scrolling ? Pin
Johan Rosengren10-Mar-05 5:31
Johan Rosengren10-Mar-05 5:31 
GeneralProblem with mouse clicks Pin
David Pritchard19-Oct-04 23:01
David Pritchard19-Oct-04 23:01 
GeneralFalse alarm? Plus non-client implementation of IconEdit Pin
David Pritchard20-Oct-04 3:47
David Pritchard20-Oct-04 3:47 
GeneralRe: False alarm? Plus non-client implementation of IconEdit Pin
Johan Rosengren20-Oct-04 6:17
Johan Rosengren20-Oct-04 6:17 
GeneralRe: False alarm? Plus non-client implementation of IconEdit Pin
David Pritchard20-Oct-04 9:38
David Pritchard20-Oct-04 9:38 
GeneralRe: False alarm? Plus non-client implementation of IconEdit Pin
Johan Rosengren21-Oct-04 8:17
Johan Rosengren21-Oct-04 8:17 
GeneralControls-in-controls: An edit box with an icon Pin
Larsson23-Sep-04 22:49
Larsson23-Sep-04 22:49 
GeneralRe: Controls-in-controls: An edit box with an icon Pin
Johan Rosengren23-Sep-04 23:20
Johan Rosengren23-Sep-04 23:20 
Generalfor an in depth discussion see also... Pin
RuskieF19-Jun-04 3:13
RuskieF19-Jun-04 3:13 
GeneralRe: for an in depth discussion see also... Pin
Johan Rosengren19-Jun-04 3:19
Johan Rosengren19-Jun-04 3:19 
GeneralA tiny, tiny painting correction Pin
David Pritchard10-Jun-04 10:12
David Pritchard10-Jun-04 10:12 
GeneralRe: A tiny, tiny painting correction Pin
Johan Rosengren10-Jun-04 10:16
Johan Rosengren10-Jun-04 10:16 
GeneralBug! SetIcon(HICON) Pin
David Pritchard27-May-04 3:15
David Pritchard27-May-04 3:15 
GeneralRe: Bug! SetIcon(HICON) Pin
Johan Rosengren27-May-04 3:55
Johan Rosengren27-May-04 3:55 
GeneralRe: Bug! SetIcon(HICON) Pin
David Pritchard27-May-04 10:44
David Pritchard27-May-04 10:44 
GeneralRe: Bug! SetIcon(HICON) Pin
Johan Rosengren27-May-04 19:09
Johan Rosengren27-May-04 19:09 
GeneralRe: Bug! SetIcon(HICON) Pin
David Pritchard28-May-04 12:02
David Pritchard28-May-04 12:02 
GeneralRe: Bug! SetIcon(HICON) Pin
Johan Rosengren28-May-04 21:38
Johan Rosengren28-May-04 21:38 
GeneralRe: Bug! SetIcon(HICON) Pin
David Pritchard29-May-04 4:01
David Pritchard29-May-04 4:01 
GeneralRe: Bug! SetIcon(HICON) Pin
Johan Rosengren29-May-04 4:27
Johan Rosengren29-May-04 4:27 
GeneralRe: Some comments Pin
Johan Rosengren10-Mar-04 8:23
Johan Rosengren10-Mar-04 8:23 
If I understand you correctly, I'm really beginning to freak outSmile | :)

Some kind of simple pluggable buddies-to-edits framework... I feel the synapses starting to fire up...
Generalthe ES_MULTILINE issue Pin
.dan.g.9-Mar-04 23:47
professional.dan.g.9-Mar-04 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.