📄 convunit.pas
字号:
unit ConvUnit;
interface
function DfmFileToDefFile(const DfmPath, DefPath: string): Boolean;
implementation
uses Windows, SysUtils, Classes, CodeUnit;
function FileToString(const FilePath: string): string;
var
hFile: THandle;
nSize: DWord;
begin
Result := '';
hFile := CreateFile(PChar(FilePath), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile = INVALID_HANDLE_VALUE) then Exit;
nSize := GetFileSize(hFile, nil);
SetLength(Result, nSize);
ReadFile(hFile, Result[1], nSize, nSize, nil);
CloseHandle(hFile);
end;
function StringToFile(const FilePath, FileText: string): Boolean;
var
hFile: THandle;
nSize: DWord;
begin
Result := False;
hFile := CreateFile(PChar(FilePath), GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile = INVALID_HANDLE_VALUE) then Exit;
WriteFile(hFile, FileText[1], Length(FileText), nSize, nil);
SetEndOfFile(hFile);
CloseHandle(hFile);
Result := True;
end;
function StringToDefine(const Str: string): string;
var
J, L: Integer;
P: PDWord;
begin
L := Length(Str);
if (L = 0) then
Result := #$27#$27
else begin
SetLength(Result, L * 4 + (L - 1) div 20 * 4);
P := @Result[1];
for J := 1 to L do
if (J mod 20 = 1) and (J <> 1) then
begin
P^ := PDWord(PChar(' +'#13#10))^;
Inc(P);
P^ := PDWord(PChar('#$' + IntToHex(Ord(Str[J]), 2)))^;
Inc(P);
end else
begin
P^ := PDWord(PChar('#$' + IntToHex(Ord(Str[J]), 2)))^;
Inc(P);
end;
end;
end;
function DfmFileToDefFile(const DfmPath, DefPath: string): Boolean;
var
DfmStream, BinStream: TStringStream;
DfmString, BinString, DefString: string;
begin
DfmStream := nil;
BinStream := nil;
Result := False;
try
// 读出Dfm
DfmString := FileToString(DfmPath);
if (DfmString = '') then Exit;
DfmStream := TStringStream.Create(DfmString);
// 转成Bin
BinStream := TStringStream.Create(BinString);
ObjectTextToBinary(DfmStream, BinStream);
// 加密Bin
BinString := BinStream.DataString;
FastCode(BinString);
// 转成Def
DefString := StringToDefine(BinString);
// 保存Def
Result := StringToFile(DefPath, DefString);
finally
DfmStream.Free;
BinStream.Free;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -