![]() |
Delphi-Version: 2010
SETs unterschiedlicher Grösse kombinieren
Ich hab ein Problem eine Komponente so hinzubasteln, dass sie unter D2007 und D2010 funktioniert.
In Delphi2007 lief folgender Code problemlos:
Delphi-Quellcode:
In Delphi2010 funktioniert's nicht mehr, da TShiftState ein paar Zustände dazugekriegt hat und jetzt 2 Byte gross ist.
type TSx_ShiftKeys=set of (skShift, skAlt, skCtrl); // subset of TShiftState
procedure SetZoomShift(const V:TSx_ShiftKeys); var fZoomShift: TShiftState; begin fZoomShift:=[ssLeft]+TShiftState(V); ... Wie könnt ich das vernünftig lösen? Das Set brauch ich wieder, damit die Komponente im Design-mode vernünftig konfigurierbar ist. So eine Lösung
Delphi-Quellcode:
kommt mir völlig behindert vor - und ich müsst erst wieder ein define + Alternativcode herumwickeln damit alles auch unter D2007 läuft.
fZoomShift:=[ssLeft];
w:=word(fZoomShift) or byte(v); fZoomShift:=TShiftState(w); Danke! Ralf |
AW: SETs unterschiedlicher Grösse kombinieren
Ich würde es wie folgt machen:
Delphi-Quellcode:
Noch schöner wäre statt des
type
TSx_ShiftKey = (skShift, skAlt, skCtrl); TSx_ShiftKeys = set of TSx_ShiftKey; function Sx_ShiftKeysToShiftState(const V: TSx_ShiftKeys): TShiftState; var sk: TSx_ShiftKey; begin Result := []; for sk in V do begin case sk of skShift: Include(Result, ssShift); skAlt: Include(Result, ssAlt); skCtrl: Include(Result, ssCtrl); end; end; end; procedure SetZoomShift(const V: TSx_ShiftKeys); var fZoomShift: TShiftState; begin fZoomShift := Sx_ShiftKeysToShiftState(V); Include(fZoomShift, ssLeft); end;
Delphi-Quellcode:
ein konstantes Zuordnungsarray, aber da die VCL-Schreiberlinge ungeschickterweise den Elementtyp von TShiftState (wie du den von TSx_ShiftKeys :P) namenlos gelassen haben, geht das nicht (zumindest ohne Casterei).
case
|
AW: SETs unterschiedlicher Grösse kombinieren
Hallo,
Zitat:
Delphi-Quellcode:
Wenn du TSomeShiftKeys schon bei der Definition der Eigenschaft verwendest anstatt neue Bezeichner für die Werte des (eingeschränkten) Aufzählungstypen einzuführen, könnte die Routine SetZoomShift so aussehen:
type
TSx_ShiftKey = (skShift, skAlt, skCtrl); TSx_ShiftKeys = set of TSx_ShiftKey; TSomeShiftKey = ssShift..ssCtrl; TSomeShiftKeys = set of TSomeShiftKey; function Sx_ShiftKeysToShiftState (const V: TSx_ShiftKeys): TShiftState; const Map: array [TSx_ShiftKey] of TSomeShiftKey = (ssShift, ssAlt, ssCtrl); var sk: TSx_ShiftKey; begin Result := []; for sk in V do Include (Result, Map[sk]); end; procedure SetZoomShift(const V: TSx_ShiftKeys); var fZoomShift: TShiftState; begin fZoomShift := Sx_ShiftKeysToShiftState(V); Include(fZoomShift, ssLeft); end;
Delphi-Quellcode:
Gruß Hawkeye
procedure SetZoomShift (const V: TSomeShiftKeys);
var fZoomShift: TShiftState; begin fZoomShift := V + [ssLeft]; end; |
AW: SETs unterschiedlicher Grösse kombinieren
Zitat:
Zitat:
|
AW: SETs unterschiedlicher Grösse kombinieren
Wie wäre es damit?
Delphi-Quellcode:
Und dann eventuell noch sowas.
type
TZoomShiftKeys = type TShiftState;
Delphi-Quellcode:
const
AllowedZoomKeys = [ssShift..ssCtrl]; |
AW: SETs unterschiedlicher Grösse kombinieren
Zitat:
Zitat:
Eine fragwürdige Alternative hab ich am WE noch gebastelt:
Delphi-Quellcode:
{$IF Compilerversion<=18.5} // Bei D2007 hat's noch funktioniert
type TSx_ShiftKeys=set of (skShift, skAlt, skCtrl); {$ELSE} type TSx_ShiftKeys=set of (skShift, skAlt, skCtrl, _sk_IgnoreMe = (sizeof(TShiftState)*8)-1 ); // to make set same size as TShiftKey {$IFEND} |
AW: SETs unterschiedlicher Grösse kombinieren
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:37 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