看看这个<br><br>这是一个注册EXE,OBJ,BIN三种类型文件当其被RichEdit打开时会自动转换为16进制显示的例子<br><br>第一:要从TCoriversion派生出一个新类<br>第二:重载CorrvertReadStream函数<br>第三:在主窗体的OnCreate函数中登记文件类型。用RichEdit的.RegisterConversionFormat函数<br><br><br>unit main;<br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,<br> Menus, StdCtrls, ComCtrls;<br><br>type<br> TMainForm = class(TForm)<br> MainMenu1: TMainMenu;<br> MenuFile: TMenuItem;<br> MenuOpen: TMenuItem;<br> MenuSaveAs: TMenuItem;<br> N1: TMenuItem;<br> MenuExit: TMenuItem;<br> OpenDlg: TOpenDialog;<br> SaveDlg: TSaveDialog;<br> Rich: TRichEdit;<br> StatusBar: TStatusBar;<br> procedure FormCreate(Sender: TObject);<br> procedure MenuOpenClick(Sender: TObject);<br> procedure MenuSaveAsClick(Sender: TObject);<br> procedure MenuExitClick(Sender: TObject);<br> procedure MenuPopupPopup(Sender: TObject);<br> procedure MenuSelectAllClick(Sender: TObject);<br> procedure MenuCopyClick(Sender: TObject);<br> private<br> procedure Progress(Address:LongInt);<br> end;<br><br>var<br> MainForm: TMainForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>type<br> THexConversion = class(TConversion)<br> public<br> function ConvertReadStream(Stream:TStream; Buffer
Char;<br> BufSize:integer): integer; override;<br> end;<br><br><br>// This implements a callback procedure used by TRichEdit when loading<br>// a file. Gets called repeatedly until stream is empty.<br>//<br>function THexConversion.ConvertReadStream(Stream:TStream; Buffer
Char;<br> BufSize:integer): integer;<br>var s:string;<br> buf:array[1..16] of char;<br> i,n:integer;<br>begin<br> Result := 0;<br> if BufSize <= 82 then Exit;<br> s := Format(';%.5x ',[Stream.Position]);<br> n := Stream.Read(buf,16);<br> if n = 0 then Exit;<br> for i := 1 to n do<br> begin<br> AppendStr(s,IntToHex(ord(buf
),2)+' ');<br> if i mod 4 = 0 then AppendStr(s,' ');<br> end;<br> AppendStr(s,StringOfChar(' ',62-length(s)));<br> for i := 1 to n do<br> begin<br> if (buf < #32) or (buf > #126) then<br> buf := '.';<br> AppendStr(s,buf);<br> end;<br> AppendStr(s,#13#10);<br> StrPCopy(Buffer,s);<br> Result := length(s);<br> if Stream.Position and $FFF = 0 then MainForm.Progress(Stream.Position);<br>end;<br><br>procedure TMainForm.Progress(Address:LongInt);<br>begin<br> StatusBar.SimpleText := 'Reading... $'+IntToHex(Address,5);<br> StatusBar.Update;<br>end;<br><br>procedure TMainForm.FormCreate(Sender: TObject);<br>begin<br> Rich.RegisterConversionFormat('bin',THexConversion);<br> Rich.RegisterConversionFormat('obj',THexConversion);<br> Rich.RegisterConversionFormat('exe',THexConversion);<br>end;<br><br>procedure TMainForm.MenuOpenClick(Sender: TObject);<br>var fname:string;<br>begin<br> if OpenDlg.Execute then<br> begin<br> try<br> Screen.Cursor := crHourglass;<br> fname := ExtractFileName(OpenDlg.Filename);<br> StatusBar.SimpleText := 'Reading...';<br> Rich.Lines.Clear;<br> Application.ProcessMessages;<br> try<br> Rich.Lines.LoadFromFile(OpenDlg.Filename);<br> StatusBar.SimpleText := fname;<br> except on E:EFOpenError do<br> begin<br> StatusBar.SimpleText := '';<br> MessageDlg(Format('Can''t open file %s.',[fname]),mtError,[mbOk],0);<br> end;<br> end;<br> finally<br> Screen.Cursor := crDefault;<br> end;<br> end;<br>end;<br><br><br>procedure TMainForm.MenuSaveAsClick(Sender: TObject);<br>begin<br> SaveDlg.Filename := ChangeFileExt(OpenDlg.FileName,'.txt');<br> if SaveDlg.Execute then<br> Rich.Lines.SaveToFile(SaveDlg.FileName);<br>end;<br><br>procedure TMainForm.MenuExitClick(Sender: TObject);<br>begin<br> Close;<br>end;<br><br>end.