Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Komponenten ansprechen - mit ini konfigurieren (https://www.delphipraxis.net/169581-komponenten-ansprechen-mit-ini-konfigurieren.html)

matashen 30. Jul 2012 13:25

Komponenten ansprechen - mit ini konfigurieren
 
Hallo zusammen,

ich muss momentan ein paar Komponenten bei einer Anwendung frei konfigurierbar machen.
Hab das einfach mal mit Inifiles gemacht, klappt auch.
Momentan mach ich das aber mit Findcomponent, was zwar in der jetzigen Form läuft, aber mir nicht sonderlich gut gefällt.
Evtl hat ja da einer ne Idee dazu. Mit Inifiles steuerbar solls bleiben.

Hier mal wies bei mir im Inifile aussieht

Code:
[objects]
count=73
0=dot
1=irgendwas
...

[dot]
type = button
hint='Punkt zeichnen | Zum frei Hand Zeichnen mit Hilfe der Strichbreite, auch möglich nur einzelne Punkte zu zeichnen'
left = 10
top = 10
... usw
So ausgewertet wird das so
Delphi-Quellcode:
    objects_count:=inimain.ReadInteger('objects','count',0);
    if objects_count<>0 then
      for i := 0 to objects_count-1 do begin;
          objects_name:=inimain.ReadString('objects',inttostr(i),'');
          object_type:=inimain.ReadString(objects_name,'type','');
          object_type:=lowercase(object_type);
          if object_type='button' then begin;
                TButton(Findcomponent(objects_name)).hint:=inimain.Readstring(objects_name,'hint',TButton(Findcomponent(objects_name)).Hint);
Und genau da stört mich das ich den Componentyp angeben muss.
Meine Traumlösung wär hier einfach

objectsname.x:=inimain.Readinteger.....

geht sowas, gibts da Lösungsansätze?

Gruß Matthias

jfheins 30. Jul 2012 13:43

AW: Komponenten ansprechen - mit ini konfigurieren
 
Also für die Properties Hint, Left und Top würde ja auch schon TControl ausreichen.
Falls du spezielle Properties benötigst und es trotzdem flexibel halten möchtest, käme vielleicht eine Collection in Frage.
(Das ist der Mechanismus hinter den dfm Dateien, die Komponenten können sich also bereits aus dem Text ihre Properties rauspflücken...)

Bummi 30. Jul 2012 13:46

AW: Komponenten ansprechen - mit ini konfigurieren
 
Mit RTTI in der Art von:

Delphi-Quellcode:
procedure TVorlage.LoadAndUseRestrinct(const fn:String);
var
  sl:TStringList;
  i:Integer;
  ClassNam,Prop,Info:String;

  Procedure ParseLine(const s:String);
    var
    st:String;
    begin
    st:=s;
    ClassNam:=Copy(st,1,pos(',',st)-1);
    st:=Copy(st,pos(',',st)+1,length(st));
    Prop:=Copy(st,1,pos(',',st)-1);
    Info:=Copy(st,pos(',',st)+1,length(st));
    end;
begin
  sl:=TStringList.Create;
  try
  sl.LoadFromFile(fn);
  for i:=0 to sl.Count - 1 do
    begin
    try
    ParseLine(sl[i]);
    SetProperty(Self,Classnam,Prop,Info);
    except end;
    end;
  finally
  sl.Free;
  end;
end;


Procedure SetProperty(Parent:TComponent;Const ClassNam,Prop:String;Info:Variant);
var
PropInfo: PPropInfo;
Kind:TTypeKind;
begin

if Parent.FindComponent(ClassNam)<>nil then
   begin

   PropInfo := GetPropInfo(Parent.FindComponent(ClassNam),Prop);
   if PropInfo<>nil then
     begin

     Kind:=PropInfo.PropType^.Kind;
     Case Kind of
        tkUnknown:;
        tkInteger:try
                  SetOrdProp(Parent.FindComponent(ClassNam),PropInfo.Name,StrToInt(Info));
                  except
                  SetPropValue(Parent.FindComponent(ClassNam),PropInfo.Name,GetColorForString(Info));
                  end;
        tkChar:  SetPropValue(Parent.FindComponent(ClassNam),PropInfo.Name,Info);
        tkEnumeration:SetEnumProp(Parent.FindComponent(ClassNam),PropInfo,Info);
        tkFloat: SetFloatProp(Parent.FindComponent(ClassNam),PropInfo,StrToFloat(Info));
        tkString: SetStrProp(Parent.FindComponent(ClassNam),PropInfo,Info);
        tkSet:   SetSetProp(Parent.FindComponent(ClassNam),PropInfo,Info);
        tkClass:;
        tkMethod:;
        tkWChar:   SetStrProp(Parent.FindComponent(ClassNam),PropInfo,Info);
        tkLString: SetStrProp(Parent.FindComponent(ClassNam),PropInfo,Info);
        tkWString: SetPropValue(Parent.FindComponent(ClassNam),PropInfo.Name,Info);
        tkVariant: SetVariantProp(Parent.FindComponent(ClassNam),PropInfo,Info);
        tkArray: ;
        tkRecord: ;
        tkInterface:;
        tkInt64:   SetInt64Prop(Parent.FindComponent(ClassNam),PropInfo,StrToInt64(Info));
        tkDynArray: ;
     end;
     end;
   end;
end;

matashen 30. Jul 2012 15:17

AW: Komponenten ansprechen - mit ini konfigurieren
 
Hallöchen,

danke Bummi das sieht sehr vielversprechend aus, werd ich mir zu gemüte führen.

Gruß Matthias

Sir Rufo 30. Jul 2012 17:08

AW: Komponenten ansprechen - mit ini konfigurieren
 
Kleiner Tipp zu den IniFiles

Den Index mit "objects" kannst du dir komplett sparen Delphi-Referenz durchsuchenTIniFile.ReadSections
Und selbst wenn, den Eintrag "count" kannst du dir auf jeden Fall sparen Delphi-Referenz durchsuchenTIniFile.ReadSection

Und das mit dem Index 0=... 1=... ist auch eher umständlich als hilfreich.
besser
Code:
[objects]
dot=1
irgendwas=1
der Wert kann dann als Boolean genutzt werden, ob dieser Eintrag aktiv ist, oder nicht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:01 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz