Ver Mensaje Individual
  #6  
Antiguo 21-02-2012
elGuerrero elGuerrero is offline
Miembro
NULL
 
Registrado: ene 2012
Posts: 10
Reputación: 0
elGuerrero Va por buen camino
Lightbulb Solución con WMPaint

La solución anterior me ocasionó problemas en la apariencia del componente pues borraba el contenido de él, no actualizaba cuando cambiaba la MaxLength y otros detalles.
Se me ocurrió hacerlo a través de WMPaint, que se ejecuta al redibujar el control, así que recargue los procedimientos

Código:
  TEditor = class(TEdit)
    procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  protected
    procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;
    procedure SetAutoSize(Value: Boolean); override;
    procedure DoSetMaxLength(Value: Integer); virtual;
  end;
Luego en la sección de implementationredefiní el procedimiento

Código:
procedure TEditor.WMPaint(var Msg: TWMPaint);
var
  sText: String;
  MCanvas: TControlCanvas;
begin
  if (AutoSize) and (MaxLength > 0) then
    Begin
    MCanvas := TControlCanvas.Create;
    try
      MCanvas.Control   := Self;
      MCanvas.Font      := Self.Font;
      sText:= DupeString('X', MaxLength+2);
      Self.Width:= MCanvas.TextWidth(sText);
    finally
      MCanvas.Free;
    end; //try
    End;
  Inherited;
end;
Para forzarlo a redibujar el componente, utilicé RePaint en los mismos procedimientos:

Código:
procedure TEditor.SetAutoSize(Value: Boolean);
begin
  inherited;
  Repaint;
end;

procedure TEditor.DoSetMaxLength(Value: Integer);
begin
  inherited;
  Repaint;
end;

procedure TEditor.CMFontChanged(var Message: TMessage);
begin
  inherited;
  Repaint;
end;
Y ahora si, ya está funcionando como deseaba.

Esta solución la terminé consultando varios códigos fuente, sin embargo, tengo duda en cuanto a que si tengo bugs en el código o errores de diseño ¿Uds. qué creen?
Les agradezco de antemano sus respuestas.
Responder Con Cita