stringlist相关的,请大侠出招
就是建一个包*.pfs,包头的结构是TPfFileSysCtrl,定义目录项结构 TPfDirItem
目录下边就是数据相了(工程目的,是要把很多的图片放到包里,用的时候再调出来.)
谢谢大侠出招
谢谢
unit PackFileMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ToolWin, ComCtrls, Grids, ExtDlgs, StdCtrls, ExtCtrls;
type
TPackFileForm = class(TForm)
PackFileMenu: TMainMenu;
F1: TMenuItem;
O1: TMenuItem;
C1: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
D1: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
pfStatus: TStatusBar;
pfToolBar: TToolBar;
N5: TMenuItem;
N6: TMenuItem;
fileList: TStringGrid;
dlgOpenPf: TOpenDialog;
dlgAddFiles: TOpenDialog;
Image1: TImage;
procedure mnuOpenFileClick(Sender: TObject);
procedure mnuAddFilesClick(Sender: TObject);
procedure N4Click(Sender: TObject);
private
{ Private declarations }
pfFileName: string;
files: TStrings;
tmpfh: integer;
fh: integer;
public
{ Public declarations }
procedure pfGridInit();
end;
{ 定义文件系统的控制结构 }
PTPfFileSysCtrl = ^TPfFileSysCtrl;
TPfFileSysCtrl = record
fileNum: word; { 包内包含的文件个数 }
dirItemNum: word; { 包内目录项个数 }
pad: array[0..27] of byte;
end;
{ 定义目录项结构 }
PTPfDirItem = ^TPfDirItem;
TPfDirItem = record
fileId: word; { 文件ID: ID为0的目录项为空闲目录项 }
filename: array[0..17] of char; { 文件名 }
extname: array[0..3] of char; { 扩展名 }
offset: dword; { 文件在包内的偏移 }
len: dword; { 文件长度 }
end;
var
PackFileForm: TPackFileForm;
ss: file of PTPfDirItem;
implementation
{$R *.dfm}
procedure TPackFileForm.pfGridInit();
begin
filelist.Cells[0, 0] := '序号 ';
filelist.Cells[1, 0] := '文件ID ';
filelist.Cells[2, 0] := '文件名 ';
filelist.Cells[3, 0] := '文件大小 ';
filelist.Cells[4, 0] := '文件位置 ';
{filelist.Cells[0, 1] := '1 ';
filelist.Cells[1, 1] := '0001 ';
filelist.Cells[2, 1] := 'main.jpg ';
filelist.Cells[3, 1] := '1,024,000 ';
filelist.Cells[4, 1] := '1024 '; }
pfFilename := ' ';
pfStatus.Panels[2].Text := ' ';
end;
procedure TPackFileForm.mnuOpenFileClick(Sender: TObject);
var
i: integer;
begin
//if (pfFileName[1] = char(0)) then
//pfStatus.Panels[2].Text := ' ';
if dlgOpenPf.Execute then
begin
i := length(pfFileName);
if (i > 0) and (fh > 0) then
begin
fileclose(fh);
fh := 0;
end;
pfFileName := dlgOpenPf.FileName;
pfStatus.Panels[0].Text := '包文件名: ' + pfFileName;
if not fileExists(pfFileName) then
begin
fh := fileCreate(pfFileName);
end
else
begin
fh := fileopen(pfFileName, fmOpenReadWrite);
end;
end;
end;
procedure parseFileName(fullname: string; var filename, extname: string); //fullname:是该文件的详细路径
var
i, m, n, r, len: integer;
fn: string;
ext: string;
begin
len := length(fullname);
n := 1;
m := 0;
for i := len downto 1 do
begin
if (fullname[i] = '\ ') or (fullname[i] = '/ ') then
begin
n := i + 1;
break;
end;
if (fullname[i] = '. ') and (m = 0) then
m := i + 1;
end;
r := len;
if (m > 0) then
r := m - 2;
fn := copy(fullname, n, (r - n + 1));
if m > 0 then
begin
ext := copy(fullname, m, (len - m + 1));
end;
filename := fn;
extname := ext;
end;
procedure TPackFileForm.mnuAddFilesClick(Sender: TObject);//把文件加入包中,没有写完
var
i: integer;
fn: string;
ext: string;
fullname: string;
str: string;
addlist, orilist: TStrings;
begin
addlist := TStringList.Create;
orilist := TStringList.Create;
if dlgAddFiles.Execute then
begin
for i := 0 to dlgAddFiles.Files.Count - 1 do
begin
parseFileName(dlgAddFiles.Files.Strings[i], fn, ext);
fullname := fn + '. ' + ext;
if (dlgAddFiles.files.IndexOf(fullname) < 0) then // 在stringlist中查找该名称是否存在,不存在的话就加入
begin
orilist.Add(dlgAddFiles.Files.Strings[i]); //完整的路径及名称
addlist.Add(fullname); //名称
end
end;
end;
end;
procedure TPackFileForm.N4Click(Sender: TObject);
begin
PackFileForm.Close;
end;
[解决办法]
太长了 你就直接说stringlist在什么地方弄错了 值不对还是怎么的
建议LZ调试看看
另外
addlist := TStringList.Create;
orilist := TStringList.Create;
这两个东西构造的时候切记写到异常保护模式中去
[解决办法]
StringList用完记着释放
[解决办法]
建议用DELPHI的资源文件 .res 后缀形式的来保存图片
[解决办法]
mnuAddFilesClick
这个过程只用创建StringList怎么没有释放StringList