Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / MFC

The Ultimate Toolbox Home Page

Rate me:
Please Sign up or sign in to vote.
4.97/5 (141 votes)
25 Aug 2007CPOL13 min read 3.2M   91.4K   476  
The Ultimate Toolbox is now Open Source
// HexView.cpp : implementation file
//

#include "stdafx.h"
#include "Communicator.h"
#include "CommunicatorDoc.h"
#include "HexView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHexView

IMPLEMENT_DYNCREATE(CHexView, CEditView)

CHexView::CHexView()
	{
	}

CHexView::~CHexView()
	{
	}


BEGIN_MESSAGE_MAP(CHexView, CEditView)
	//{{AFX_MSG_MAP(CHexView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHexView diagnostics

#ifdef _DEBUG
void CHexView::AssertValid() const
	{
	CEditView::AssertValid();
	}

void CHexView::Dump(CDumpContext& dc) const
	{
	CEditView::Dump(dc);
	}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHexView message handlers

void CHexView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
	{
	UNREFERENCED_PARAMETER(pSender);
	CCommunicatorDoc::CHintData* pHintData = (CCommunicatorDoc::CHintData*)pHint;
	CString sText;

	if (lHint == CCommunicatorDoc::NotifyAddData)
		{
		sText.Format(_T("%i. Data length = 0x%X (%i)\r\n"), 
			pHintData->m_hClient, pHintData->m_nDataLength, pHintData->m_nDataLength);
		AddText(sText);

		// Show the received data
		if (pHintData->m_pData == NULL)
			{
			sText = _T("\t<Unknown Data>\r\n");
			AddText(sText);
			return;
			}

		BYTE* pData = pHintData->m_pData;
		UINT nMaxIndex = pHintData->m_nDataLength;
		UINT nIndex = 0;
		CString sRaw;
		CString sHex;
		CString sHexTemp;
		while (nIndex < nMaxIndex)
			{
			// Split in two parts (of 8 bytes) seperated by a space
			sRaw.Empty();
			sHex.Empty();

			// ... Add offset
			sRaw.Format(_T("%4.4X  "), nIndex);

			for (int nLoop = 0; nLoop < 2; nLoop++)
				{
				// Calculate 8 bytes
				UINT nPartIndex = 0;
				while (nPartIndex < 8  && nIndex < nMaxIndex)
					{
					if (isprint(pData[nIndex]))
						sRaw += (char)pData[nIndex];
					else
						sRaw += _T('.');
					sHexTemp.Format(_T("%2.2X "), pData[nIndex]);
					sHex += sHexTemp;
					nPartIndex++;
					nIndex++;
					}

				sRaw += _T(' ');
				sHex += _T(' ');
				}

			// Show the text
			sText.Format(_T("%-24.24s|%s\r\n"), sRaw, sHex);
			AddText(sText);
			}
		AddText(_T("\r\n"));
		}
	}

void CHexView::AddText(LPCTSTR pszText) 
	{
	LONG nWindowTextLength;
	nWindowTextLength = GetEditCtrl().GetWindowTextLength();
	GetEditCtrl().SetSel(nWindowTextLength, nWindowTextLength);
	GetEditCtrl().ReplaceSel(pszText);
	}


BOOL CHexView::PreCreateWindow(CREATESTRUCT& cs) 
	{
	BOOL bRet = CEditView::PreCreateWindow(cs);
	cs.style |= ES_READONLY;	
	return bRet;
	}

void CHexView::OnInitialUpdate() 
	{
	CEditView::OnInitialUpdate();

	// Create a fixed size font
	m_font.CreateFont(	0,						// nHeight
						0,						// nWidth
						0,						// nEscapement
						0,						// nOrientation
						FW_DONTCARE,			// nWeight
						FALSE,					// bItalic
						FALSE,					// bUnderline
						0,						// cStrikeOut
						DEFAULT_CHARSET,		// nCharSet
						OUT_DEFAULT_PRECIS,		// nOutPrecision
						CLIP_DEFAULT_PRECIS,	// nClipPrecision
						DEFAULT_QUALITY,		// nQuality
						FIXED_PITCH,			// nPitchAndFamily
						NULL);					// lpszFacename 
  
	GetEditCtrl().SetFont(&m_font, FALSE);
	}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.

Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
This is a Organisation

476 members

Comments and Discussions