=从资源中获取数据是这样的:
function GetMyRes(resname:string):string;
var
rStream: TResourceStream;
s:string;
begin
rStream := TResourceStream.Create(hInstance, resname, RT_RCDATA);
...
...
result:=s;
End;
>
=使用数据
Procedure GetReses;
var
myres1:string;
myres2:string;
begin
myres1:=GetMyRes("MYSTRING1");
myres2:=GetMyRes("MYSTRING2");
end;
>
=将数据放入资源是这样的:
Procedure SaveToResource(Dest:string);
var
s:string;
cbData : Cardinal;
hDestRes : THANDLE;
begin
if x=1 then s:='1a' else s:='1b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING1',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
if x=2 then s:='2a' else s:='2b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING2',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
if x=3 then s:='3a' else s:='3b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING3',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
if x=4 then s:='4a' else s:='4b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING4',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
if x=5 then s:='5a' else s:='5b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING5',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
end;
>
这里的问题是,如果我像这样放置资源会怎样:(零递增)
Procedure SaveToResource(Dest:string);
var
s:string;
cbData : Cardinal;
hDestRes : THANDLE;
begin
if x=1 then s:='1a' else s:='1b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
if x=2 then s:='2a' else s:='2b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',1,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
if x=3 then s:='3a' else s:='3b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',2,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
if x=4 then s:='4a' else s:='4b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',3,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
if x=5 then s:='5a' else s:='5b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',4,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
end;
如何获得 0 到 4 范围内的“MYSTRINGS_ALL”?
>
这是行不通的,因为只有当我将 ZER0 放入我一一命名的所有内容时,它才会得到。
function GetMyRes(resname:string):string;
var
rStream: TResourceStream;
s:string;
begin
rStream := TResourceStream.Create(hInstance, resname, RT_RCDATA);
...
result:=s;
End;
什么是正确的函数或获取从 0 到 4 个字符串的所有范围的方法?
> 我认为这是正确的,我认为是的!如果代码中有任何错误,请更正。
function GetMySTRResources(id:integer): string;
var
hF,hR:THandle;
s:PChar;
begin
hF:=FindResourceEx(HInstance, RT_RCDATA ,'MYSTRINGS_ALL', id);
if hF<>0 then begin
hR:=LoadResource(HInstance, hF) ;
if hR<>0 then begin
s:=LockResource(hR) ;
if Not Assigned(s) then s:='';
UnlockResource(hR) ;
end;
FreeResource(hF) ;
end;
Result := s;
end;
谢谢罗布肯尼迪