Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Grilla con colores (https://www.clubdelphi.com/foros/showthread.php?t=90608)

santiago14 26-07-2016 14:34:25

Grilla con colores
 
1 Archivos Adjunto(s)
Buenas, estoy con un cliente que quiere que en la grilla (TStringGrid) de datos se vea como en el adjunto.
Lo explico, la grilla tiene la fila seleccionada en un color (azul por ejemplo), y la celda actual en otro color (amarillo...) No he podido lograr el efecto y el tipo rompe las b... noche y día.

Gracias.

BDWONG 26-07-2016 18:38:27

Hola buscando me econtre un enlace donde muestra como pintar una celda en especifico aqui te lo dejo http://stackoverflow.com/questions/6...in-string-grid


La prueba esta hecha en Lazarus 1.6 pero seguro servira igual en Delphi
Bueno y en base a la respuesta del usuario @RRUZ pues hize esto:

Código Delphi [-]
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);
var
  i,j:Integer;
begin

  if (ACol >=1) and (ARow =StringGrid1.Row) then
    with TStringGrid(Sender) do
    begin
      //paint the background Green
      Canvas.Brush.Color := clBlue;
      Canvas.FillRect(aRect);
      Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[ACol, ARow]);
    end;

  if (ACol = StringGrid1.Col) and (ARow = StringGrid1.Row) then
    with TStringGrid(Sender) do
    begin
      //paint the background Green
      Canvas.Brush.Color := clYellow;
      Canvas.FillRect(aRect);
      Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[ACol, ARow]);
    end;
end;

Ejemplo:
https://www.youtube.com/watch?v=x5kb...ature=youtu.be

BDWONG 27-07-2016 01:09:56

Hay un problema todo funciona bien hasta que se selecciona una celda gris ahi todo falla estuve buscando el porque sucede pero no lo logro allar
pero bueno el codigo anterior trabaja parcialmente, espero tu lo puedas corregir pero aun asi voy a seguir intentado.
Saludos....

santiago14 27-07-2016 01:32:57

Ok. Veo el fuente.


Enviado desde mi iPhone utilizando Tapatalk

BDWONG 28-07-2016 00:33:48

Hola después de investigar y preguntar he dado con la solución, cabe aclarar que todo estas pruebas están hechas en lazarus 1.6 pero supongo que funcionara de manera muy similar en Delphi aunque claro puede haber diferencias.

Primero como mencione se necesita utilizar el evento onDrawCell del stringGrid ahi colocamos lo siguiente
Código Delphi [-]
if (ACol >=1) and (ARow =StringGrid1.Row) then
    with TStringGrid(Sender) do
    begin
      //paint the background Green
      Canvas.Brush.Color := clBlue;
      Canvas.FillRect(aRect);
      Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[ACol, ARow]);
    end;

  if (ACol = StringGrid1.Col) and (ARow = StringGrid1.Row) then
    with TStringGrid(Sender) do
    begin
      //paint the background Green
      Canvas.Brush.Color := clYellow;
      Canvas.FillRect(aRect);
      Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[ACol, ARow]);
    end;

El primer if valida que no se pinte las celdas estaticas (las que esta en gris) y solo pinte las celdas que se encuentren en la fila seleccionada por el usuario, el segundo if se encarga de cambiar el color a la celda que tenga el foco,
Como mencione arriba había el código tenia unos problemas al momento de de seleccionar las celdas estáticas ya que después no pintaban de manera correcta no se si el problema sea único de Lazarus o también es similar en Delphi.

Video donde muestro el error: https://www.youtube.com/watch?v=xu3t...ature=youtu.be

Para corregirlo basta con llamar a la propiedad invalidate que lo que hace es informar que se necesita repintar el stringGrid este propiedad se llama dentro del evento OnSelectCell o OnSelection ambas funciona.

En mi caso lo puse en OnSelectCell

Código Delphi [-]
procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
  var CanSelect: Boolean);
begin
  StringGrid1.Invalidate;

end;

Agradezco al foro de Lazarus por su ayuda http://forum.lazarus.freepascal.org/...c,33490.0.html
Me pareció interesante publicarlo acá por si otros tiene el mismo problema.

Saludos.....:cool:

Casimiro Notevi 28-07-2016 01:08:53

^\||/^\||/^\||/

santiago14 28-07-2016 15:23:27

Cita:

Empezado por BDWONG (Mensaje 507224)
....
Agradezco al foro de Lazarus por su ayuda http://forum.lazarus.freepascal.org/...c,33490.0.html
Me pareció interesante publicarlo acá por si otros tiene el mismo problema.

Saludos.....:cool:

Mil gracias BDWONG, funcionó a la perfección. Justo lo que se necesitaba.
También un caluroso saludo al foro de Lazarus, que tan bien hace su trabajo.

Santiago.

ecfisa 28-07-2016 16:34:13

Hola.
Cita:

Empezado por santiago14 (Mensaje 507193)
...
Lo explico, la grilla tiene la fila seleccionada en un color (azul por ejemplo), y la celda actual en otro color (amarillo...) ...

Te pongo un ejemplo con otra opción, fijate si te sirve:

Código Delphi [-]
...
interface

uses ..., Mask;

type
  TStringGrid = class(Grids.TStringGrid)
  protected
    function CreateEditor: TInplaceEdit; override;
  end;

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
  private
  public
  end;

...

implementation

const
  COLORHIGH = clYellow;
  COLORLOW  = clBlue;

{ TStringGrid (Respetar los colores en la edición) }
function TStringGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  if Focused then
    TMaskEdit(Result).Color := COLORHIGH
  else
    TMaskEdit(Result).Color := COLORLOW;
end;

var
  LastRowSel: Integer;

// cargar algunos datos en el StringGrid
procedure TForm1.FormCreate(Sender: TObject);
var
  c, r: Integer;
  sg: TStringGrid;
begin
  sg := StringGrid1;
  sg.Rows[0].CommaText := 'NRO_OP,TITULO_AVISO,TEXTO_AVISO';
  
  for c := sg.FixedCols to sg.ColCount-1 do
  begin
    sg.ColWidths[c] := 120;
    for r := sg.FixedRows to sg.RowCount -1 do
    begin
      sg.Cells[c, r] := Format('%.12d',[c+r]);
      sg.Objects[c, r] := TObject(sg.Color);   // color por defecto: El del StringGrid !
    end;
  end;
  sg.ColWidths[1] := 300;
  LastRowSel := sg.Row;
  
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  sg: TStringGrid;
begin
  sg := TStringGrid(Sender);
  if not (gdFixed in State) then
  begin
    sg.Canvas.Brush.Color := TColor(sg.Objects[ACol, ARow]);
    sg.Canvas.FillRect(Rect);
    sg.Canvas.TextOut(Rect.Left+2, Rect.Top+2, sg.Cells[Acol,ARow]);
  end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
var
  sg: TStringGrid;
  c: Integer;
begin
  sg := TStringGrid(Sender);
  // anterior línea seleccionada en el color del StringGrid
  for c := sg.FixedCols to sg.ColCount - 1 do
    sg.Objects[c, LastRowSel] := TObject(sg.Color);

  // nueva linea seleccionada
  for c := sg.FixedCols to sg.ColCount - 1 do
    if c = aCol then
      sg.Objects[c, ARow] := TObject(COLORHIGH)
    else
     sg.Objects[c, ARow] := TObject(COLORLOW);

  LastRowSel := ARow; // ultima línea = nueva línea
end;

end.

Salida:


Saludos :)

santiago14 28-07-2016 16:38:45

Buenísimo.
Muchas gracias.

BDWONG 28-07-2016 20:30:58

Genial ecfisa entras mas soluciones mejor le voy a dar una revisada a tu codigo.
Saludos..

roman 28-07-2016 22:54:38

Una pequeña variante:

Código Delphi [-]
interface

type
  TStringGrid = class(Grids.TStringGrid)
  protected
    function CreateEditor: TInplaceEdit; override;
  end;

implementation

{ TStringGrid }

const
  clHiglightCell = clYellow;

type
  TEditor = class(TInPlaceEdit);

function TStringGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  TEditor(Result).Color := clHiglightCell;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  with Sender as TStringGrid do
  begin
    if (ARow = Row) then
    begin
      if (ACol = Col) then
        Canvas.Brush.Color := clHiglightCell
      else
      begin
        Canvas.Brush.Color := clHighlight;
        Canvas.Font.Color := clHighlightText;
      end;

      Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, Cells[ACol, ARow]);
    end;
  end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin
  with Sender as TStringGrid do
  begin
    if ARow <> Row then
      Invalidate;
  end;
end;

DefaultDrawing debe ser true y goRowSelect debe ser false.

LineComment Saludos


La franja horaria es GMT +2. Ahora son las 14:48:16.

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