![]() |
![]() ![]() ![]() ![]() |
|
Как прочитать свойство напрямую из его ресурсаDelphi , Компоненты и Классы , RTTI
Автор: Michael Duerig Does anyone know if there is an easy way to load the value of a component's property directly from its resource without creating the component? Something like:
if ReadPropertyValue('Form1.Button1', 'width') > 1000 then
ShowMessage('You are about to create a big button!');
function TForm1.ReadProp(r: TReader): string;
begin
result := '';
{Determine the value type of the property, read it with the appropriate method of TReader
and convert it to string. Not all value types are implemented here but you get the idea.}
case r.NextValue of
vaInt8, vaInt16, vaInt32:
result := IntToStr(r.ReadInteger);
vaExtended:
result := FloatToStr(r.ReadFloat);
vaString:
result := r.ReadString;
else
r.SkipValue; {Not implemented}
end;
end;
procedure TForm1.ReadRes(PropPath: string; r: TReader);
var
p: string;
begin
{Skip the class name}
r.ReadStr;
{Construct the property path}
if PropPath = '' then
p := r.ReadStr
else
p := PropPath + '.' + r.ReadStr;
{Read all properties and its values and fill them into the memo}
while not r.EndOfList do
Memo1.Lines.Add(p + '.' + r.ReadStr + ' = ' + ReadProp(r));
{Skip over the end of the list of the properties of this component}
r.CheckValue(vaNull);
{Recursively read the properties of all sub-components}
while not r.EndOfList do
begin
ReadRes(p, r);
r.CheckValue(vaNull);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
strm: TResourceStream;
Reader: TReader;
begin
strm := TResourceStream.Create(HInstance, 'TForm1', RT_RCDATA);
Reader := TReader.Create(strm, 1024);
try
Memo1.Clear;
Reader.ReadSignature;
ReadRes('', Reader);
finally
Reader.Free;
strm.Free;
end;
end;
Only one small problem. r.SkipValue was protected (in D5) but I hacked that out with the following code:
type
THackReader = class(TReader);
{ ... }
THackReader(r).SkipValue;
And now it works like a charm. infamous Да, вы правы в том, что в Delphi 5 поле В новых версиях Delphi (например, Delphi XE и позднее), поле Надо отметить, что есть альтернативные подходы для достижения вашей цели. Например, вы можете использовать класс Этот подход требует больше настройки и регистрации типов, но может быть жизнеспособной альтернативой, если вам нужно прочитать свойства из ресурсов в более гибкой форме. Обратите внимание, что это только пример, и вы можете потребовать адаптации его к вашим конкретным требованиям. В статье описывается способ чтения свойств компонента напрямую из его ресурса без создания компонента, используя класс TReader и функцию ReadProp для преобразования значения свойства в строку. Комментарии и вопросыПолучайте свежие новости и обновления по Object Pascal, Delphi и Lazarus прямо в свой смартфон. Подпишитесь на наш
|
||||
©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007 | ||||