RE:Pintar fila de un StringGrid
Para pintar las celdas de un StringGrid a total arbitrio es necesario asignar el evento OnDrawCell y usar los métodos del Canvas del StringGrid. Aquí va un ejemplo:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
TempRect: TRect;
begin
with Sender as TStringGrid do begin
if gdFixed in State then begin
Canvas.Brush.Color := FixedColor;
Canvas.Font.Color := clWindowText;
Canvas.Font.Style := [fsBold];
end else if gdSelected in State then begin
Canvas.Brush.Color := clHighlight;
Canvas.Font.Color := clHighlightText;
Canvas.Font.Style := [];
end else if ((ARow + FixedRows) And 1) <> 0 then begin
Canvas.Brush.Color := $00DDFFFF;
Canvas.Font.Color := clWindowText;
Canvas.Font.Style := [];
end else begin
Canvas.Brush.Color := $00DDEEFF;
Canvas.Font.Color := clWindowText;
Canvas.Font.Style := [];
end;
Canvas.FillRect(Rect);
Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, Cells[ACol,ARow]);
if gdFocused in State then
Canvas.DrawFocusRect(Rect)
else if (gdFixed in State) and Ctl3D then begin
DrawEdge(Canvas.Handle, Rect, BDR_RAISEDINNER, BF_RIGHT+BF_BOTTOM);
DrawEdge(Canvas.Handle, Rect, BDR_RAISEDINNER, BF_LEFT+BF_TOP);
end;
end;
end;
Como este código se encarga de pintar todas las celdas, la propiedad DefaultDrawing se debería poner en False, pues no tiene sentido que primero el componente dibuje las celdas y luego en el evento OnDrawCell las vuelva a dibujar encima.
El texto de las celdas se puede centrar o también alinear a la derecha modificando los parámetros en la llamada a TextRect:
Centrado:
CellText := Cells[ACol,ARow];
Canvas.TextRect(Rect, Rect.Left + (Rect.Right - Rect.Left -
Canvas.TextWidth(CellText) + 1) div 2, Rect.Top + 2, CellText);
Alineado a la derecha:
CellText := Cells[ACol,ARow];
Canvas.TextRect(Rect, Rect.Right - Canvas.TextWidth(CellText) - 2,
Rect.Top + 2, CellText);
Copyright (c) 2003 Ernesto De Spirito
http://www.latiumsoftware.com/es/index.php