⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hexsho~1.cpp

📁 visual c++ 时尚编程百例 全部源代码
💻 CPP
字号:
// HexShowDoc.cpp : implementation of the CHexShowDoc class
//

#include "stdafx.h"
#include "HexShow.h"

#include "HexShowDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc

IMPLEMENT_DYNCREATE(CHexShowDoc, CDocument)

BEGIN_MESSAGE_MAP(CHexShowDoc, CDocument)
	//{{AFX_MSG_MAP(CHexShowDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc construction/destruction

CHexShowDoc::CHexShowDoc()//构造函数
{
	// TODO: add one-time construction code here
	//增加初始化代码
	m_nBytesPerLine=16;//每行显示16个字节
	m_lFileLength=0L;
	m_pHexFile=NULL;

}

CHexShowDoc::~CHexShowDoc()
{//在析构函数中增加代码
    if (m_pHexFile !=NULL)
	{
		m_pHexFile->Close();
		delete m_pHexFile;
		m_pHexFile=NULL;
	}
}

BOOL CHexShowDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc serialization

void CHexShowDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc diagnostics

#ifdef _DEBUG
void CHexShowDoc::AssertValid() const
{
	CDocument::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc commands

BOOL CHexShowDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	//编辑消息处理函数
	if(m_pHexFile!=NULL)
	{
		m_pHexFile->Close();
		delete m_pHexFile;
	}
	m_pHexFile=new CFile(lpszPathName,CFile::modeRead|CFile::typeBinary);
	if (!m_pHexFile)
	{
		AfxMessageBox("文件打开出错!");
		return FALSE;
	}
	m_lFileLength=m_pHexFile->GetLength();
	return TRUE;
}

BOOL CHexShowDoc::ReadFileAndProcess(CString &strLine, long lOffset)
{
	long lPos;
	if (lOffset !=-1)
		lPos=m_pHexFile->Seek(lOffset,CFile::begin);
	else
		lPos=m_pHexFile->GetPosition();
	unsigned char szBuf[16];
	int nRet =m_pHexFile->Read(szBuf,m_nBytesPerLine);
	if (nRet<=0)
		return FALSE;
	CString sTemp;
	CString sChars;
	sTemp.Format(_T("%8.8X: "),lPos);
	strLine=sTemp;
	for (int i=0;i<nRet; i++)
	{
		if (i==0)
			sTemp.Format(_T("%2.2X"),szBuf[i]);
		else if (i%16==0)
			sTemp.Format(_T(" %2.2X"),szBuf[i]);
		else if (i%8==0)
			sTemp.Format(_T("-%2.2X"),szBuf[i]);
		else
			sTemp.Format(_T(" %2.2X"),szBuf[i]);
		if (_istprint(szBuf[i]))
			sChars +=szBuf[i];
		else sChars +=_T('.');
		strLine +=sTemp;
	}//end for
	if (nRet<m_nBytesPerLine)
	{
		CString sPad (_T(' '),2+3*(m_nBytesPerLine-nRet));
		strLine +=sPad;
	}
	strLine +=_T("  ");
	strLine +=sChars;
	return TRUE;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -