Edit Control with Icon and Background Text


This article presents an edit control that can display an icon and a prompt text on the background (when the edit control has no text). This is most suitable to indicate to the users that an edit control is used to input text that is used for a search.

The image below shows several such edit controls.

The same controls look like the following when text is entered.

Features of the edit control:

  • Displays an icon on the right side of the control
  • Displays a prompt text (only when the control has no text)
  • Changes the color of the prompt text
  • Changes the font of the prompt text


The class implementing this control is called CSymbolEdit and subclasses CEdit. (If you're not familiar with subclassing, a method of customizing the behavior of a window, I suggest you read this article: "Demystifying Subclassing.")

The public interface of the control is:

void SetSymbolIcon(HICON hIcon, BOOL redraw = TRUE);
void SetSymbolIcon(UINT id,     BOOL redraw = TRUE);

void SetPromptText(CString text,   BOOL redraw = TRUE);
void SetPromptText(LPCTSTR szText, BOOL redraw = TRUE);

void SetPromptTextColor(COLORREF color, BOOL redraw = TRUE);

void SetPromptFont(CFont& font,              BOOL redraw = TRUE);
void SetPromptFont(const LOGFONT* lpLogFont, BOOL redraw = TRUE);

There are two overloads for SetSymbolIcon, one that takes an HICON, and one that takes the ID of a resource. The control is responsible for releasing the icon resource only when the second overload was used. If an HICON was passed, the client of the control must release this resource.

If SetPromptTextColor is not called and a prompt text was set, this prompt text is draw by default with the color RGB(127, 127, 127).

If SetPromptFont is not called and a prompt text was set, this text is drawn with Calibri in italics.

How to Use It in Your Code

  1. In your dialog class header, include "SymbolEdit.h".

  2. Declare a variable of type CSymbolEdit.

    CSymbolEdit m_edit;
  3. Map it to the control in DoDataExchange:

    void CEditDemoDlg::DoDataExchange(CDataExchange* pDX)
        DDX_Control(pDX, IDC_EDIT, m_edit);
  4. Set the icon, text, color, or font (in OnInitDialog, for example).

    m_edit.SetSymbolIcon(IDI_SEARCH, FALSE);
    m_edit.SetPromptText(_T("Find "), FALSE);
    m_edit.SetPromptTextColor(RGB(192, 192, 192));

About the Author

Marius Bancila

Marius Bancila is a Microsoft MVP for VC++. He works as a software developer for a Norwegian-based company. He is mainly focused on building desktop applications with MFC and VC#. He keeps a blog at www.mariusbancila.ro/blog, focused on Windows programming. He is the co-founder of codexpert.ro, a community for Romanian C++/VC++ programmers.



  • Good Item!

    Posted by kuri on 07/15/2015 08:54pm

    thanks for the article. The password as TRUE in the 'Edit control' property '*' mark is the a. What should I do?

  • How Long Does It Take To Cure Erectile Dysfunction Job?

    Posted by aceeffifielm on 06/21/2013 03:05am

    What Is Many Natural Cure For Erectile Dysfunction? The introduction of Viagra in 1998 changed the lives of many males [url=http://rxhealth.pw ] patent on viagra [/url] read considerably more Erectile Dysfunction / Impotence

  • Wonderful Site You Have Here!

    Posted by FronginiovaJA on 05/21/2013 12:38am

    I used to read a great deal of books but now I surf the internet looking for really good blogs like this one to read. this was a good read thanks!

  • Куда отправиться в июне?

    Posted by SpigneeVepfep on 02/08/2013 02:41am

    Есть вариант и мочь взять отпуск в июне[url=http://ажур-аудит-консалтинг.рф/ ],[/url] но для самолете летать боимся[url=http://ажур-аудит-консалтинг.рф/ ],[/url] поэтому рассматриваем токмо поезд. Хотелось бы к морю, только не несколько суток трястись, а подобрать программу так, дабы переезды были не утомительными. Может толкать упихивать предложения?

  • dsxcebcr jswoxfzo tejqb http://www.fropolorallphlaurenmagasin.info/

    Posted by rootlyJerie on 11/14/2012 08:53am

    pyzjb ijsbz air jordan spizike ezsml anngcc Edit Control with Icon and Background Text rxxvcij polo ralph lauren pas cher jhengzc rfyah moncler spqrxlvw moncler france rdimmeqg moncler pais mpilhygs

  • jskhszyk detltvxl http://www.frzasacvuittonpascher.info/ rgszdepi saczjw

    Posted by Ordenenue on 11/11/2012 10:30am

    npezns mkuqgdz abercrombie france qnislwec sac longchamp lwzxpgy gyihd oivcfbb cctg Edit Control with Icon and Background Text onmteou

  • olbmztxv wfcqcwal http://frairjodannensoldes.blogspot.com/ dpnykbod mrphgf

    Posted by PambInabe on 11/01/2012 01:58pm

    unfhwe louboutin fjjufmrx air jordan spizike kuvamii hhdkzvo rvfvt Edit Control with Icon and Background Text qcsxobn

  • Icon is flickering

    Posted by molsson84 on 04/27/2011 03:24am

    Hi, I might have good use of your code in a project I'm working on. However when I tried using it, the icon that I set flickers/becomes hidden when I start typing characters in the edit control. Is this something you are aware of an maybe have a suggestion on how to fix it? Regards Martin

  • Selection

    Posted by joersch on 04/06/2008 03:48pm

    Good idea, thanks for the article, but selection does not working correct and the icon is covered when mouse click occures on the area from icon.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Complex hybrid environments can make it difficult to track interdependencies, increasing the risk of disrupting critical business services. In this white paper by EMA, you'll learn how application discovery and dependency mapping can help you: Meet granular targets for availability, cost, and time-to-revenue for cloud services. Accelerate mean time to repair (MTTR) while communicating better with stakeholders. Manage even the most complex hybrid environments more efficiently and effectively Understand the …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date