Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to read the value of a component property directly from its resource Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Object Pascal
Language
Delphi 5.x
Views
135
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

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!');

Answer:

1   function TForm1.ReadProp(r: TReader): string;
2   begin
3     result := '';
4     {Determine the value type of the property, read it with the appropriate method
5     of TReader and convert it to string. Not all value types are implemented here
6     but you get the idea.}
7     case r.NextValue of
8       vaInt8, vaInt16, vaInt32:
9         result := IntToStr(r.ReadInteger);
10      vaExtended:
11        result := FloatToStr(r.ReadFloat);
12      vaString:
13        result := r.ReadString;
14    else
15      r.SkipValue; {Not implemented}
16    end;
17  end;
18  
19  procedure TForm1.ReadRes(PropPath: string; r: TReader);
20  var
21    p: string;
22  begin
23    {Skip the class name}
24    r.ReadStr;
25    {Construct the property path}
26    if PropPath = '' then
27      p := r.ReadStr
28    else
29      p := PropPath + '.' + r.ReadStr;
30    {Read all properties and its values and fill them into the memo}
31    while not r.EndOfList do
32      Memo1.Lines.Add(p + '.' + r.ReadStr + ' = ' + ReadProp(r));
33    {Skip over the end of the list of the properties of this component}
34    r.CheckValue(vaNull);
35    {Recursively read the properties of all sub-components}
36    while not r.EndOfList do
37    begin
38      ReadRes(p, r);
39      r.CheckValue(vaNull);
40    end;
41  end;
42  
43  procedure TForm1.Button1Click(Sender: TObject);
44  var
45    strm: TResourceStream;
46    Reader: TReader;
47  begin
48    strm := TResourceStream.Create(HInstance, 'TForm1', RT_RCDATA);
49    Reader := TReader.Create(strm, 1024);
50    try
51      Memo1.Clear;
52      Reader.ReadSignature;
53      ReadRes('', Reader);
54    finally
55      Reader.Free;
56      strm.Free;
57    end;
58  end;


Only one small problem.
r.SkipValue was protected (in D5) but I hacked that out with the following code:

59  type
60    THackReader = class(TReader);
61    { ... }
62    THackReader(r).SkipValue;


And now it works like a charm.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC