Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Rechteck in stringgrid teilen und einfärben (https://www.delphipraxis.net/164629-rechteck-stringgrid-teilen-und-einfaerben.html)

Hartfrid Krause 22. Nov 2011 15:42

Rechteck in stringgrid teilen und einfärben
 
Ich möchte in einer stringgrid eine beliebige Zelle in drei gleichgroße Rechtecke aufteilen und diese kleineren Rechtecke mit verschiedenen Farben ausfüllen.
Dasselbe mit zwei Flächen (=Dreiecken) ist gelöst, aber die Erweiterung zu drei funktioniert nicht.

Für dreiecke färben habe ich eine Procedur:

PROCEDURE DreieckeFaerben(Sender:TObject;ACol,ARow:Integer;R ect:TRect;
State:TGridDrawState;col1,col2:Tcolor);
var pl:array of TPoint;
cr:TRect;
begin
with stringgrid1 do
begin
setlength(pl,4);
cr:=CellRect(acol,arow);
pl[0]:=cr.TopLeft;
pl[1].X:=cr.BottomRight.X;
pl[1].Y:=cr.TopLeft.Y;
pl[2]:=cr.BottomRight;
pl[3]:=pl[0];
canvas.Brush.Color:=col2;
canvas.Polygon(pl);
canvas.FloodFill(pl[1].x-2,pl[1].y+2,clwhite,fssurface);
pl[0]:=cr.TopLeft;
pl[1].X:=cr.TopLeft.X;
pl[1].Y:=cr.BottomRight.Y;
pl[2]:=cr.BottomRight;
pl[3]:=pl[0];
canvas.Brush.Color:=col1;
canvas.Polygon(pl);
canvas.FloodFill(pl[1].x+2,pl[1].y-2,clwhite,fssurface);
canvas.Brush.Style:=bsclear;
end;
end;

Soetwas entsprechendes für eine Dreiteilung such ich noch.

Bummi 22. Nov 2011 15:58

AW: Rechteck in stringgrid teilen und einfärben
 
Delphi-Quellcode:
procedure TForm2.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; aRect: TRect; State: TGridDrawState);
const
ColArray:Array[0..2] of TColor=(clblue,clLime,clRed);
var
i:Integer;
w:Integer;
begin
  with stringgrid1 do
  begin
    w := (aRect.Right - aRect.Left) div 3;
    for i := 0 to 2 do
        begin
        Canvas.Pen.Color := ColArray[i];
        Canvas.Brush.Color := ColArray[i];
        Canvas.Rectangle(Rect(aRect.Left + i*w,aRect.Top,aRect.Left + (i+1)*w,aRect.Bottom));
        end;
  end;
end;

shmia 22. Nov 2011 17:17

AW: Rechteck in stringgrid teilen und einfärben
 
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;

Hartfrid Krause 24. Nov 2011 13:35

AW: Rechteck in stringgrid teilen und einfärben
 
Hervorragend;
ich habe die procedure in meinen dreieckprocedur erfolgreich eingepasst.
danke

Hartfrid Krause 24. Nov 2011 13:42

AW: Rechteck in stringgrid teilen und einfärben
 
Noch ein kleines stringgrid-Problem.
Wie kann ich eine zweispaltige stringgrid so einrichten, dass
a. in der ersten Spalten keine Änderungen der Eingabe möglich ist und
b. in der 2. Spalte nur Zahleneingaben möglich sind (bei edit-feldern kann man ja mit keypressed arbeiten, was in einer strinngrid wohl nicht möglich ist)
c. die Returntaste sich nur auf die 2. Spalte bezieht, da ja hier die Eingaben erfolgen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:34 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz