📄
字号:
如何用vc++60编写查看二进制文件程序
雷霆工作室 韩燕
---- 在计算机应用中,经常需要查看二进制文件的内容。目前,在各种vc++书籍中介绍查看文本文件的文章很多,但鲜有介绍查看二进制文件的文章。本文从功能设计、方案设计、编程实现以及技术要点等方面来简单介绍。
---- 1 功能设计
---- 显示界面见图1(略),将窗口客户区划分为三部分,左边列用于以16进制方式显示文件内容的相应位置,中间列用于以16进制方式显示文件内容,右边列用于显示文件内容对应的ascii码的内容。为简化程序设计,没有打印功能。
---- 2 方案设计
---- 采用mfc的sdi(单文档界面)。由于在一屏内一般不可能显示整个文件的内容,所以选择视类的基类为cscrollview。二进制文件的读出与处理在文档类中完成,文件的显示与滚动由视类来实现。
---- 3 编程实现
---- 3.1 使用mfc appwizard向导产生一应用框架在vc++的“file”菜单中,单击“new”,弹出一new对话框。在“projects”页中选择“mfc appwizard [exe]”,在“project name”编辑框中填入“hexshow”(见图2(略)),按“ok”按钮,退出new对话框。在“mfc appwizard step 1”对话框中选择单选钮“single document”,按“next>”按钮,进入“mfc appwizard step 2 of 6”对话框,保持缺省选择,按“next>”按钮,进入“mfc appwizard step 3 of 6”对话框,保持缺省选择,按“next>”按钮,进入“mfc appwizard step 4 of 6”对话框,取消“printing and print preview”选项(见图3(略)),按“next>”按钮,进入“mfc appwizard step 5 of 6”对话框,保持缺省设置,继续按“next>”按钮,进入“mfc appwizard step 6 of 6”对话框,在“base class”组合框中选择cscrollview(见图4(略)), 按“finish”按钮即可完成应用框架的定制。
---- 3.2 在文档类chexshowdoc中增加文件的读出及处理工作
---- 3.2.1 定义文档的成员变量,做好初始化及清理工作
---- 打开hexshowdoc.h文件,增加2个公共变量:
cfile* m_phexfile;
long m_lfilelength;
int m_nbytesperline;
//每行显示多少个byte然后,打开hexshowdoc.cpp文件,
---- 在类的构造函数中增加下列初始化代码:
m_phexfile = null;
m_lfilelength = 0l;
m_nbytesperline=16;
//每行显示16个byte在类的析构函数中增加下列清理代码:
if (m_phexfile != null)
{
m_phexfile- >close();
delete m_phexfile;
m_phexfile = null;
}
---- 3.2.2 在onopendocument()中打开文档
---- 首先利用classwizard重载消息成员函数onopendocument()。
---- 在该成员函数的代码添加处增加下列代码:
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();
---- 3.2.3 增加用于读文件及进行输出格式化处理的成员函数为chexshowdoc类增加成员函数如下:
bool chexshowdoc::readfileandprocess(cstring &strline, long loffset)
{
long lpos;
if (loffset != -1l)
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.8lx : "), 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;
}
if (nret < m_nbytesperline)
{
cstring spad(_t(' '),
2+3*(m_nbytesperline-nret));
strline += spad;
}
strline += _t(" ");
strline += schars;
return true;
}
---- 3.3 在视中添加显示文件内容以及处理滚动操作
---- 3.3.1 变量定义以及初始化
---- a) 打开hexshowview.h文件,增加成员变量如下:
cfont* m_pfont; //用于为显示文件内容选择字体
---- b) 在视的构造函数中为文件内容显示选择合适的字体
//选择一种名为“fixedsys”的字体,该字体使得字符的排列整齐
logfont m_logfont;
memset(&m_logfont, 0, sizeof(m_logfont));
_tcscpy(m_logfont.lffacename, _t("fixedsys"));
cclientdc dc(null);
m_logfont.lfheight = ::muldiv
(120, dc.getdevicecaps(logpixelsy), 720);
m_logfont.lfpitchandfamily = fixed_pitch;
m_pfont = new cfont;
m_pfont- >createfontindirect(&m_logfont);
c) 将chexshowview::oninitialupdate()中的代码修改为:
cscrollview::oninitialupdate();
chexshowdoc* pdoc = getdocument();
assert_valid(pdoc);
csize sizetotal(0, pdoc- >m_lfilelength);
setscrollsizes(mm_text, sizetotal);
---- c) 在视的析构函数中完成对字体对象的删除,增加代码如下:
if (m_pfont != null)
delete m_pfont;
---- 3.3.2 在视中的ondraw()中添加如下代码
chexshowdoc* pdoc = getdocument();
assert_valid(pdoc);
cfont* poldfont;
cstring sline;//用于显示的文本行
csize scrolledsize;//窗口的客户区的范围
int istartline;//当前屏第一行显示的行的索引号
int nheight;//输出文本行的高度
crect scrollrect;
//获得该屏滚动条的位置
cpoint scrolledpos = getscrollposition();
crect rectclient;
getclientrect(&rectclient);
// 求出每行的高度(单位:象素数)
textmetric tm; //tm用于存储库存字体的参数;
pdc- >gettextmetrics(&tm); nheight = tm.tmheight; poldfont = pdc- >selectobject(m_pfont); // 根据滚动,求出开始行 scrolledsize = csize(rectclient.width(), rectclient.height()); scrollrect = crect(rectclient.left, scrolledpos.y, rectclient.right, scrolledsize.cy + scrolledpos.y); istartline = scrolledpos.y/16; // make sure we are drawing where we should scrollrect.top = istartline*nheight; if (pdoc- >m_phexfile != null) { int nline; for (nline = istartline; scrollrect.top < scrollrect.bottom; nline++) { if (!pdoc- >readfileandprocess (sline, nline*16)) break; nheight = pdc- >drawtext(sline, -1, &scrollrect, dt_top | dt_noprefix | dt_singleline); scrollrect.top += nheight; } } pdc- >selectobject(poldfont);
---- 3.4 对该工程进行编译、连接,形成运行文件hexshow.exe经过运行、实际测试,使用效果良好。
---- 4 技术关键
---- 通过上面介绍,可知该程序并不复杂。其设计到的技术关键有4条。
---- a) 利用文档/视架构能有效地降低软件的复杂度,使文档专注于处理数据,而视由于继承自cscrollview,则便于文本的显示和滚动;
---- b) 选择一种合适的字体非常重要,否则,可能出现显示混乱的情况;
---- c) 选择一个正确的成员函数往往能起到事半功倍的效果,比如,进行文本输出时,使用cdc::drawtext(…),就比使用常规的cdc::textout(…)有很大的优点;
---- d) 不管滚动条处于什么位置,视只显示所涉及到的文本行。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -