为什么TEdit没有居中对齐啊,我要做一个居中对齐怎么做呢?

解决方案 »

  1.   

    在你的程序中替换掉TEdit,如下:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
     TEdit = class(StdCtrls.TEdit)
     private
       FAlignment : TAlignment;
       procedure SetAlignment(Value: TAlignment);
     protected
       procedure CreateParams(var Params: TCreateParams); override;
     public
       property Alignment: TAlignment read FAlignment write SetAlignment;
    end;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TEdit.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 TEdit.SetAlignment(Value: TAlignment);
    begin
     if FAlignment <> Value then
     begin
       FAlignment := Value;
       RecreateWnd;
     end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Edit1.Alignment := taCenter;  // 居中,也可以设置其它对方式(taLeftJustify, taRightJustify)
    end;end.