Modify TEdit to include text alignment

Post date: Mar 12, 2010 1:58:52 AM

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.