Hi delfan,
要想将 TEdit 的 Text 以右对齐方式显示需要自己写一个控件去作; 右对齐方式是
创建一个窗体的时候在它的风格里面所设定的, 对齐方式可在三个参数中选一个[ES_LEFT, ES_RIGNT, ES_CENTER], 默认值是ES_LEFT; 设定该值是通过一个
Windowed Control 的 CreateParams 过程完成的, 由于 Delphi 在 TEdit 中
没有定义对齐的属性, 所以你必须自定义一个 Alignment: TAlignment类型的属性,
每次改变改属性后必须重新创建窗体(调用 RecreateWnd 函数); 下面是一个实际
的例子动态的创建一个带有左中右对齐属性(经过测试); 如果想以后使用这个对象,
你可以单独创建一个 Unit 并加入注册函数, 使其变成一个控件:
type
//自定 TMyEdit 对象
TMyEdit = class(TEdit)
private
FAlignment: TAlignment;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure SetAlignment(Value: TAlignment);
published
property Alignment: TAlignment read FAlignment write SetAlignment;
end;
//Deliphi 的 TForm1
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyEdit1: TMyEdit;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TMyEdit.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
procedure TMyEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));
begin
inherited CreateParams(Params);
with Params do
begin
Style := Style or Alignments[UseRightToLeftAlignment, FAlignment];
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyEdit1:=TMyEdit.Create(Self);
MyEdit1.Alignment := taRightJustify;
MyEdit1.parent:=Self;
MyEdit1.Left := 20;
MyEdit1.Top := 50;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyEdit1.Free;
end;