Visual Studio .NET 2002Visual C++ 7.1Visual Studio 6Visual C++ 7.0Visual Studio .NET 2003Windows 2003Windows 2000Visual C++ 6.0Windows XPMFCIntermediateDevVisual StudioWindowsC++
A CHexEdit Control - For HEX and DEC number input






4.40/5 (12 votes)
Jan 25, 2005
2 min read

108136

4289
An article on how to write an Edit control used for HEX and DEC number input.
Introduction
Recently, I was working on a diagnostic software which requires a Edit Control used for HEX/DEC numbers input. The CEdit
that comes with MFC is not so good for this job, as it can not prevent the user from typing an invalid HEX number.
Therefore, I created the CHexEdit
control derived from CEdit
for HEX/DEC numbers input, it is very easy to use.
Features of the CHexEdit control
These are the features of the CHexEdit
class:
- Automatically validate user input and prevent invalid HEX/DEC number format input.
- Detect HEX/DEC numbers automatically and highlight the difference by coloring the text.
- Ability to convert HEX to DEC or DEC to HEX numbers by using the menu that comes with it.
- Redefines Paste, Cut, Delete, Copy operations to avoid invalid input.
- Easy to manage input values through its
GetValue()
/SetValue()
functions. - Easy to implement for MFC developers.
Using the CHexEdit Control
To use CHexEdit
, you must go through the following steps:
- In your application, add a control variable for you Edit Control.
- Change
CEdit
toCHexEdit
in your corresponding .h files, like this:public: CHexEdit m_cEdit;
- Optionally, you can define your own HEX/DEC number background/foreground colors when initializing your application.
m_cEdit.SetHexColor(RGB(255,255,255),RGB(0,0,0)); //black text color and white text background for hex number m_cEdit.SetDecColor(RGB(0,0,0),RGB(255,255,255)); //white text color and black text background for dec number
- Optionally, you can define its default number through
SetValue(unsigned int _value,bool _bHex)
.m_cEdit.SetValue(100,true); //set value to 100 using HEX format to express
- You can get the input value through
GetValue()
.
Technical background
The CHexEdit
is derived form CEdit
, and there are three modifications, including:
- Validate user input. This is done by overriding
CEdit::PreTranslateMessage(MSG* pMsg)
. When a user strikes a key, it determines whether the key is valid, and makes the input letters to upper case making the number look a bit nicer. - Redefine
CEdit
's default popup menu. Since a user can not all the time do copy, cut, paste, etc., it is good to check the clipboard buffer and make these menu items grayed when the data is not suitable. - Get input number type (HEX/DEC). By intercepting
EN_UPDATE
message, it checks whether the input number is HEX or DEC, and colors the background and foreground automatically.void CHexEdit::OnEnUpdate() { CStringtext; this->GetWindowText(text); //Hex or Dec? this->m_bHex=false; if(text.Find("0x")== 0) this->m_bHex=true; for(int i=0; i < text.GetLength(); i++) { char c=text.GetAt(i); if(c >= 'A' && c <= 'F') this->m_bHex=true; } this->RedrawWindow(); }
History
- 21 Jan 2005 - v1.0 - First release.