BCB中实现TRichEdit的自动名目化
这是一个注册EXE,OBJ,BIN三种范例文件,当其被RichEdit打开时会自动转换为16进制显示的例子
——————————————————————————–
步调:
第一:要从TCoriversion派生出一个新类
第二:重载CorrvertReadStream函数
第三:在主窗体的OnCreate函数中挂号文件范例。用RichEdit的.RegisterConversionFormat函数
//---------------------------------------------------------------------------
// 从TCoriversion派生出一个新类
class THexConversion : public TConversion
{
public:
virtual int __fastcall ConvertReadStream(TStream *pStream,
char *pSrcBuffer, int nBufSize);
};
//---------------------------------------------------------------------------
// 重载 ConvertReadStream 函数
int __fastcall THexConversion::ConvertReadStream(TStream *pStream,
char *pSrcBuffer, int nBufSize)
{
String strTemp;
unsigned char szDstBuffer[16];
int n;
int nResult;
if(nBufSize <= 82)
return 0;
// 行号,雷同UtrlEdit,用8位来暗示
strTemp = strTemp.sprintf("%.8xh: ", pStream->Position);
n = pStream->Read(szDstBuffer, 16);
if(n == 0)
return 0;
// 显示字符的ASCII值,四个一组,中间用空格脱离
for(int i=0; i<n; i++)
{
AppendStr(strTemp, IntToHex(szDstBuffer[i], 2) + ' ');
if((i+1) % 4 == 0)
AppendStr(strTemp, ' ');
}
String str;
str.StringOfChar(' ', 65 - strTemp.Length());
AppendStr(strTemp, str + "; ");
// 显示实际的Ascii字符,假如长短可打印字符,用'.'取代
for(int i=0; i<n; i++)
{
if((szDstBuffer[i] < 32) || (szDstBuffer[i] > 126))
szDstBuffer[i] = '.';
AppendStr(strTemp, (char)szDstBuffer[i]);
}
AppendStr(strTemp, "\n");
StrCopy(pSrcBuffer, strTemp.c_str());
nResult = strTemp.Length();
// 显示加载进度
Form1->Process(pStream->Position);
Application->ProcessMessages();
return nResult;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// 挂号文件范例
RichEdit1->RegisterConversionFormat(NULL, "bin", __classid(THexConversion));
RichEdit1->RegisterConversionFormat(NULL, "obj", __classid(THexConversion));
RichEdit1->RegisterConversionFormat(NULL, "exe", __classid(THexConversion));
}
//---------------------------------------------------------------------------
// 自界说函数,须在.h文件中TForm1类中声明一下,譬喻:
// public:
// void __fastcall Process(int nPos);
//
// 为防备不认真任的转载者,在些注明原作及修改者信息,请留情。
// 原作:张晶晶
// 修改:ccrun(老妖),接待降临C++Builder研究: http://www.ccrun.com
//---------------------------------------------------------------------------
void __fastcall TForm1::Process(int nPos)
{
// 显示加载进度
StatusBar1->SimpleText = "正在处理惩罚... " + String(nPos);
StatusBar1->Update();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
RichEdit1->Lines->LoadFromFile("C:\\123\\123.exe");
StatusBar1->SimpleText = "加载完成!";
}
//---------------------------------------------------------------------------