找了个代码,原理类似,看有参考价值没有
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,
Menus, StdCtrls, ComCtrls, Buttons;
type
THexConversion = class(TConversion)
public
function ConvertReadStream(Stream: TStream; Buffer: PChar; BufSize: integer): integer; override;
end;
TForm1 = class(TForm)
RichEdit1: TRichEdit;
StatusBar1: TStatusBar;
OpenDlg: TOpenDialog;
SaveDlg: TSaveDialog;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
procedure Progress(Address: LongInt);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function THexConversion.ConvertReadStream(Stream: TStream; Buffer: PChar;
BufSize: integer): integer;
var
s: string;
buf: array[1..16] of char;
i, n: integer;
begin
Result := 0;
if BufSize <= 82 then Exit;
s := Format(';%.5x ', [Stream.Position]);
n := Stream.Read(buf, 16);
if n = 0 then Exit;
for i := 1 to n do begin
AppendStr(s, IntToHex(ord(buf), 2) + ' ');
if i mod 4 = 0 then AppendStr(s, ' ');
end;
AppendStr(s, StringOfChar(' ', 62 - length(s)));
for i := 1 to n do begin
if (buf < #32) or (buf > #126) then
buf := '.';
AppendStr(s, buf);
end;
AppendStr(s, #13#10);
StrPCopy(Buffer, s);
Result := length(s);
if Stream.Position and $FFF = 0 then Form1.Progress(Stream.Position);
end;
procedure TForm1.Progress(Address: LongInt);
begin
StatusBar1.SimpleText := 'Reading... $' + IntToHex(Address, 5);
StatusBar1.Update;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
RichEdit1.RegisterConversionFormat('bin', THexConversion);
RichEdit1.RegisterConversionFormat('obj', THexConversion);
RichEdit1.RegisterConversionFormat('bmp', THexConversion);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
fname: string;
begin
if OpenDlg.Execute then begin
try
Screen.Cursor := crHourglass;
fname := ExtractFileName(OpenDlg.Filename);
StatusBar1.SimpleText := 'Reading...';
RichEdit1.Lines.Clear;
Application.ProcessMessages;
try
RichEdit1.Lines.LoadFromFile(OpenDlg.Filename);
StatusBar1.SimpleText := fname;
except on E: EFOpenError do begin
StatusBar1.SimpleText := '';
MessageDlg(Format('Can''t open file %s.', [fname]), mtError, [mbOk], 0);
end;
end;
finally
Screen.Cursor := crDefault;
end;
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
SaveDlg.Filename := ChangeFileExt(OpenDlg.Filename, '.txt');
if SaveDlg.Execute then
RichEdit1.Lines.SaveToFile(SaveDlg.Filename);
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
Close;
end;
end.
object Form1: TForm1
Left = 190
Top = 107
Width = 521
Height = 398
BorderIcons = [biSystemMenu, biMinimize]
Caption = 'RichEdit读取二进制文件示例'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 12
object RichEdit1: TRichEdit
Left = 4
Top = 8
Width = 497
Height = 301
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
ImeName = '中文 (简体) - 拼音加加3.11'
ParentFont = False
ScrollBars = ssVertical
TabOrder = 0
end
object StatusBar1: TStatusBar
Left = 0
Top = 350
Width = 513
Height = 21
Panels = <>
SimplePanel = False
end
object BitBtn1: TBitBtn
Left = 252
Top = 316
Width = 75
Height = 25
Caption = '打开'
TabOrder = 2
OnClick = BitBtn1Click
end
object BitBtn2: TBitBtn
Left = 340
Top = 316
Width = 75
Height = 25
Caption = '保存'
TabOrder = 3
OnClick = BitBtn2Click
end
object BitBtn3: TBitBtn
Left = 428
Top = 316
Width = 75
Height = 25
Caption = '退出'
TabOrder = 4
OnClick = BitBtn3Click
end
object OpenDlg: TOpenDialog
Left = 544
Top = 76
end
object SaveDlg: TSaveDialog
Left = 556
Top = 144
end
end