Autor Beitrag
Cash
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Di 18.03.03 22:48 
Hi@all

Kann man eigentlich einem shape eine Dreieckform zuweisen auch wenn es nicht unter den Eigenschaften steht?
Wenn ja wie?

_________________
Ein Sieger gewinnt etwas, ein 'Geschlagener' lernt eine Menge dazu :)
delphiDeveloper
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48



BeitragVerfasst: Mi 19.03.03 16:55 
Titel: Neue Kompo schreiben, die von TShape erbt
Speichere den Sourcecode unter MyShape.pas ab
und installiere die Komponente.
Ich hab lediglich den Typ TShapeType ergaenzt
und in der ovveride Methode paint ein primitives
Zeichen eines Dreiecks hinzugfügt.
Du Kannst das "Polygon zeichnen" nach deinen Wünschen
gestalten. Die Aufgage ein "Dreieck zeichnen" ist nicht so einfach
wie "Rechteck zeichnen" da die Winkel ja eine entscheidende Rolle
spielen. Man koennte sich aber vorstellen, dieses noch durch ein zusaetzliches eingefuehrtes Property zu loesen. Und dann in der Paint Methode entsprechend zu reagieren.

ausblenden volle Höhe Quelltext
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:
137:
138:
unit Myshape;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,Graphics;
 type
  TShapeType = (stRectangle, stSquare, stRoundRect, stRoundSquare,
    stEllipse, stCircle, stTriangle);  // Typ ergaenzt

  TMyshape = class(TShape)
  private
   FPen: TPen;
   FBrush: TBrush;
   FShape: TShapeType;
    { Private-Deklarationen }
     procedure SetShape(Value: TShapeType);
     procedure SetBrush(Value: TBrush);
     procedure SetPen(Value: TPen);

  protected
    { Protected-Deklarationen }
    procedure Paint; override;
  public
     constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    { Public-Deklarationen }
  published
      procedure StyleChanged(Sender: TObject);
     property Shape: TShapeType read FShape write SetShape default stRectangle;
     property Pen: TPen read FPen write SetPen;
     property Brush: TBrush read FBrush write SetBrush;
    { Published-Deklarationen }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Beispiele', [TMyshape]);
end;

{ TMyshape }

constructor TMyshape.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := ControlStyle + [csReplicatable];
  Width := 65;
  Height := 65;
  FPen := TPen.Create;
  FPen.OnChange := StyleChanged;
  FBrush := TBrush.Create;
  FBrush.OnChange := StyleChanged;
  FShape := stTriangle;
end;

destructor TMyshape.Destroy;
begin
  FPen.Free;
  FBrush.Free;
  inherited Destroy;
end;

procedure TMyshape.Paint;
  var
  X, Y, W, H, S: Integer;
begin
  with Canvas do
  begin
    Pen := FPen;
    Brush := FBrush;
    X := Pen.Width div 2;
    Y := X;
    W := Width - Pen.Width + 1;
    H := Height - Pen.Width + 1;
    if Pen.Width = 0 then
    begin
      Dec(W);
      Dec(H);
    end;
    if W < H then S := W else S := H;
    if FShape in [stSquare, stRoundSquare, stCircle] then
    begin
      Inc(X, (W - S) div 2);
      Inc(Y, (H - S) div 2);
      W := S;
      H := S;
    end;
    case FShape of
      stRectangle, stSquare:
        Rectangle(X, Y, X + W, Y + H);
      stRoundRect, stRoundSquare:
        RoundRect(X, Y, X + W, Y + H, S div 4, S div 4);
      stCircle, stEllipse:
        Ellipse(X, Y, X + W, Y + H);
    end;
    if FShape = stTriangle then
    begin
      // hier musst du dir deine Dreieckdefinition
      // definieren
      // dabei faellt auf man brauch ein zweites property
      // wie gleichschenklig, rechtwinkliges Dreieck etc.
      // ein Schnellschuß sieht so aus Polygon ...
      Polygon([Point(x, y), Point(x-(W div 2), y+(H div 2)),
               Point(X+W,Y), Point(X, Y)]);

    end;
  end;
end;

procedure TMyshape.SetBrush(Value: TBrush);
begin
  FBrush.Assign(Value);
end;

procedure TMyshape.SetPen(Value: TPen);
begin
  FPen.Assign(Value);
end;

procedure TMyshape.SetShape(Value: TShapeType);
begin
   if FShape <> Value then
  begin
    FShape := Value;
    Invalidate;
  end;
end;

procedure TMyshape.StyleChanged(Sender: TObject);
begin
  Invalidate;
end;

end.