Einzelnen Beitrag anzeigen

Muetze1
(Gast)

n/a Beiträge
 
#16

Re: Saubere Programmierung mit Delphi

  Alt 16. Mär 2008, 00:16
Zitat von Union:
Du setzt die Farbwerte nicht auf den Ursprungszustand zurück. Wenn jemand Deine Klasse benutzt und selber Zeichenoperationen auf dem Canvas durchführt, änderst Du einfach die Farben.
Das heisst du initialisierst niemals deine Farben vor Zeichenoperationen? Ich finde das vergebliche Mühe, da andere Zeichenoperationen fast immer die Farbe vor derer selber festlegen bzw. sogar unterschiedliche, von anderen Parametern abhängige Farben, festlegen.

Zitat von M. Hassmann:
parameter sollten als const übergeben werden in deinem constructor
Und lieferst du dazu auch eine Begründung?

Und nun noch mein Vorschlag:

Delphi-Quellcode:
unit UBackground;

interface

uses
  Forms, Graphics;

type
  TBackground = class
  private
    fBackgroundColor: TColor;
    fLineColor: TColor;
    fDistance: Integer;
  public
    procedure Horizontal(AForm: TForm);
    procedure Vertical(AForm: TForm);

    property BackgroundColor: TColor read fBackgroundColor write fBackgroundColor;
    property LineColor: TColor read fLineColor write fLineColor;
    property Distance: integer read fDistance write fDistance;
  end;

implementation

procedure TBackground.Horizontal(AForm: TForm);
var
  lWidth, lHeight: integer;
begin
  if assigned(AForm) then
  begin
    AForm.Color := fBackgroundColor;
    AForm.Canvas.Pen.Color := fLineColor;

    lWidth := AForm.ClientWidth;
    lHeight := AForm.ClientHeight;
    while lHeight >= 0 do
    begin
      AForm.Canvas.MoveTo(0, lHeight);
      AForm.Canvas.LineTo(lWidth, lHeight);

      Dec(lWidth, fDistance);
    end;
  end;
end;

procedure TBackground.Vertical(AForm: TForm);
var
  lWidth, lHeight: Integer;
begin
  if assigned(AForm) then
  begin
    AForm.Color := fBackgroundColor;
    AForm.Canvas.Pen.Color := fLineColor;

    lHeight := AForm.ClientHeight;
    lWidth := AForm.ClientWidth;
    while lWidth >= 0 do
    begin
      AForm.Canvas.MoveTo(lWidth, 0);
      AForm.Canvas.LineTo(lWidth, lHeight);

      Dec(lWidth, fDistance);
    end;
  end;
end;

end.
  Mit Zitat antworten Zitat