Autor Beitrag
JRegier
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1268

Win XP Home, Prof, 2003 Server
D6 Enterprise
BeitragVerfasst: So 26.06.05 09:29 
Hallo, ich will Font.Style als Zahl in DB speichern!

Nun ich kann bei einem SET Ord(SET) abfragen!
Wie ist es bei Style geht ja nicht Ord(Style)?

Muß ich mit
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
if Style = [fsBold,fsItalic] then
... = 3
else
if Style = [fsItalic] then
... = 2
else
if Style = [fsBold] then
... = 1
else
... = 0


machen ? Ich würde es kürze fassen wollen! Wie ginge es anders?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 26.06.05 11:12 
Delphi hat an der Stelle einen kleinen Bug. Rein syntaktisch wäre zwar Integer(Font.Style) korrekt. Für den Delphi-Kompiler ist aber nur ein Umweg möglich:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
var
    Value: PInteger;
begin
    Value := @Font.Style;
    IntValue := Value^;


Eine Andere Möglichkeit sieht so aus:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
var
    Style: TFontStyle;
    Value: Integer absolute FontStyle;
begin
    Style := @Font.Style;
    IntValue := Value;


Erstere Möglichkeit ist jedoch gebräuchlicher ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
JRegier Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1268

Win XP Home, Prof, 2003 Server
D6 Enterprise
BeitragVerfasst: So 26.06.05 11:19 
user profile iconBenBE hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
var Value: PInteger;
begin
    Value := @Font.Style;
    IntValue := Value^;



Aber das sind ja Adress-Operatoren wird der Wert nicht immer unterschiedlich sein?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 26.06.05 11:25 
Ne, für als Record deklarierte Properties geht das so ...
Hab aber grad nochmal nachgeguckt ... TFont.Style ist über Get-Funktion implementiert :arrow: Nutzung der zweiten Variante oder zwischenspeichern des Wertes.

@Adress-Operationen: Man beachte, dass ich in Zeile 4 den Wert dereferenziere. Das hat schon so seine Richtigkeit (in der Hinsicht weiß ich, was ich tu ;-))

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 27.06.05 10:14 
Hallo,

ich hatte mal ein ähnliches Problem,
das war meine Lösung:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
//...
uses TypInfo;
//...
function FontStyleInt(aObj: TObject): Integer;
var PropInfo : PPropInfo;
begin
  result := 0;
  PropInfo := GetPropInfo(aObj.ClassInfo,'Font');
  aObj := TObject(GetOrdprop(aObj,PropInfo));
  PropInfo := GetPropInfo(aObj.ClassInfo,'Style');
  result:= GetOrdprop(aObj,PropInfo);
end;
//...
showmessage(IntToStr(FontStyleInt(TreeView1)));

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mo 27.06.05 10:29 
Hallo,

so geht's auch:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
var fs:Integer;
...
fs:=Integer(TFontStyle(Font.Style));
...
...
// in die andere Richtung
Font.Style:=TFontStyles(TFontStyle(fs));
...

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 27.06.05 11:23 
Hallo,

super :)
in der folgenden Form funktioniert es einwandfrei
ausblenden Delphi-Quelltext
1:
fs:=Integer(TFontStyle(TreeView1.Font.Style));					

wie müsste ich da vorgehen um es über eine Funktion für alle Componenten nutzbar zu machen?

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mo 27.06.05 11:38 
Hallo,

ich verstehe Deine Frage nicht ganz. Meinst Du so?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
function FontStyle2Int(aStyle:TFontStyle):Integer; overload;
function FontStyle2Int(aFont:TFont):Integer; overload;
...
...
...
function FontStyle2Int(aStyle:TFontStyle):Integer;
begin
  Result:=Integer(TFontStyle(aStyle));  
end// FontStyle2Int

function FontStyle2Int(aFont:TFont):Integer;
begin
  Result:=Integer(TFontStyle(aFont.Style));  
end// FontStyle2Int

...
...
fs:=FontStyle2Int(TreeView1.Font.Style);
// oder
fs:=FontStyle2Int(TreeView1.Font);
...

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 27.06.05 12:23 
Hallo,

meinte das in der Art meiner Funktion, etwa so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function FontStyleInt(aObj: TObject): Integer;
begin
  result := Integer(TFontStyle(aObj.Font.Style)); 
end;
//...
showmessage(IntToStr(FontStyleInt(TreeView1)));

dann habe ich aber einen undefinierten Bezeichner 'Font'

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
JRegier Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1268

Win XP Home, Prof, 2003 Server
D6 Enterprise
BeitragVerfasst: Mo 27.06.05 12:36 
Vielen Dank Jungs! Ihr seid Klasse!
Knaecke
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 30

WIN 2000, WIN XP, Gentoo
D5 Enterprise
BeitragVerfasst: Mi 17.08.05 14:17 
Ich hab auch gerade ein ähnliches Problem:

Ich möchte die Styles von Strings im FontDialog einstellen und später in einer Datei als Integer speichern. Wenn ich aber
ausblenden Quelltext
1:
((p.obj as CVirtualElement).Entity as CIOV_E_Text).Style := Integer(TFontStyle(frmEditText.FontDialog.Font.Style));					

angebe, dann bekomm ich vom Compiler "Ungültige Typumwandlung" an den Kopf geschmissen (der L-Wert ist vom Typ Integer, der Cursor springt auch an der rechten Seite bis zum Semikolon).

Probier ich die Variante mit Pointern wie von BenBE oben beschrieben, also
ausblenden Quelltext
1:
2:
3:
4:
var
  dummy: PInteger;
begin
  dummy := @frmEditText.FontDialog.Font.Style;

dann meldet er mir "Variable erforderlich". Bin ich einfach nur zu blöd, den Fehler zu sehen oder ist das irgendwas Verstecktes?

_________________
"Kekskrümel im Bett pieksen weniger, wenn man vorher darauf uriniert", meinte mal ein Kumpel... ;)
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 17.08.05 15:14 
Versuch mal den Weg über die Funktion Ord. Um alle in diesem Thread versteckten Compiler-Annahmen und Gültigkeitsbedingungen aufzulisten könnte man ein Buch füllen. Ansonsten probier die zweite Variante aus meinem Post aus (die mit der Absolute-Anweisung). Dort musst Du aber FontStyle --> Style ersetzen, Fehler von mir ...

Ansonsten schau Dir einfach mal noch die anderen Lösungsmöglichkeiten hier im Thread an. Die sollten gegenüber meiner Variante für unerfahrene bevorzugt werden.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Knaecke
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 30

WIN 2000, WIN XP, Gentoo
D5 Enterprise
BeitragVerfasst: Mi 17.08.05 15:39 
Danke für die Tipps Namensvetter (bin auch ein Benny ;) ) aber Deine zweite Version
ausblenden Quelltext
1:
2:
3:
4:
5:
var    
    Style: TFontStyle;
    Value: Integer absolute Style;
begin
    Style := @frmEditText.FontDialog.Font.Style;


bringt mir wieder ein "Variable erforderlich", bei Ord() hab ich
ausblenden Quelltext
1:
Ord(frmEditText.FontDialog.Font.Style);					

und auch
ausblenden Quelltext
1:
Integer(Ord(frmEditText.FontDialog.Font.Style));					

probiert, beides wieder mit dem Ergebnis "Inkompatible Typen". :(

Dabei ist doch bei mir nix anders als oben oder? Nur dass ich halt über den FontDialog auf Font.Style zugreifen will und nicht über ein Memo oder so... Alles andere (Fontdialog.Font.Size, Fontdialog.Font.Name, ...) funktioniert ja auch ohne Probleme...

_________________
"Kekskrümel im Bett pieksen weniger, wenn man vorher darauf uriniert", meinte mal ein Kumpel... ;)
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 17.08.05 18:05 
Versuch mal Integer(TFontStyle(frmEditText.FontDialog.Font.Style));. Das müsste IIRC so funzen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Knaecke
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 30

WIN 2000, WIN XP, Gentoo
D5 Enterprise
BeitragVerfasst: Do 18.08.05 08:51 
Das hatte ich ja als Erstes probiert, hab ich ja oben schon geschrieben :(

Naja, wenn es weiterhin nicht klappt, werd ich wohl ne neue Dateiversionsnummer machen müssen und dann das Ganze halt auch als Set abspeichern und nicht (wie bereits implementiert) als Integer. *schnief*

Aber ich setz mich erstmal an andere Sachen, vielleicht fällt ja irgendwem (oder auch mir) noch was ein...

_________________
"Kekskrümel im Bett pieksen weniger, wenn man vorher darauf uriniert", meinte mal ein Kumpel... ;)
JRegier Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1268

Win XP Home, Prof, 2003 Server
D6 Enterprise
BeitragVerfasst: Do 18.08.05 14:42 
user profile iconKnaecke hat folgendes geschrieben:
Das hatte ich ja als Erstes probiert, hab ich ja oben schon geschrieben :(

Naja, wenn es weiterhin nicht klappt, werd ich wohl ne neue Dateiversionsnummer machen müssen und dann das Ganze halt auch als Set abspeichern und nicht (wie bereits implementiert) als Integer. *schnief*

Aber ich setz mich erstmal an andere Sachen, vielleicht fällt ja irgendwem (oder auch mir) noch was ein...


user profile iconsmiegel hat folgendes geschrieben:
Hallo,

so geht's auch:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
var fs:Integer;
...
fs:=Integer(TFontStyle(Font.Style));
...
...
// in die andere Richtung
Font.Style:=TFontStyles(TFontStyle(fs));
...


Das hier klappt doch schon wunderbar mir langt schon die Antwort! Vielen Dank!
Knaecke
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 30

WIN 2000, WIN XP, Gentoo
D5 Enterprise
BeitragVerfasst: Do 18.08.05 15:41 
Danke für den Hinweis, aber nun zum dritten Mal: Es geht nicht... :roll:

Ich bekomm vom Compiler die Meldung "Ungültige Typumwandlung".


Selbst direkt im Formular hab ich mir ja nun schon mal spaßeshalber eine Dummy-Variable angelegt, nur um es auf dem kürzesten Weg zu testen.. aber selbst da funzt es nicht. (Ungültige Typumwandlung). Hier der Code:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TfrmEditText.btnFontClick(Sender: TObject);
var
  dummy: Integer;
begin
  if FontDialog.Execute then begin
    dummy := Integer(TFontStyle(FontDialog.Font.Style));
  end;
end;



Wenn es so einfach wäre, würd ich ja hier nicht um Hilfe schreien ;) Könnte es evtl. ein Problem vom Compiler sein?

_________________
"Kekskrümel im Bett pieksen weniger, wenn man vorher darauf uriniert", meinte mal ein Kumpel... ;)
JRegier Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1268

Win XP Home, Prof, 2003 Server
D6 Enterprise
BeitragVerfasst: Do 18.08.05 15:45 
user profile iconKnaecke hat folgendes geschrieben:
Danke für den Hinweis, aber nun zum dritten Mal: Es geht nicht... :roll:

Ich bekomm vom Compiler die Meldung "Ungültige Typumwandlung".


Selbst direkt im Formular hab ich mir ja nun schon mal spaßeshalber eine Dummy-Variable angelegt, nur um es auf dem kürzesten Weg zu testen.. aber selbst da funzt es nicht. (Ungültige Typumwandlung). Hier der Code:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TfrmEditText.btnFontClick(Sender: TObject);
var
  dummy: Integer;
begin
  if FontDialog.Execute then begin
    dummy := Integer(TFontStyle(FontDialog.Font.Style));
  end;
end;



Wenn es so einfach wäre, würd ich ja hier nicht um Hilfe schreien ;) Könnte es evtl. ein Problem vom Compiler sein?


Ich habe Delphi 6.0 und es funzt einwandfrei!
SMO
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 120
Erhaltene Danke: 18


D2005 Personal
BeitragVerfasst: Do 18.08.05 16:27 
Oh je, was ist denn hier los. Wildes Durcheinander. Dabei ist die Lösung relativ einfach. :)

user profile iconBenBE hat folgendes geschrieben:
Delphi hat an der Stelle einen kleinen Bug. Rein syntaktisch wäre zwar Integer(Font.Style) korrekt. Für den Delphi-Kompiler ist aber nur ein Umweg möglich:


Stimmt nicht ganz. Zuersteinmal ist die Eigenschaften "Font.Style" ein Set (eine Menge) und zwar vom Typ TFontStyles und nicht TFontStyle, was hier die ganze Zeit benutzt wird!

Die Datentypen sind folgendermaßen deklariert:
ausblenden Delphi-Quelltext
1:
2:
TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);
TFontStyles = set of TFontStyle;

Beide Datentypen sind jeweils nur 1 Byte groß. Das kann gerne mit SizeOf() nachgeprüft werden. Es ist zwar etwas doof vom Delphi-Compiler, dass er Ord() nicht auf Sets anwenden kann (was vielleicht daran liegt, dass ein Set bis zu 32 Bytes groß sein kann und es noch keinen ordinalen Datentyp gibt, der eine Zahl dieser Größe aufnehmen könnte). Aber da nunmal TFontStyles genau 1 Byte groß ist, funktioniert der Typecast Value := Byte(Font.Style) problemlos, im Gegensatz zu Integer(). ;)
Ist IMHO auch schöner und sinnvoller als Integer(TFontStyle(Font.Style)); Das Set auf seine zugrundeliegende Enumeration casten? Igitt. Geht auch nur, wenn die Datentypen gleich groß sind.

user profile iconKnaecke hat folgendes geschrieben:
Danke für den Hinweis, aber nun zum dritten Mal: Es geht nicht... :roll:

Ich bekomm vom Compiler die Meldung "Ungültige Typumwandlung".

Dann sind bei dir in Delphi 5 wahrscheinlich die Datentypen TFontStyle und TFontStyles unterschiedlich groß. Ermittle einfach die Größe von TFontStyles mit SizeOf(TFontStyles). Wenn sie 1 ist benutze Byte(Font.Style);, bei 2 benutze Word(Font.Style); und bei 4 Longword(Font.Style);.
Knaecke
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 30

WIN 2000, WIN XP, Gentoo
D5 Enterprise
BeitragVerfasst: Do 18.08.05 17:07 
user profile iconSMO hat folgendes geschrieben:
Zuersteinmal ist die Eigenschaften "Font.Style" ein Set (eine Menge) und zwar vom Typ TFontStyles und nicht TFontStyle, was hier die ganze Zeit benutzt wird!

Das hatte ich auch schon gesehen und mich gewundert, dass es trotzdem geht (zumindest bei den anderen). Aber so tief steck in Delphi noch nicht drin, komm aus der C++ und Java-Ecke... Keine Ahnung was es hier noch so an "Features" gibt und wie man am besten wohin castet etc. ;)

Vielen Dank für deine Antwort SMO, die hat dann wirklich geholfen.

Für alle anderen hier noch der umgedrehte Weg:
ausblenden Quelltext
1:
Font.Style := TFontStyles(Byte(value));					

_________________
"Kekskrümel im Bett pieksen weniger, wenn man vorher darauf uriniert", meinte mal ein Kumpel... ;)