科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件RichEdit的自动格式化

RichEdit的自动格式化

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

这是一个注册EXE,OBJ,BIN三种类型文件当其被RichEdit打开时会自动转换为16进制显示的例子.

作者:佚名 来源:逸仙时空 2007年11月2日

关键字: RICHEDIT 自动格式化

  • 评论
  • 分享微博
  • 分享邮件
这是一个注册EXE,OBJ,BIN三种类型文件当其被RichEdit打开时会自动转换为16进制显示的例子

C++ Builder

请参照Delphi的例子

Delphi

第一:要从TCoriversion派生出一个新类

第二:重载CorrvertReadStream函数

第三:在主窗体的OnCreate函数中登记文件类型。用RichEdit的.RegisterConversionFormat函数

unit main;

interface

uses

Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,

Menus, StdCtrls, ComCtrls;



type

TMainForm = class(TForm)

MainMenu1: TMainMenu;

MenuFile: TMenuItem;

MenuOpen: TMenuItem;

MenuSaveAs: TMenuItem;

N1: TMenuItem;

MenuExit: TMenuItem;

OpenDlg: TOpenDialog;

SaveDlg: TSaveDialog;

Rich: TRichEdit;

StatusBar: TStatusBar;

procedure FormCreate(Sender: TObject);

procedure MenuOpenClick(Sender: TObject);

procedure MenuSaveAsClick(Sender: TObject);

procedure MenuExitClick(Sender: TObject);

procedure MenuPopupPopup(Sender: TObject);

procedure MenuSelectAllClick(Sender: TObject);

procedure MenuCopyClick(Sender: TObject);

private

procedure Progress(Address:LongInt);

end;

var

MainForm: TMainForm;

implementation

{$R *.DFM}

type

THexConversion = class(TConversion)

public

function ConvertReadStream(Stream:TStream; Buffer:PChar;

BufSize:integer): integer; override;

end;

// This implements a callback procedure used by TRichEdit when loading

// a file. Gets called repeatedly until stream is empty.

//

function THexConversion.ConvertReadStream(Stream:TStream; Buffer:PChar;

BufSize:integer): intege

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[i]),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[i] < #32) or (buf[i] > #126) then

buf[i] := .;

AppendStr(s,buf[i]);

end;

AppendStr(s,#13#10);

StrPCopy(Buffer,s);

Result := length(s);

if Stream.Position and $FFF = 0 then MainForm.Progress(Stream.Position);

end;

procedure TMainForm.Progress(Address:LongInt);

begin

StatusBar.SimpleText := Reading... $+IntToHex(Address,5);

StatusBar.Update;

end;

procedure TMainForm.FormCreate(Sender: TObject);

begin

Rich.RegisterConversionFormat(bin,THexConversion);

Rich.RegisterConversionFormat(obj,THexConversion);

Rich.RegisterConversionFormat(exe,THexConversion);

end;

procedure TMainForm.MenuOpenClick(Sender: TObject);

var fname:string;

begin

if OpenDlg.Execute then

begin

try

Screen.Cursor := crHourglass;

fname := ExtractFileName(OpenDlg.Filename);

StatusBar.SimpleText := Reading...;

Rich.Lines.Clear;

Application.ProcessMessages;

try

Rich.Lines.LoadFromFile(OpenDlg.Filename);

StatusBar.SimpleText := fname;

except on E:EFOpenError do

begin

StatusBar.SimpleText := ;

MessageDlg(Format(Cant open file %s.,[fname]),mtError,[mbOk],0);

end;

end;

finally

Screen.Cursor := crDefault;

end;

end;

end;

procedure TMainForm.MenuSaveAsClick(Sender: TObject);

begin

SaveDlg.Filename := ChangeFileExt(OpenDlg.FileName,.txt);

if SaveDlg.Execute then

Rich.Lines.SaveToFile(SaveDlg.FileName);

end;

procedure TMainForm.MenuExitClick(Sender: TObject);

begin

Close;

end;

end.

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章