这是一个注册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 = "加载完成!"; } //---------------------------------------------------------------------------
|