Home » Tipps & Tricks » Komponenten » TStringGrid » Raster im StringGrid erzeugen

Raster im StringGrid erzeugen

//Matrix im StringGrid zeichnen
//M=Zeilenanzahl
//N=Spaltenanzahl
procedure DrawMatrix(Grid: TStringGrid; ACol, ARow, M, N: Integer; LineColor: TColor;
   LineWidth: Integer; State: TGridDrawState; Sender: TObject; Rect: TRect);
begin
  //Horizontale Linien ohne Fixed Spalten
  if ((ARow-Grid.FixedRows+1) MOD M = 0) AND NOT (gdFixed In State) Then
  begin
    with (sender as TStringGrid).Canvas do
    begin
      Pen.Color := LineColor;
      Pen.Width := LineWidth;
      Pen.Style := psSolid;
      MoveTo(Rect.Left-1,Rect.Bottom);
      LineTo(Rect.Right-1,Rect.Bottom);
    end;
  end;
  //Vertikale Linien ohne Fixed Zeilen
  if ((ACol-Grid.FixedCols+1) MOD N = 0) AND NOT (gdFixed In State) Then
  begin
    with (sender as TStringGrid).Canvas do
    begin
      Pen.Color := LineColor;
      Pen.Width := LineWidth;
      Pen.Style := psSolid;
      MoveTo(Rect.Right,Rect.Top-1);
      LineTo(Rect.Right,Rect.Bottom-1);
    end;
  end;
end;

Beispiel-Aufruf:

//Procedure im OnDrawCell des Grid aufrufen
procedure TForm1.GridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  //5X5 Matrix zeichnen
  DrawMatrix(Grid, ACol, ARow, 5, 5, clBlack, 2, State, Sender, Rect);
end;