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


Comments

  • Cooooool !!! Thanx

    Posted by olga145 on 05/30/2005 06:41am

    Cooooool !!! Thanx

    Reply
  • Hex edit control

    Posted by Legacy on 08/22/2003 12:00am

    Originally posted by: Costas Vlachidis

    Thanx for making this piece of code public.I'm using it in image processing!
    Coostas

    Reply
  • Memory leak

    Posted by Legacy on 03/02/2003 12:00am

    Originally posted by: redsword

    Memory leak!
    yes ! it's great. but this class due to memory leak.
    in your code:
    try {
    pControl = new CHexEditBase();
    .....
    }


    if (!pControl->SubClassWindow(hwnd)) {
    .....

    }
    return TRUE; // Free ??????

    Thanks!

    • it is free here: PostNcDestroy & m_bSelfCleanup

      Posted by jauming on 09/03/2007 12:03am

      void CHexEditBase::PostNcDestroy() 
      {
      	if(m_bSelfCleanup) {
      		m_bSelfCleanup = false;
      		delete this;
      	}
      }

      Reply
    Reply
  • HexEdit

    Posted by Legacy on 09/26/2002 12:00am

    Originally posted by: SpazyMoto

    Could you give me a starting direction to be able to paint the hex numbers based on there value? This could help to pick out table in code. Example you have the hex values of
    ff fe fd fc fb fa and you paint the rectangle or text to be diffrent shades of red ff would be bright red and fa would be a lighter shade of red.

    Reply
  • Nice work!

    Posted by Legacy on 05/05/2002 12:00am

    Originally posted by: SuNr

    Nice control! Easy, fast...useful!

    Reply
  • Do you have implementation to chage the data size?

    Posted by Legacy on 04/24/2002 12:00am

    Originally posted by: Sadru

    Do you have implementation to chage the data size?

    Reply
  • Well done control

    Posted by Legacy on 03/06/2002 12:00am

    Originally posted by: Wolfgang Ibl

    This is really a cool control with a lot of features. I have only 2 questions:

    Is it possible to display a (disabled) vertical scroll bar, even if the control's content does not exceed it's height (like it can be done with the CEdit) - the reason is, that if you fit it into a dialog, you have an ugly stripe on the right without the bar...

    Is it possible to have the ascii column editable?

    Thanks
    Wolfgang

    Reply
  • Thank you

    Posted by Legacy on 11/11/2001 12:00am

    Originally posted by: HsiaLin

    Thank you for this fine vc++ source, but if you will port it to Builder 5.0 i`ll give you a high-five :-)

    Reply
  • Wonderful control....more?

    Posted by Legacy on 11/07/2001 12:00am

    Originally posted by: Mike Lashbrook

    I love this view control and use it frequently. The only question that I have is.... Will it ever support an insert mode? That is really the only thing missing that would make it a perfect binary file editor.

    Thanks for the control,
    Mike

    Reply
  • Great work but.............

    Posted by Legacy on 10/18/2001 12:00am

    Originally posted by: Rajesh

    Hi Christian,

    Great work!!! I have an application which requires large file to be edited..but when i open a these large files for obvious resons the memory usage goes up. Is there a way by which I read only those many bytes from the file needed to be displayed depending on the current size of the window. Ofcourse there are alot of issues like how would you handle the scrolling...Do, you have any suggestions how this can be done, or if you already have a fix that will be great;-)

    thanks and Regards
    Rajesh

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds