Edit Control with Icon and Background Text
2012-04-19 22:42
423 查看
Introduction
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.Demystifying Subclassing.")
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
Implementation
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: "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
In your dialog class header, include "SymbolEdit.h".Declare a variable of type CSymbolEdit.
CSymbolEdit m_edit;
Map it to the control in DoDataExchange:
void CEditDemoDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_EDIT, m_edit); }
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 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.
In July 2007 together with two other Romanian MVPs he created codexpert.ro, a community for Romanian C++/VC++ programmers.
Downloads
EditDemo_2005.zip -
demo application in Visual Studio 2005
EditDemo_2008.zip -
demo application in Visual Studio 2008
SymbolEdit.zip - source code
for the edit control
转帖:http://blog.csdn.net/tieshashi/article/details/2306992
相关文章推荐
- Edit Control with Icon and Background Text
- Edit Control with Icon and Background Text
- Text Edit control - Usage and Demo
- edit view with icon drawable and Tag usage
- Android EditText with custom font and clear button
- Change the Foreground and Background Colour of Staic Text Control in VC++
- Get installed Applications with Name, Package Name, Version and Icon
- DataGrid Control, EditText Control
- [转载]TDateEditPicker: A Nullable & Bindingable Date Edit and Pick Control
- 翻译:Panda3D Manual/V. Programming with Panda/J. Text and Image Rendering
- selector of text and background should using different attributes
- Get notification texts colors and background
- Enhanced System Security with the LabVIEW Datalogging and Supervisory Control Module
- Data-Intensive Text Processing with MapReduce 第三章(2)——PAIRS AND STRIPES
- LinuxCNC branch for working with non-traditional machine control (ie: 3D printers) and running HAL
- TDateEditPicker: A Nullable & Bindingable Date Edit and Pick Control
- CONTROL A COMPUTER WITH YOUR BRAIN, AND LOOK GOOD DOING IT
- ui page control and scrollview with indicator at bottom
- Android带有删除按钮的EditText:EditTextWithDeleteButton
- statistical-machine-learning-for-text-classification-with-scikitlearn-and-nltk