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 22-01-2005
Avatar de gluglu
[gluglu] gluglu is offline
Miembro Premium
 
Registrado: sep 2004
Ubicación: Málaga - España
Posts: 1.455
Poder: 21
gluglu Va por buen camino
Centrar celdas en un TSTringGrid

Sé que es una pregunta muy tonta, pero no la resuelvo por incompatibilidad entre los tipos Integer y Double.

Qué me falta para poder centrar el texto dentro de una celda de un TStringGrid ?

Código Delphi [-]
procedure TMaster.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  Txt: String;
  Lon: Byte;
begin
  Txt := StringGrid1.Cells[ACol,Arow];
  If ACol = 0 then
    StringGrid1.Canvas.TextRect(Rect,Rect.Left+3,Rect.Top,Txt)
  else
    begin
      Lon := Canvas.TextWidth(Txt);
      StringGrid1.Canvas.TextRect(Rect,Lon,Rect.Top,Txt);
    end;
Si pongo
Código Delphi [-]
Lon := Canvas.TextWidth(Txt)/2;
me dá error.

Gracias
Responder Con Cita
  #2  
Antiguo 22-01-2005
Delfino Delfino is offline
Miembro
 
Registrado: jul 2003
Ubicación: Madrid
Posts: 974
Poder: 21
Delfino Va por buen camino
El componente JvStringGrid de la JVCL (www.delphi-jedi.org) te permite hacer esto sin codigo..
Responder Con Cita
  #3  
Antiguo 23-01-2005
Avatar de salvica
salvica salvica is offline
Miembro
 
Registrado: mar 2004
Ubicación: Albacete (España) ... En un lugar de la Mancha ...
Posts: 304
Poder: 21
salvica Va por buen camino
Talking

De entrada tienes que usar divisiones enteras (div), luegos debes pensar que el TRect del grid contienes posiones respecto al origen del mismo

Código Delphi [-]
 var
  Txt: String;
   LonTexto: integer;
   AnchoCelda : integer;
   MargenTexto : integer;
 begin
  Txt := StringGrid1.Cells[ACol,Arow];
  If ACol = 0 then
    StringGrid1.Canvas.TextRect(Rect,Rect.Left+3,Rect.Top,Txt)
  else
    begin
      LonTexto := Canvas.TextWidth(Txt);
      AnchoCelda := Rect.Right - Rect.Left;
      MargenTexto := (AnchoCelda-LonTexto) div 2;
      StringGrid1.Canvas.TextRect(Rect, MargenTexto, Rect.Top, Txt);
    end;
end;

nota: como no tengo el delphi a mano, no recuerdo si el valor que se pasa en segundo lugar es la posición de inicio dentro de la celda o el tamaño del texto a dibujar, pero por ahi van los tiros
Saludos, salvica
Responder Con Cita
  #4  
Antiguo 23-01-2005
Avatar de gluglu
[gluglu] gluglu is offline
Miembro Premium
 
Registrado: sep 2004
Ubicación: Málaga - España
Posts: 1.455
Poder: 21
gluglu Va por buen camino
Gracias, me ha servido mucho tu explicación.

Finalmente ha quedado así :
Código Delphi [-]
var
  Txt: String;
  LonTxt: Integer;
begin
  Txt := StringGrid1.Cells[ACol,Arow];
  If ACol = 0 then
    StringGrid1.Canvas.TextRect(Rect,Rect.Left+3,Rect.Top,Txt)
  else
    begin
      LonTxt := Canvas.TextWidth(Txt) div 2;
      StringGrid1.Canvas.TextRect(Rect,Rect.Left+8-LonTxt,Rect.Top,Txt);
    end;
Debo indicar adicionalmente que el ancho de la celda es fijo por diseño y es = 16. Por ello 16/2 = 8 y es el valor que he puesto en Rect.Left+8-LonTxt

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


La franja horaria es GMT +2. Ahora son las 21:05:32.


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