Click here to Skip to main content
13,835,332 members
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 9 Dec 2018
Licenced CPOL

MFC HEX Control

, 6 Jan 2019
Rate this:
Please Sign up or sign in to vote.
HEX control for MFC apps


Being good low level wrapper library for Windows API in general, MFC was always lacking a good native controls support.

This always forced people to implement their own common stuff for every day needs.

This HEX control is a tiny attempt to expand standard MFC functionality, because at the time of writing this article, MFC doesn't have native support for such feature.


This HEX control is implemented as CWnd derived class, and can be incorporated as a child window in any place of your existing MDI, SDI, or dialog based MFC application.

Using the Code

The usage of this control is quite simple:

  1. Add four files to your project: HexCtrl.h, HexCtrl.cpp, HexCtrl.rc and HexCtrlRes.h.
  2. Add #include "HexCtrl.h".
  3. Declare CHexCtrl object — HEXControl::CHexCtrl myHex;.
  4. Call myHex.Create function to create control instance.
  5. Call myHex.SetData method to set actual data to display as hex.

Control uses its own namespace — HEXControl. That's why you need this declaration prefix HEXControl::. Of course, you can use using namespace HEXControl; instead.


CHexCtrl class has a set of methods, that you can use to set your data to display as hex, or to customize your hex control appearance. These methods' usage is pretty straightforward and clean from their naming.

void SetData(const PBYTE pData, DWORD_PTR dwCount) const;
void ClearData();
void SetCapacity(DWORD dwCapacity);
void SetFont(const LOGFONT* pLogFontNew) const
void SetFontSize(UINT nSize) const;
void SetColor(COLORREF clrTextHex, COLORREF clrTextAscii, COLORREF clrTextCaption,
              COLORREF clrBk, COLORREF clrBkSelected) const;


The function you use to set a data to display as hex is CHexCtrl::SetData.
It has two parameters:

  1. PBYTE pData — pointer to arbitrary data in memory
  2. DWORD_PTR dwCount — amount of bytes to display

The code below constructs CHexCtrl object and displays first 0x1FF bytes of your app's memory:

CHexCtrl myHex.
myHex.Create(this, CRect(0, 0, 800, 600), 1);

The next example displays std::string's text string as hex:

std::string str = "My string";
myHex.SetData((PBYTE), str.size());

Positioning And Sizing

To properly resize and position CHexCtrl control, you should handle WM_SIZE message in its parent window, in something like that way:

void CParentWnd::OnSize(UINT nType, int cx, int cy)
    myHex.SetWindowPos(this, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOZORDER);


  • To change control's font size - «Ctrl+MouseWheel»
  • To change control's capacity - «Ctrl+Shift+MouseWheel»


If you have any useful feature to suggest, or if you found a bug — please let me know in the comments section below.


This software is available under the "MIT License modified with The Commons Clause".


  • 8th December, 2018 - First version
  • 21st December, 2018 - v1.1 Some tweaks and fixes
  • 23rd December, 2018 - v1.2 C++17 obligation has been alleviated
  • 25th December, 2018 - v1.3
    • Added
      • Bottom info bar
      • Ability to change capacity
    • Fixes
      • Some selection related bugs fixed
  • 28th December, 2018 - v1.4
    • Added
      • Search support
      • Selection offset into info bar
      • About box
    • Fixed
      • Some minor bugs fixed
  • 4th January, 2019 - v1.5 Further improvements and fixes
  • 7th January, 2019 - 1.5.1 Little fix.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Russian Federation Russian Federation
No Biography provided

You may also be interested in...


Comments and Discussions

PraiseNice Pin
Rick York25-Dec-18 19:22
mveRick York25-Dec-18 19:22 
GeneralRe: Nice Pin
Jovibor25-Dec-18 19:39
memberJovibor25-Dec-18 19:39 
PraiseExcellent! Pin
koothkeeper21-Dec-18 11:10
professionalkoothkeeper21-Dec-18 11:10 
GeneralRe: Excellent! Pin
Jovibor21-Dec-18 13:42
memberJovibor21-Dec-18 13:42 
QuestionDialogs Pin
Rick York19-Dec-18 15:02
mveRick York19-Dec-18 15:02 
AnswerRe: Dialogs Pin
Jovibor19-Dec-18 20:11
memberJovibor19-Dec-18 20:11 
GeneralRe: Dialogs Pin
Rick York19-Dec-18 20:43
mveRick York19-Dec-18 20:43 
GeneralRe: Dialogs Pin
Jovibor19-Dec-18 22:08
memberJovibor19-Dec-18 22:08 
QuestionError with std:byte Pin
Member 18025911-Dec-18 16:26
memberMember 18025911-Dec-18 16:26 
AnswerRe: Error with std:byte Pin
Jovibor11-Dec-18 19:05
memberJovibor11-Dec-18 19:05 
GeneralRe: Error with std:byte Pin
Member 18025912-Dec-18 6:18
memberMember 18025912-Dec-18 6:18 
GeneralRe: Error with std:byte Pin
Jovibor12-Dec-18 13:21
memberJovibor12-Dec-18 13:21 
AnswerRe: Error with std:byte Pin
Rick York20-Dec-18 11:25
mveRick York20-Dec-18 11:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190114.1 | Last Updated 6 Jan 2019
Article Copyright 2018 by Jovibor
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid