Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Centrar celdas en un TSTringGrid (https://www.clubdelphi.com/foros/showthread.php?t=17813)

gluglu 22-01-2005 13:44:42

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

Delfino 22-01-2005 22:16:00

El componente JvStringGrid de la JVCL (www.delphi-jedi.org) te permite hacer esto sin codigo..

salvica 23-01-2005 12:30:32

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

gluglu 23-01-2005 13:41:44

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


La franja horaria es GMT +2. Ahora son las 00:14:31.

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