Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 26-07-2016
Avatar de santiago14
santiago14 santiago14 is offline
Miembro
 
Registrado: sep 2003
Ubicación: Cerrillos, Salta, Argentina
Posts: 583
Poder: 21
santiago14 Va por buen camino
Unhappy Grilla con colores

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.
Imágenes Adjuntas
Tipo de Archivo: png GrillaColoreada.png (7,8 KB, 39 visitas)
__________________
Uno es responsable de lo que hace y de lo que omite hacer.
Responder Con Cita
  #2  
Antiguo 26-07-2016
Avatar de BDWONG
BDWONG BDWONG is offline
Miembro
NULL
 
Registrado: nov 2013
Posts: 113
Poder: 11
BDWONG Va por buen camino
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
Responder Con Cita
  #3  
Antiguo 27-07-2016
Avatar de BDWONG
BDWONG BDWONG is offline
Miembro
NULL
 
Registrado: nov 2013
Posts: 113
Poder: 11
BDWONG Va por buen camino
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....
Responder Con Cita
  #4  
Antiguo 27-07-2016
Avatar de santiago14
santiago14 santiago14 is offline
Miembro
 
Registrado: sep 2003
Ubicación: Cerrillos, Salta, Argentina
Posts: 583
Poder: 21
santiago14 Va por buen camino
Ok. Veo el fuente.


Enviado desde mi iPhone utilizando Tapatalk
__________________
Uno es responsable de lo que hace y de lo que omite hacer.
Responder Con Cita
  #5  
Antiguo 28-07-2016
Avatar de BDWONG
BDWONG BDWONG is offline
Miembro
NULL
 
Registrado: nov 2013
Posts: 113
Poder: 11
BDWONG Va por buen camino
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.....
Responder Con Cita
  #6  
Antiguo 28-07-2016
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.038
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Responder Con Cita
  #7  
Antiguo 28-07-2016
Avatar de santiago14
santiago14 santiago14 is offline
Miembro
 
Registrado: sep 2003
Ubicación: Cerrillos, Salta, Argentina
Posts: 583
Poder: 21
santiago14 Va por buen camino
Thumbs up

Cita:
Empezado por BDWONG Ver Mensaje
....
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.....
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.
__________________
Uno es responsable de lo que hace y de lo que omite hacer.
Responder Con Cita
  #8  
Antiguo 28-07-2016
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola.
Cita:
Empezado por santiago14 Ver Mensaje
...
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
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #9  
Antiguo 28-07-2016
Avatar de santiago14
santiago14 santiago14 is offline
Miembro
 
Registrado: sep 2003
Ubicación: Cerrillos, Salta, Argentina
Posts: 583
Poder: 21
santiago14 Va por buen camino
Wink

Buenísimo.
Muchas gracias.
__________________
Uno es responsable de lo que hace y de lo que omite hacer.
Responder Con Cita
  #10  
Antiguo 28-07-2016
Avatar de BDWONG
BDWONG BDWONG is offline
Miembro
NULL
 
Registrado: nov 2013
Posts: 113
Poder: 11
BDWONG Va por buen camino
Genial ecfisa entras mas soluciones mejor le voy a dar una revisada a tu codigo.
Saludos..
Responder Con Cita
  #11  
Antiguo 28-07-2016
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Poder: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
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
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
como ver un tipo de datos de una grilla en otra grilla en el mismo form? calistian Varios 5 01-10-2008 19:29:04
La Grilla YOSMITH Gráficos 1 13-06-2007 22:11:50
Coleres en las Grilla josem Varios 6 06-06-2007 20:05:15
Grilla :-( AndyLupa Varios 0 20-04-2006 15:51:18
bandas en grilla Andrea Martinez Varios 0 10-07-2004 06:42:22


La franja horaria es GMT +2. Ahora son las 07:42:37.


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
Copyright 1996-2007 Club Delphi