HexEdit Control

Environment: Visual C++ 6, Win NT,2000,95,98

Introduction

Since I couldn’t find a hexedit control that fit my needs, I launched this project to develop my own hex edit control. This hex edit control is a complete implementation of a hex edit control that features most of the wanted edit features all the standard edit controls offer. It can be used as a simple single line edit box or as a view for a hexeditor-application (see screenshot).

Features

  • multi- / single-line (depending on the windows-style)
  • automatical scrolling when multi-line
  • horizontal scrolling without scrollbars when single-line
  • show Address (on/off)
  • variable Address-Size
  • show Binary (on/off)
  • cursor navigation (arrows, home, end, page up/down)
  • copy/paste (ctrl+c / ctrl+v)
  • context menue (copy / paste) with strings from resources, when IDS_CONTROL_COPY, IDS_CONTROL_PASTE is defined
  • edit (only hex-data, not binary)
  • selection (only hex-data)
  • special highlighting of a section (not selection)
  • show selection always (only multiline mode)
  • set how many bytes per row or let it calculate (automatic)
  • set colours (every colour can be set)
  • set readonly (enabled/disabled: no colour-difference)
  • Viewclass to use directly as a view (derived from CView)

Limitations

  • Currently it’s only possible to edit in the hex view and not the text/binary view of the data.
  • The size of the data is fix, it’s not possible to insert data. (there is no insert mode, only overwrite mode)

Basic Instructions to use the control/view in your code

1) Use folowing Code in the InitInstance of your application class.

AfxOleInit();
CHexEditBase::RegisterClass(); //when using the CHexEditBase_SC
                               //windows class (not needed for
                               //the examples here)

2) Using the view is pretty simple

2.1) Use the Wizzard to create a class derived from CEditView

2.2) Replace all the “CEditView” with “CHexEditBaseView” in the generated files

2.3) To set the data which has to be displayed you can use one of two methods:

  • SetDirectDataPtr(BYTE* pData, UINT nSize, bool bUpdate)

    If you have a large amount of data and you don’t want the View to buffer it. If the control is not read-only, user input will directly modify the data pointed to by pData. And you have to make sure that this pointer will be valid as long as the control lives or it has been set to display some other data (efficient but less secure methode to set the diplayed data)

  • SetData(const BYTE* pData, UINT nSize, bool bUpdate)

    This methode copies the data given by pData (with size in bytes of nSize). Modifications will be made to the buffer the view maintains. After editing you can call GetData to get the modified data from the control.

2.4) Use something like the folowing code in the OnInitialUpdate methode

// example code for OnInitialUpdate for view class
// (derived from CHexEditBaseView)
GetHexEditCtrl().SetDirectDataPtr(m_pDataPtr, m_nSize, false);
GetHexEditCtrl().SetAddressSize(4, false);
GetHexEditCtrl().SetShowAddress(true, false);
GetHexEditCtrl().SetShowAscii(true, false);
GetHexEditCtrl().SetBytesPerRow(16, true, true);

3) Using the control is easy as well (only easiest way is discribed here)

3.1) Edit the dialogresource and insert edit-controls where you want to see the hex-control later.

3.2) Set multiline-flag when you want to use the control as a multiline (resource editor).

3.3) Use the ClassWizard to connect a member-variable (control (NOT value)) with the previously inserted edit-control. (use class CEdit)

3.4) Use the code editor and replace the CEdit (int the h-file of your dialog/fomview) with CHexEditBase. Don’t forget to include “hexeditbase.h” there.

3.5) Go to the InitDialog (or insert it) and use the m_HexEdit (or how ever your member is called) to set data:

m_HexEdit.SetData((const BYTE*)"dummydata", 9);

3.6) Use other members to set other attributes (set bUbdate (usually last parameter for set-methodes) only with the last SetXXXXX-Methode:

// example how to use CHexEditBase in OnInitDialog

//bUpdate=false
m_cHexEdit.SetShowAddress(false, false);

//bUpdate=false
m_cHexEdit.SetShowAscii(false, false);

//bUpdate=true for the last one
m_cHexEdit.SetData((const BYTE*)"dummydata", 9, true);

For more information about how to use this control check out the sourcode from the demoproject.

Version History

  • version 0.0.0.2
    • first version of this control

  • version 0.0.0.3
    • bug in CreateHighlightingPolygons (when scrolling highlighting out of window on top, sometimes the address got overpainted by some parts of the highlighting section)

  • version 1.0.0.0
    • MakeVisible is now slightly smarter
    • SetFont, GetFont WM_SETFONT, WM_GETFONT works now

  • version 1.1.0.0
    • Fixed the 16Bit scroll range limitation when thumbtracking (see OnVScroll)
    • Modified SetFont to only accept fixed-pitched fonts
    • Replaced some GetSafeHwnd() with ::IsWindow(m_hWnd)
    • Call DestroyWnd from the Destructor, to get rid of the TRACE from “CWnd::~CWnd …”

Downloads

Download demo project – 70 Kb
Download source – 20 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read