GARANTIR DESCONTO

Fórum [Dúvida] Personalizando Componentes da VCL: TLabeledEdit #367139

14/12/2008

0

Estou estudando a um tempo POO (Programação Orientada a Objetos) e também o desenvolvimento de novos componentes.

Estou personalizando um componentes já existente da VCL, o TLabeledEdit.
Estou adequando ele às minhas necessidades de desenvolvimento.

Por enquanto já consegui fazer o seguinte:
Alterar a cor de fundo do Label;
Alterar a cor da fonte do Label.


O que quero fazer agora é o seguinte.
Deixei a propriedade AutoSize do Label como False permitindo que ele ficasse alinhado em tamanho com o Edit e o Caption do Label centralizado.

Quero que a medida que o Width do Edit seja alterado o Width do Label seja alterado também para o mesmo valor.

Acho que consegui explicar bem o que quero.
Qualquer coisa postem que eu me explico melhor.

Estou utilizando Delphi 2007.
Será que podem me ajudar nessa, seja passando o código ou me indicando uma leitura que me ajude (seria até melhor)?


Bruno6652

Bruno6652

Responder

Posts

14/12/2008

Bruno6652

Olha o que eu fiz.

Estava herdando essa nova classe TLabeledEditEx que criei de TLabeledEdit.
Aí resolvi mudar para fazer um teste e herdei de TCustomLabeledEdit.
estranho, mas funcionou do jeito que eu queria, não sei realmente porquê.

Mas o problema é que a minha propriedade LabelWidth está como Published e eu não quero que ela aparece no Objetc Inspector, entretanto eu já mudei a visibilidade dela pra Public e aí quando faço isso não funciona mais.


Vou postar o código completo da minha classe aqu que deve facilitar neh.

unit LabeledEditEx;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls, ExtCtrls, Graphics;

type
  TLabeledEditEx = class(TCustomLabeledEdit)
  private
    { Private declarations }
    FEditWidth: Integer;
    FLabelBackgroundColor: TColor;
    FLabelFontColor: TColor;
    FLabelWidth: Integer;
    function GetEditWidth: Integer;
    procedure SetLabelWidth(Value: Integer);
    procedure SetLabelBackgroundColor(Value: TColor);
    procedure SetLabelFontColor(Value: TColor);
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property LabelWidth: Integer Read GetEditWidth Write SetLabelWidth;
  published
    { Published declarations }
    property LabelBackugroundColor: TColor Read FLabelBackgroundColor Write SetLabelBackgroundColor Default clInactiveCaption;
    property LabelFontColor: TColor Read FLabelFontColor Write SetLabelFontColor Default clWindow;
    //property LabelWidth: Integer Read GetEditWidth Write SetLabelWidth;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents(´Bruno Components´, [TLabeledEditEx]);
end;

{ TLabeledEditEx }

constructor TLabeledEditEx.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FLabelBackgroundColor := clInactiveCaption;
  FLabelFontColor := clWindow;

  EditLabel.Transparent := False;
  EditLabel.Color := FLabelBackgroundColor;
  EditLabel.Font.Color := FLabelFontColor;

  TLabel(EditLabel).AutoSize := False;
  TLabel(EditLabel).Alignment := taCenter;

  EditLabel.Width := Self.Width;
end;

destructor TLabeledEditEx.Destroy;
begin
  inherited Destroy;
end;

function TLabeledEditEx.GetEditWidth: Integer;
begin
  FLabelWidth := Self.Width;

  EditLabel.Width := FLabelWidth;

  Result := FLabelWidth;
end;

procedure TLabeledEditEx.SetLabelBackgroundColor(Value: TColor);
begin
  FLabelBackgroundColor := Value;

  EditLabel.Color := FLabelBackgroundColor;
end;

procedure TLabeledEditEx.SetLabelFontColor(Value: TColor);
begin
  FLabelFontColor := Value;

  EditLabel.Font.Color := FLabelFontColor;
end;

procedure TLabeledEditEx.SetLabelWidth(Value: Integer);
begin
  FLabelWidth := Self.Width;

  EditLabel.Width := FLabelWidth;
end;

end.


Devo ter feito alguma coisa errada mesmo.
E aproveitando, se cometi algum erro de programação de componentes, me corrijam tambem.


Obrigado mais uma vez. [/code]


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar