Ver Mensaje Individual
  #2  
Antiguo 20-04-2005
Avatar de Lepe
[Lepe] Lepe is offline
Miembro Premium
 
Registrado: may 2003
Posts: 7.424
Reputación: 29
Lepe Va por buen camino
Por cada columna del dbgrid tienes que recorrer todos los registros y ver la longitud máxima, y despues asignar ese ancho a la columna.

ajustando el DisplayWidth. De este hilo, en lugar de sacar el ancho máximo en caracteres, puedes devolver directamente la cadena más larga, y pasarlo a FontWidthInPixels para saber el ancho exacto.

Código Delphi [-]
uses Graphics,sysutils,types,windows;


function FontDimensionInPixels(SurfaceToPaint:THandle; Str:string;var TheSize : Tsize):Boolean;
begin
  Result := windows.GetTextExtentPoint32(SurfaceToPaint,PChar(str),Length(Str),TheSize)
end;


function FontHeigthInPixels(SurfaceToPaint:THandle; Str:string; var TheHeight : integer):Boolean;
var s:TSize;
begin
  Result := FontDimensionInPixels(SurfaceToPaint,Str,S);
  if Result then TheHeight := s.cy;
end;

function FontWidthInPixels(SurfaceToPaint:THandle; Str:string;var TheWidth:integer):Boolean;
var s:TSize;
begin
  Result := fontdimensioninPixels(SurfaceToPaint,Str,S);
  if Result then TheWidth:= s.cx;
end;

SurfaceToPaint sería Dbgrid1.canvas.handle o bien dbgrid1.columns[0].canvas.handle, no lo he probado para un dbgrid con varias columnas, pero debe funcionar.

Un saludo
__________________
Si usted entendió mi comentario, contácteme y gustosamente,
se lo volveré a explicar hasta que no lo entienda, Gracias.
Responder Con Cita