Click here to Skip to main content
11,410,379 members (60,699 online)
Click here to Skip to main content

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

, 16 Jun 2004 Public Domain
Rate this:
Please Sign up or sign in to vote.
Adding controls to a CEdit, an edit box with an icon

Sample Image - editwithicon.gif


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.


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*


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


About the Author

Johan Rosengren
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 PinmemberAPache_hema_317-Mar-14 14:05 
AnswerRe: source code for Buddy Button "404 - File or directory not found" in PinmemberFlaviu214-Jan-15 23:07 
QuestionScrolling ? PinmemberMattthe cooldude9-Mar-05 14:58 
AnswerRe: Scrolling ? PinmemberJohan Rosengren10-Mar-05 6:31 
GeneralProblem with mouse clicks PinmemberDavid Pritchard20-Oct-04 0:01 
There's a problem with this control when the user clicks on the icon. The click goes straight through to the edit and is interpreted as a click in the edit. The edit then gets horribly confused and starts overwriting any existing text in the control because it thinks the insertion point has changed.

To fix this, we need to block mouse messages, with something like this:

// DP 20/10/2004 Block mouse messages if clicked on icon
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnRButtonDblClk(UINT nFlags, CPoint point);

// DP 20/10/2004 Block mouse messages if clicked on icon

// DP 20/10/2004 Block mouse messages if clicked on icon
void CIconEdit::OnLButtonDown(UINT nFlags, CPoint point)
if (!IsClickInIcon(point))
CEdit::OnLButtonDown(nFlags, point);

void CIconEdit::OnRButtonDown(UINT nFlags, CPoint point)
if (!IsClickInIcon(point))
CEdit::OnRButtonDown(nFlags, point);

void CIconEdit::OnRButtonUp(UINT nFlags, CPoint point)
if (!IsClickInIcon(point))
CEdit::OnRButtonUp(nFlags, point);

void CIconEdit::OnLButtonUp(UINT nFlags, CPoint point)
if (!IsClickInIcon(point))
CEdit::OnLButtonUp(nFlags, point);

/*afx_msg*/ void CIconEdit::OnLButtonDblClk(UINT nFlags,CPoint point)
if (!IsClickInIcon(point))
CEdit::OnLButtonDblClk(nFlags, point);

/*afx_msg*/ void CIconEdit::OnRButtonDblClk(UINT nFlags,CPoint point)
if (!IsClickInIcon(point))
CEdit::OnRButtonDblClk(nFlags, point);

// DP: 20/10/2004 Function to determine if click is in icon
BOOL CIconEdit::IsClickInIcon(IN CPoint point)

if (m_icon.m_hWnd)
CRect rectIcon;

// Get icon rect

// If clicked on icon, ignore
bIsInIcon = rectIcon.PtInRect(point);

return bIsInIcon;

Now this is a bit unsatisfactory, and for me it pretty much tips the scales in favour of the GipsySoft solution mentioned in an earlier post. I've actually used it in my application for inserting a "buddy button" in an edit and it works fine. However, it's really a code for a "buddy window" of any type. It would also solve my icon-in-a-password-edit problem.

I'll try to knock together a control with it if I have time.
GeneralFalse alarm? Plus non-client implementation of IconEdit PinmemberDavid Pritchard20-Oct-04 4:47 
GeneralRe: False alarm? Plus non-client implementation of IconEdit PinmemberJohan Rosengren20-Oct-04 7:17 
GeneralRe: False alarm? Plus non-client implementation of IconEdit PinmemberDavid Pritchard20-Oct-04 10:38 
GeneralRe: False alarm? Plus non-client implementation of IconEdit PinmemberJohan Rosengren21-Oct-04 9:17 
GeneralControls-in-controls: An edit box with an icon PinmemberLarsson23-Sep-04 23:49 
GeneralRe: Controls-in-controls: An edit box with an icon PinmemberJohan Rosengren24-Sep-04 0:20 
Generalfor an in depth discussion see also... PinmemberAmun-Ra19-Jun-04 4:13 
GeneralRe: for an in depth discussion see also... PinmemberJohan Rosengren19-Jun-04 4:19 
GeneralA tiny, tiny painting correction PinmemberDavid Pritchard10-Jun-04 11:12 
GeneralRe: A tiny, tiny painting correction PinmemberJohan Rosengren10-Jun-04 11:16 
GeneralBug! SetIcon(HICON) PinmemberDavid Pritchard27-May-04 4:15 
GeneralRe: Bug! SetIcon(HICON) PinmemberJohan Rosengren27-May-04 4:55 
GeneralRe: Bug! SetIcon(HICON) PinmemberDavid Pritchard27-May-04 11:44 
GeneralRe: Bug! SetIcon(HICON) PinmemberJohan Rosengren27-May-04 20:09 
GeneralRe: Bug! SetIcon(HICON) PinmemberDavid Pritchard28-May-04 13:02 
GeneralRe: Bug! SetIcon(HICON) PinmemberJohan Rosengren28-May-04 22:38 
GeneralRe: Bug! SetIcon(HICON) PinmemberDavid Pritchard29-May-04 5:01 
GeneralRe: Bug! SetIcon(HICON) PinmemberJohan Rosengren29-May-04 5:27 
GeneralRe: Some comments PinmemberJohan Rosengren10-Mar-04 9:23 
Generalthe ES_MULTILINE issue Pinmember.dan.g.10-Mar-04 0:47 
GeneralRe: the ES_MULTILINE issue PinmemberJohan Rosengren10-Mar-04 1:02 
GeneralRe: the ES_MULTILINE issue Pinmember.dan.g.10-Mar-04 1:35 
GeneralRe: the ES_MULTILINE issue PinmemberJohan Rosengren10-Mar-04 9:28 
Generalinteresting Pinmember.dan.g.10-Mar-04 0:43 
GeneralRe: interesting Pinmembergrigri18-Mar-04 12:32 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.5 | Last Updated 17 Jun 2004
Article Copyright 2004 by Johan Rosengren
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid