如何让edit里的文字居中?

解决方案 »

  1.   

    Edit1.Alignment := taCenter;
      

  2.   

    unit MyEdit;interfaceuses
      Controls,Windows, Messages, SysUtils, Variants, Classes, Graphics,Forms, Dialogs, StdCtrls;
    type
      TMyEdit=class(TEdit)
      private
        FAlignment:TAlignment;
        procedure SetAlignment(const Value: TAlignment);
        procedure CreateParams(var Params:TCreateParams);override;
      protected
      published
         property  Alignment:TAlignment read FAlignment write SetAlignment;
      end;
    implementation{ TMyEdit }constructor TMyEdit.Create(AOwner: TComponent);
    begin
      inherited;end;procedure TMyEdit.CreateParams(var Params: TCreateParams);
    begin
      inherited;
      case FAlignment of
        taLeftJustify:
          begin
            Params.Style := Params.Style + ES_LEFT;
          end;
        taRightJustify:
          begin
            Params.Style := Params.Style + ES_RIGHT;
          end;
        taCenter:
          begin
            Params.Style := Params.Style + ES_CENTER;
          end;
      end;
    end;
    procedure TMyEdit.SetAlignment(const Value: TAlignment);
    begin
      if FAlignment<>Value then
      begin
        FAlignment := Value;
        RecreateWnd;
      end;
    end;
    end.---------------
    uses MyEdit;procedure TForm1.Button1Click(Sender: TObject);
    var
      MyEdit:TMyEdit;
    begin
      MyEdit:=TMyEdit.Create(Form1);
      MyEdit.Alignment:=taCenter;
      MyEdit.Parent:=Form1;
    end;
      

  3.   

    重载有些过了,呵呵,Edit1.Alignment := taCenter;
    这个方便。祝:身体健康!