Gracias a ambos por sus ideas. Me hicieron sentido, pero creo que me falta un detalle técnico para implementarlas en mi componente.
Al componente que hice le asigne un monospaced font para que fuera un ancho constante, lo incluyo en el Form y le modifico las propiedades en tiempo de diseño pero no me ha funcionado.
Les adjunto lo esencial del código, esperando me puedan orientar para corregir mi error:
Código:
unit Editor;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Forms,
Graphics, ExtCtrls, StrUtils;
type
TEditor = class(TEdit)
protected
procedure SetAutoSize(Value: Boolean); override;
procedure DoSetMaxLength(Value: Integer); virtual;
procedure AjustarWidth;
public
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Comunes', [TEditor]);
end;
procedure TEditor.SetAutoSize(Value: Boolean);
var bCambio: Boolean;
begin
bCambio:= Value <> AutoSize;
inherited;
if bCambio then AjustarWidth;
end;
procedure TEditor.DoSetMaxLength(Value: Integer);
begin
AjustarWidth;
inherited;
end;
procedure TEditor.AjustarWidth;
var
B: TBitMap;
sText: String;
begin
if (AutoSize) and (MaxLength > 0) then
begin
//Calculo ancho de una cadena completa,
//pues regularmente Text no estará al máximo
sText:= #32+ DupeString('X', MaxLength) +#32;
//
B:= TBitMap.Create;
try
B.Canvas.Font.Assign(Font);
Width:= B.Canvas.TextWidth(sText)
finally
B.Free
end;//try
end;//if
end;
END.
Gracias por adelantado.