调整 TEdit 文本对齐

譬如让 Edit1 文本右对齐:
SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_RIGHT);

本例效果图:

调整 TEdit 文本对齐

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ExtCtrls;



type

  TForm1 = class(TForm)

    Edit1: TEdit;

    RadioGroup1: TRadioGroup;

    procedure FormCreate(Sender: TObject);

    procedure RadioGroup1Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



procedure TForm1.FormCreate(Sender: TObject);

begin

  RadioGroup1.Items.CommaText := 'Left,Center,Right';

  RadioGroup1.ItemIndex := 0;

end;



procedure TForm1.RadioGroup1Click(Sender: TObject);

var

  es: Integer;

begin

  es := GetWindowLong(Edit1.Handle, GWL_STYLE) or 3;

  es := es xor (3 - RadioGroup1.ItemIndex);

  SetWindowLong(Edit1.Handle, GWL_STYLE, es);

  Edit1.Refresh;

end;



end.


 
   
这里面有个问题需要知道: 决定对齐方式的三个常量值 ES_LEFT、ES_CENTER、ES_RIGHT 分别是 0、1、2, 刚好可以对应 RadioGroup1.ItemIndex; 再换一种思路实现一下:

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ExtCtrls;



type

  TForm1 = class(TForm)

    Edit1: TEdit;

    RadioGroup1: TRadioGroup;

    procedure FormShow(Sender: TObject);

    procedure RadioGroup1Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



var

  es_bak: Integer;



procedure TForm1.FormShow(Sender: TObject);

begin

  RadioGroup1.Items.CommaText := 'Left,Center,Right';

  es_bak := GetWindowLong(Edit1.Handle, GWL_STYLE);

end;



procedure TForm1.RadioGroup1Click(Sender: TObject);

var

  es: Integer;

begin

  es := es_bak or RadioGroup1.ItemIndex;

  SetWindowLong(Edit1.Handle, GWL_STYLE, es);

  Edit1.Refresh;

end;



end.


 
   

你可能感兴趣的:(it)