2010. szeptember 1., szerda
Modify a resource string at runtime
Problem/Question/Abstract:
How to modify a resource string at runtime
Answer:
{Modify a resourcestring constant:
Parameters:
Resstring is a pointer to a TResStringRec describing a resourcestring constant.
If this parameter is nil the function returns false.
Newvalue points to a zero-terminated string that should be the new value of the
resourcestring. This pointer must be guaranteed to stay valid for the lifetime of the
program! It should point to a constant string (a literal).
Returns true if the change was successful, false if not.
Precondition: newvalue <> nil
Will not work in Kylix! }
function ChangeResourceString(resstring: PResStringRec; newvalue: PChar): Boolean;
var
oldprotect: DWORD;
begin
Result := false;
{$IFDEF LINUX}
Thisfunction does not work in Kylix!{$ENDIF}
if resstring = nil then
Exit;
Assert(Assigned(newvalue), 'ChangeResourceString: newvalue must be <> nil');
Result := VirtualProtect(resstring, Sizeof(resstring^), PAGE_EXECUTE_READWRITE,
@oldProtect);
if Result then
begin
resstring^.Identifier := Integer(newvalue);
VirtualProtect(resstring, SizeOf(resstring^), oldProtect, @oldProtect);
end;
end;
Used like this:
resourcestring
TestStr = 'Hello World';
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.caption := TestStr;
ChangeResourceString(pResStringRec(@TestStr), 'Some test text');
label2.caption := TestStr;
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése