Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   formato celda en stringrid (https://www.clubdelphi.com/foros/showthread.php?t=89510)

albelg 03-12-2015 19:44:49

formato celda en stringrid
 
hola colegas del club. Necesito me ayuden en esto si es posible. Lo q quiero es darle un color x a una celda especifica dentro del stringrid.
Hasta ahora con:

Código Delphi [-]
stringgrid1.color:=clred

pongo todo el stringrid en rojo pero necesito q solo sea a una celda especifica. gracias de antemano

Caminante 03-12-2015 19:55:26

Hola

Busca en los foros por drawcolumncell


Saludos

ecfisa 03-12-2015 22:54:58

Hola abelg.

Como bien te indica Caminante, el evento que debes usar es OnDrawCell.

Por otro lado, la clase TStringGrid posee la propiedad vectorial Objects que permite asociar un Objeto a cada celda, lo que la hace el candidato ideal para almacenar los colores de cada una de ellas.

Ejemplo:
Código Delphi [-]
...
implementation 

var
  ColRow: TPoint;

// Inicializar
procedure TForm1.FormCreate(Sender: TObject);
var
  sg : TStringGrid;
  c, r: Integer;
begin
  sg := StringGrid1;
  for r := sg.FixedRows to sg.RowCount-1 do
    for c := sg.FixedCols to sg.ColCount - 1 do
    begin
      sg.Cells[c,r]   := Format('Cell[%d,%d]',[c, r]);
      sg.Objects[c,r] := TObject(sg.Color);
    end;
end;

// Pintar celdas
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  sg: TStringGrid;
begin
  sg := TStringGrid(Sender);
  if (ACol >= sg.FixedCols) and (ARow >= sg.FixedRows) then
  begin
    sg.Canvas.Brush.Color := TColor(sg.Objects[ACol, ARow]);
    sg.Canvas.FillRect(Rect);
    sg.Canvas.TextOut(Rect.Left, Rect.Top+2, sg.Cells[ACol, ARow]);
  end;
end;

// Celda actual bajo el mouse
procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  TStringGrid(Sender).MouseToCell(X,Y, ColRow.X, ColRow.Y);
end;

// Seleccionar color
procedure TForm1.StringGrid1DblClick(Sender: TObject);
var
  sg: TStringGrid;
begin
  sg := TStringGrid(Sender);
  if (ColRow.X >= sg.FixedCols) and (ColRow.Y >= sg.FixedRows) then
    if ColorDialog1.Execute then
      sg.Objects[sg.Col,sg.Row] := TObject(ColorDialog1.Color);
end;

Muestra:


Saludos :)

albelg 04-12-2015 14:15:31

ok, muchas gracias a los dos


La franja horaria es GMT +2. Ahora son las 10:41:18.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi