unit mgr;interfaceuses Windows, Messages, SysUtils, Classes, Forms, StdCtrls,shlobj, Controls, Dialogs,shellapi;type TForm1 = class(TForm) GroupBox1: TGroupBox; Button1: TButton; OpenDialog1: TOpenDialog; SaveDialog1: TSaveDialog; Button3: TButton; GroupBox2: TGroupBox; ListBox1: TListBox; procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean); procedure ListBox1DblClick(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } fstream1:tfilestream; fstream2:tfilestream; list:tstrings; len:tstrings; public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM} const flen=136192; //请注意修改这儿的长度type FILE_INFO=record filename:array[0..MAX_PATH] of char; len:integer; end;function SelectDirectory(handle:hwnd;const Caption: string; const Root: WideString;out Directory: string): Boolean; var lpbi:_browseinfo; buf:array [0..MAX_PATH] of char; id:ishellfolder; eaten,att:cardinal; rt:pitemidlist; initdir:pwidechar; begin result:=false; lpbi.hwndOwner:=handle; lpbi.lpfn:=nil; lpbi.lpszTitle:=pchar(caption); lpbi.ulFlags:=BIF_RETURNONLYFSDIRS+BIF_EDITBOX; SHGetDesktopFolder(id); initdir:=pwchar(root); id.ParseDisplayName(0,nil,initdir,eaten,rt,att); lpbi.pidlRoot:=rt; getmem(lpbi.pszDisplayName,MAX_PATH); try result:=shgetpathfromidlist(shbrowseforfolder(lpbi),buf); except freemem(lpbi.pszDisplayName); end; if result then begin directory:=buf; if length(directory)<>3 then directory:=directory+'\'; end; end;procedure TForm1.Button1Click(Sender: TObject); var info:FILE_INFO; i:integer; buf:array[0..4096] of byte; s:integer; begin if savedialog1.Execute then if opendialog1.Execute then begin try copyfile(pchar(paramstr(0)),pchar(savedialog1.FileName),false); fstream1:=tfilestream.Create(pchar(savedialog1.FileName),fmopenreadwrite); fstream1.Seek(flen,soFromBeginning); for i:=0 to opendialog1.Files.Count-1 do begin strpcopy(info.filename,extractfilename(opendialog1.files.strings[i])); fstream2:=tfilestream.Create(opendialog1.Files.Strings[i],fmopenread); info.len:=fstream2.Size; fstream1.Write(info,sizeof(info)); while fstream2.Position<>fstream2.Size do begin s:=fstream2.Read(buf,sizeof(buf)); fstream1.Write(buf,s); end; fstream2.Free; end; finally fstream1.Free; end; end; end;procedure TForm1.Button3Click(Sender: TObject); var f:textfile; info:FILE_INFO; i:integer; buf:array[0..4096] of byte; s:integer; count,b:integer; dir:string; begin if selectdirectory(handle,'选择输出文件夹','',dir) then try fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite); fstream1.Seek(flen,soFromBeginning); while fstream1.Position<>fstream1.Size do begin fstream1.Read(info,sizeof(info)); count:=0; assignfile(f,dir+info.filename); rewrite(f); closefile(f); fstream2:=tfilestream.Create(dir+info.filename,fmopenwrite); fstream2.Size:=0; i:=info.len div sizeof(buf); for b:=1 to i do begin s:=fstream1.Read(buf,sizeof(buf)); fstream2.Write(buf,s); inc(count,s); end; s:=fstream1.Read(buf,info.len-count); fstream2.Write(buf,s); fstream2.Free; end; finally fstream1.Free; end; end;procedure TForm1.FormCreate(Sender: TObject); var info:FILE_INFO; begin list:=tstringlist.Create; len:=tstringlist.Create; try fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite); fstream1.Seek(flen,soFromBeginning); while fstream1.Position<>fstream1.Size do begin fstream1.Read(info,sizeof(info)); list.Add(inttostr(fstream1.position)); len.Add(inttostr(info.len)); listbox1.Items.Add(info.filename); fstream1.Seek(info.len,soFromCurrent); end; finally fstream1.Free; end; if listbox1.Items.Count>0 then button3.Enabled:=true else button3.Enabled:=false; end;procedure TForm1.SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean); var f:integer; begin f:=filecreate(savedialog1.FileName); if f<=0 then begin MessageBox(handle,'不能选择输出到该文件!',pchar(application.Title),MB_OK+MB_ICONerror); canclose:=false; end; fileclose(f); end;procedure TForm1.ListBox1DblClick(Sender: TObject); var path:array[0..max_path] of char; filename:string; f,b,s,count:integer; buf:array[0..4096] of char; begin if button3.Enabled=false then exit; gettemppath(Max_path,path); filename:=path+listbox1.Items.Strings[listbox1.itemindex]; fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite); f:=filecreate(filename); fileclose(f); count:=0; fstream2:=tfilestream.Create(filename,fmopenwrite); fstream1.Seek(strtoint(list.Strings[listbox1.ItemIndex]),sofrombeginning); f:=strtoint(len.Strings[listbox1.itemindex]) div sizeof(buf); for b:=1 to f do begin s:=fstream1.Read(buf,sizeof(buf)); fstream2.Write(buf,s); inc(count,s); end; s:=fstream1.Read(buf,strtoint(len.Strings[listbox1.itemindex])-count); fstream2.Write(buf,s); fstream2.Free; shellexecute(handle,'open',pchar(filename),'','',sw_show); fstream1.Free; end;procedure TForm1.FormDestroy(Sender: TObject); var i:integer; path:array[0..max_path] of char; filename:string; begin list.Free; len.Free; gettemppath(Max_path,path); for i:=0 to listbox1.Items.Count-1 do begin filename:=path+listbox1.Items.Strings[i]; deletefile(filename); end; end;end.
你是不是要把文件绑在正在执行的EXE的本身里面?
如果这样的话
那办法有很多
最难的就是删除EXE自身
其他都好办的
关于删除EXE自身
可以查找以前的贴子
说简单一点,就是自解压类型的操作??
给出源码立即得分!
(哭了)你真是的救命恩人呀!真是太谢谢了!先看看!
[email protected]
Windows, Messages, SysUtils, Classes, Forms,
StdCtrls,shlobj, Controls, Dialogs,shellapi;type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Button1: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Button3: TButton;
GroupBox2: TGroupBox;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean);
procedure ListBox1DblClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
fstream1:tfilestream;
fstream2:tfilestream;
list:tstrings;
len:tstrings;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
const
flen=136192; //请注意修改这儿的长度type
FILE_INFO=record
filename:array[0..MAX_PATH] of char;
len:integer;
end;function SelectDirectory(handle:hwnd;const Caption: string; const Root: WideString;out Directory: string): Boolean;
var
lpbi:_browseinfo;
buf:array [0..MAX_PATH] of char;
id:ishellfolder;
eaten,att:cardinal;
rt:pitemidlist;
initdir:pwidechar;
begin
result:=false;
lpbi.hwndOwner:=handle;
lpbi.lpfn:=nil;
lpbi.lpszTitle:=pchar(caption);
lpbi.ulFlags:=BIF_RETURNONLYFSDIRS+BIF_EDITBOX;
SHGetDesktopFolder(id);
initdir:=pwchar(root);
id.ParseDisplayName(0,nil,initdir,eaten,rt,att);
lpbi.pidlRoot:=rt;
getmem(lpbi.pszDisplayName,MAX_PATH);
try
result:=shgetpathfromidlist(shbrowseforfolder(lpbi),buf);
except
freemem(lpbi.pszDisplayName);
end;
if result then
begin
directory:=buf;
if length(directory)<>3 then directory:=directory+'\';
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
info:FILE_INFO;
i:integer;
buf:array[0..4096] of byte;
s:integer;
begin
if savedialog1.Execute then
if opendialog1.Execute then
begin
try
copyfile(pchar(paramstr(0)),pchar(savedialog1.FileName),false);
fstream1:=tfilestream.Create(pchar(savedialog1.FileName),fmopenreadwrite);
fstream1.Seek(flen,soFromBeginning);
for i:=0 to opendialog1.Files.Count-1 do
begin
strpcopy(info.filename,extractfilename(opendialog1.files.strings[i]));
fstream2:=tfilestream.Create(opendialog1.Files.Strings[i],fmopenread);
info.len:=fstream2.Size;
fstream1.Write(info,sizeof(info));
while fstream2.Position<>fstream2.Size do
begin
s:=fstream2.Read(buf,sizeof(buf));
fstream1.Write(buf,s);
end;
fstream2.Free;
end;
finally
fstream1.Free;
end;
end;
end;procedure TForm1.Button3Click(Sender: TObject);
var
f:textfile;
info:FILE_INFO;
i:integer;
buf:array[0..4096] of byte;
s:integer;
count,b:integer;
dir:string;
begin
if selectdirectory(handle,'选择输出文件夹','',dir) then
try
fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite);
fstream1.Seek(flen,soFromBeginning);
while fstream1.Position<>fstream1.Size do
begin
fstream1.Read(info,sizeof(info));
count:=0;
assignfile(f,dir+info.filename);
rewrite(f);
closefile(f);
fstream2:=tfilestream.Create(dir+info.filename,fmopenwrite);
fstream2.Size:=0;
i:=info.len div sizeof(buf);
for b:=1 to i do
begin
s:=fstream1.Read(buf,sizeof(buf));
fstream2.Write(buf,s);
inc(count,s);
end;
s:=fstream1.Read(buf,info.len-count);
fstream2.Write(buf,s);
fstream2.Free;
end;
finally
fstream1.Free;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var
info:FILE_INFO;
begin
list:=tstringlist.Create;
len:=tstringlist.Create;
try
fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite);
fstream1.Seek(flen,soFromBeginning);
while fstream1.Position<>fstream1.Size do
begin
fstream1.Read(info,sizeof(info));
list.Add(inttostr(fstream1.position));
len.Add(inttostr(info.len));
listbox1.Items.Add(info.filename);
fstream1.Seek(info.len,soFromCurrent);
end;
finally
fstream1.Free;
end;
if listbox1.Items.Count>0 then button3.Enabled:=true else button3.Enabled:=false;
end;procedure TForm1.SaveDialog1CanClose(Sender: TObject;
var CanClose: Boolean);
var
f:integer;
begin
f:=filecreate(savedialog1.FileName);
if f<=0 then
begin
MessageBox(handle,'不能选择输出到该文件!',pchar(application.Title),MB_OK+MB_ICONerror);
canclose:=false;
end;
fileclose(f);
end;procedure TForm1.ListBox1DblClick(Sender: TObject);
var
path:array[0..max_path] of char;
filename:string;
f,b,s,count:integer;
buf:array[0..4096] of char;
begin
if button3.Enabled=false then exit;
gettemppath(Max_path,path);
filename:=path+listbox1.Items.Strings[listbox1.itemindex];
fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite);
f:=filecreate(filename);
fileclose(f);
count:=0;
fstream2:=tfilestream.Create(filename,fmopenwrite);
fstream1.Seek(strtoint(list.Strings[listbox1.ItemIndex]),sofrombeginning);
f:=strtoint(len.Strings[listbox1.itemindex]) div sizeof(buf);
for b:=1 to f do
begin
s:=fstream1.Read(buf,sizeof(buf));
fstream2.Write(buf,s);
inc(count,s);
end;
s:=fstream1.Read(buf,strtoint(len.Strings[listbox1.itemindex])-count);
fstream2.Write(buf,s);
fstream2.Free;
shellexecute(handle,'open',pchar(filename),'','',sw_show);
fstream1.Free;
end;procedure TForm1.FormDestroy(Sender: TObject);
var
i:integer;
path:array[0..max_path] of char;
filename:string;
begin
list.Free;
len.Free;
gettemppath(Max_path,path);
for i:=0 to listbox1.Items.Count-1 do
begin
filename:=path+listbox1.Items.Strings[i];
deletefile(filename);
end;
end;end.
然后用loadresource函数解压。
添加图片过程我们可以直接用前面的Cjt_AddtoFile,而现在要做的是如何把图像读出并显示。我们用前面的Cjt_LoadFromFile先把图片读出来保存为文件再调入也是可以的,但是还有更简单的方法,就是直接把文件流读出来显示,有了流这个利器,一切都变的简单了。
现在的图片比较流行的是BMP格式和JPG格式。我们现在就针对这两种图片写出读取并显示函数。Function Cjt_BmpLoad(ImgBmp:TImage;SourceFile:String):Boolean;
var
Source:TFileStream;
MyFileSize:integer;
begin
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareDenyNone);
try
try
Source.Seek(-sizeof(MyFileSize),soFromEnd);
Source.ReadBuffer(MyFileSize,sizeof(MyFileSize));//读出资源
Source.Seek(-MyFileSize,soFromEnd);//定位到资源开始位置
ImgBmp.Picture.Bitmap.LoadFromStream(Source);
finally
Source.Free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;
上面是读出BMP图片的,下面的是读出JPG图片的函数,因为要用到JPG单元,所以要在程序中添加一句:uses jpeg。Function Cjt_JpgLoad(JpgImg:Timage;SourceFile:String):Boolean;
var
Source:TFileStream;
MyFileSize:integer;
Myjpg: TJpegImage;
begin
try
Myjpg:= TJpegImage.Create;
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareDenyNone);
try
Source.Seek(-sizeof(MyFileSize),soFromEnd);
Source.ReadBuffer(MyFileSize,sizeof(MyFileSize));
Source.Seek(-MyFileSize,soFromEnd);
Myjpg.LoadFromStream(Source);
JpgImg.Picture.Bitmap.Assign(Myjpg);
finally
Source.Free;
Myjpg.free;
end;
except
Result:=false;
Exit;
end;
Result:=true;
end;
有了这两个函数,我们就可以制作读出程序了。下面我们以BMP图片为例:
运行Delphi,新建一个工程,放上一个显示图像控件Image1。在窗口的Create事件中写上一句就可以了:
Cjt_BmpLoad(Image1,Application.ExeName);
这个就是头文件了,然后我们用前面的方法生成一个head.res资源文件。
下面就可以开始制作我们的添加程序了。全部代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, ExtDlgs;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
OpenPictureDialog1: TOpenPictureDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
Function ExtractRes(ResType, ResName, ResNewName : String):boolean;
Function Cjt_AddtoFile(SourceFile,TargetFile:string):Boolean;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
Function TForm1.ExtractRes(ResType, ResName, ResNewName : String):boolean;
var
Res : TResourceStream;
begin
try
Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType));
try
Res.SavetoFile(ResNewName);
Result:=true;
finally
Res.Free;
end;
except
Result:=false;
end;
end;
Function TForm1.Cjt_AddtoFile(SourceFile,TargetFile:string):Boolean;
var
Target,Source:TFileStream;
MyFileSize:integer;
begin
try
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareExclusive);
Target:=TFileStream.Create(TargetFile,fmOpenWrite or fmShareExclusive);
try
Target.Seek(0,soFromEnd);//往尾部添加资源
Target.CopyFrom(Source,0);
MyFileSize:=Source.Size+Sizeof(MyFileSize);//计算资源大小,并写入辅程尾部
Target.WriteBuffer(MyFileSize,sizeof(MyFileSize));
finally
Target.Free;
Source.Free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption:='Bmp2Exe演示程序.作者:陈经韬';
Edit1.Text:='';
OpenPictureDialog1.DefaultExt := GraphicExtension(TBitmap);
OpenPictureDialog1.Filter := GraphicFilter(TBitmap);Button1.Caption:='选择BMP图片';
Button2.Caption:='生成EXE';
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
Edit1.Text:=OpenPictureDialog1.FileName;
end;procedure TForm1.Button2Click(Sender: TObject);
var
HeadTemp:String;
begin
if Not FileExists(Edit1.Text) then
begin
Application.MessageBox('BMP图片文件不存在,请重新选择!','信息',MB_ICONINFORMATION+MB_OK)
Exit;
end;
HeadTemp:=ChangeFileExt(Edit1.Text,'.exe');
if ExtractRes('exefile','head',HeadTemp) then
if Cjt_AddtoFile(Edit1.Text,HeadTemp) then
Application.MessageBox('EXE文件生成成功!','信息',MB_ICONINFORMATION+MB_OK)
else
begin
if FileExists(HeadTemp) then DeleteFile(HeadTemp);
Application.MessageBox('EXE文件生成失败!','信息',MB_ICONINFORMATION+MB_OK)
end;
end;
end.
怎么样?很神奇吧:)把程序界面弄的漂亮点,再添加一些功能,你会发现比起那些要注册的软件来也不会逊多少吧。
-----------------------------------------------------------------------