Modify TEdit to include text alignment

posted 11 Mar 2010, 17:58 by Delphi Basics
This snippet shows you how to modify the default delphi components to suit your needs.  This example allows the user to specify an alignment for the text in the edit box.

unit mnEdit;

interface

uses
  SysUtils, Classes, Controls, StdCtrls, Windows;

type type
  TmnEdit = class(TEdit)
  private
    FAlignment: TAlignment;
    procedure SetAlignment(Value: TAlignment);
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    property Alignment: TAlignment read FAlignment write SetAlignment;
  end;

procedure Register;

implementation

procedure Register; procedure Register;
begin
  RegisterComponents('mnEdit', [TmnEdit]); RegisterComponents ( 'mnEdit', [TmnEdit]);
end;

{ TmnEdit }

procedure TmnEdit.CreateParams(var Params: TCreateParams);
const
  Alignments : array[TAlignment] of LongWord= (ES_Left,ES_Right, ES_Center);
begin
  inherited CreateParams(Params);
  Params.Style := Params.Style or Alignments[FAlignment];
end;

procedure TmnEdit.SetAlignment(Value: TAlignment);
begin
  if FAlignment <> Value then
  begin
    FAlignment := Value;
    RecreateWnd;
  end;
end;

end.

Comments