Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#3

AW: Rechteck in stringgrid teilen und einfärben

  Alt 22. Nov 2011, 17:17
Ich werfe mal meine Prozedure DrawBiColorRect in die Runde.
Damit wird gezeigt, wie man das Problem unabhängig vom StringGrid und Formular verallgemeinert
und wiederverwendbar lösen kann.
Eine saubere Sache, denn es wird der Canvas und das Zielrechteck übergeben
und ob dahinter ein Stringgrid, DBGrid, Image oder was auch immer steht spielt keine Rolle mehr.

Danach sollte die Eigenentwicklung einer Prozedure die 3 Farben benützt kein Problem mehr sein.

Delphi-Quellcode:
// Zeichnet ein zweifarbiges Recheck auf einen Canvas
// mode:
// horizontal geteilt -
// vertikal geteilt |
// diagonal geteilt /
// diagonal geteilt \
procedure DrawBiColorRect(canvas:TCanvas; r:TRect; c1, c2:TColor; mode:char);
var
   rect1, rect2 : TRect;
   poly1, poly2 : array[0..2] of TPoint;

   procedure DrawRects;
   begin
      canvas.Brush.Color := c1;
      canvas.FillRect(rect1);
      canvas.Brush.Color := c2;
      canvas.FillRect(rect2);
   end;

   procedure DrawPoly;
   begin
      canvas.Brush.Color := c1;
      canvas.Pen.Color := c1;
      canvas.Polygon(poly1);
      canvas.Brush.Color := c2;
      canvas.Pen.Color := c2;
      canvas.Polygon(poly2);
   end;
begin
   case mode of
      '|':
      begin
         rect1 := r;
         rect2 := r;
         rect1.Right := ((rect1.Right-rect1.Left) div 2) + rect1.Left;
         rect2.Left := rect1.Right;
         DrawRects;
      end;

      '-':
      begin
         rect1 := r;
         rect2 := r;
         rect1.Bottom := ((rect1.Bottom-rect1.Top) div 2) + rect1.Top;
         rect2.Top := rect1.Bottom;
         DrawRects;
      end;

      '/':
      begin
         poly1[0] := r.TopLeft;
         poly1[1] := Point(r.Right, r.Top);
         poly1[2] := Point(r.Left, r.Bottom);
         poly2[0] := r.BottomRight;
         poly2[1] := Point(r.Right, r.Top);
         poly2[2] := Point(r.Left, r.Bottom);
         DrawPoly;
      end;

      '\':
      begin
         poly1[0] := r.TopLeft;
         poly1[1] := Point(r.Right, r.Top);
         poly1[2] := Point(r.Right, r.Bottom);
         poly2[0] := r.BottomRight;
         poly2[1] := Point(r.Left, r.Top);
         poly2[2] := Point(r.Left, r.Bottom);
         DrawPoly;
      end;
   end;
end;
Andreas

Geändert von shmia (22. Nov 2011 um 17:19 Uhr)
  Mit Zitat antworten Zitat