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:
- Add four files to your project: HexCtrl.h, HexCtrl.cpp, HexCtrl.rc and HexCtrlRes.h.
CHexCtrl object —
myHex.Create function to create control instance.
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 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
It has two parameters:
PBYTE pData — pointer to arbitrary data in memory
DWORD_PTR dwCount — amount of bytes to display
The code below constructs
CHexCtrl object and displays first
0x1FF bytes of your app's memory:
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";
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
- Bottom info bar
- Ability to change capacity
- Some selection related bugs fixed
- 28th December, 2018 - v1.4
- Search support
- Selection offset into info bar
- About box
- 4th January, 2019 - v1.5 Further improvements and fixes
- 7th January, 2019 - 1.5.1 Little fix.