Ich arbeite gerade an einer Komponente, die eine selbstdefinierte Klasse als property besitzt.
Zum bearbeiten dieser Eigenschaft habe ich einen Property-Editor gebaut.
Wenn ich zur Entwurfszeit die eigenschaft mit Hilfe des Editors änder, so werden die Änderungen im Formular sichtbar.
Starte ich jedoch das Programm, so wird die Komponente anscheinend neu initialisiert. Daraus folgt, daß die im constructor definierten Standardeinstellungen der Komponente benutzt werden.
Wie muß ich die Komponente verändern, damit die im OE gemachten Änderungen beibehalten werden ?
Hier ein vereinfachtes Beispiel meines Codes:
Code:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136:
| unit Indicator;
interface
uses Messages, Windows, SysUtils, Classes, Extctrls, Controls, Graphics, DsgnIntf, Dialogs, Unit2; type TGradient = record Color : TColor; Pos : Real; end; TGradientArray = array of TGradient;
TGradients = class(TObject) public Gradients : TGradientArray; end;
TIndicator = class(TCustomControl) private FGradients : TGradients; procedure SetGradients(Value : TGradients); protected procedure Paint; override; public constructor Create(AOwner:TComponent); override; destructor Destroy; override; published property Gradients : TGradients read FGradients write SetGradients; end;
TGradientsEditor = Class(TPropertyEditor) Function GetAttributes : TPropertyAttributes; Override; Function GetValue : String; Override; Procedure Edit; Override; end;
procedure Register;
implementation
procedure TIndicator.SetGradients(Value : TGradients); begin SetLength(FGradients.Gradients, Length(Value.Gradients)); FGradients.Gradients := copy(Value.Gradients,0,High(Value.Gradients)); end;
constructor TIndicator.Create(AOwner:TComponent); var BGradients : TGradientArray; begin inherited Create(AOwner); Height := 200; Width := 100; FGradients := TGradients.Create; SetLength(BGradients,2); BGradients[0].Color := clBlue; BGradients[0].Pos := 0; BGradients[1].Color := clRed; BGradients[1].Pos := 5; FGradients.Gradients := BGradients; BGradients := nil; end;
destructor TIndicator.Destroy; begin FGradients.Free; inherited Destroy; end;
procedure TIndicator.Paint; var Rect: TRect; begin Canvas.Pen.Mode := pmCopy; Canvas.Brush.Style := bsSolid; Canvas.CopyMode := cmSrcCopy ; Rect := GetClientRect; Canvas.brush.Color := FGradients.Gradients[0].color; Canvas.FillRect(Rect); Rect.Bottom := (Rect.Bottom - Rect.Top) div 2; Canvas.brush.Color := FGradients.Gradients[1].color; canvas.FillRect(Rect); end;
function TGradientsEditor.GetAttributes : TPropertyAttributes; begin Result := inherited GetAttributes + [paDialog] +[paSubProperties]+[paMultiSelect]; end;
function TGradientsEditor.GetValue : String; begin FmtStr(Result, '(%s)', [ GetPropType^.Name ]); end;
procedure TGradientsEditor.Edit; var BGradientsDlg: TGradientsDlg; BGradients : TGradients; BMin,BMax :Single; Count : Integer; P:TPoint; begin BGradientsDlg := TGradientsDlg.Create(nil); with BGradientsDlg do begin GetCursorPos(P); Left:=P.X; Top:=P.Y; if ShowModal = mrOK then begin for Count:=0 to PropCount-1 do begin BGradients := TGradients(GetOrdValueAt(Count)); try BGradients.Gradients[0].Color := clGreen; BGradients.Gradients[1].Color := clYellow; except end; end; end; Free; end; end; procedure Register; begin RegisterComponents('Marx', [TIndicator]); RegisterPropertyEditor(TypeInfo(TGradients),TIndicator,'Gradients',TGradientsEditor); end;
end. |
Moderiert von
Aya: Delphi-Tags hinzugefügt.