CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Controls >> Other Controls


HexEdit Control
Rating:

Christian Kuendig (view profile)
June 11, 2001


(continued)




(
Full Size Image)

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

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Great work! a little extension.. - Radu (01/11/2006)
Cooooool !!! Thanx - olga145 (05/30/2005)
Hex edit control - Legacy CodeGuru (08/22/2003)
Memory leak - Legacy CodeGuru (03/02/2003)
HexEdit - Legacy CodeGuru (09/26/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES