关于EDIT的问题(100分)

  • 主题发起人 主题发起人 jiabingli
  • 开始时间 开始时间
J

jiabingli

Unregistered / Unconfirmed
GUEST, unregistred user!
在DELPHI中,Edit.text中的数据如何右对齐或聚中?
Edit控件好象没有右对齐或聚中的属性。请教如何实现右对齐或聚中?
 

写一个组件就行了。

unit Edit1;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls;

type
TEdit1 = class(TEdit)
private
FAlignment : TAlignment; //对齐方式
procedure SetAlignment(Value: TAlignment);
protected
procedure CreateParams(var Params: TCreateParams); override;
public
{ Public declarations }
published
property Alignment: TAlignment read FAlignment write SetAlignment;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('dwh', [TEdit1]);
end;


procedure TEdit1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);

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 TEdit1.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
RecreateWnd;
end;
end;

end.
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=870605
 
将下面的源码存为pas文件,然后安装此组件就OK了。

unit AEdit;

interface

uses
Windows,
SysUtils,
Messages,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
Menus,
StdCtrls,
ExtCtrls;

type
TEoCEdit = class(TEdit)
private
{ Private declarations }
FAlignment: TAlignment;
protected
{ Protected declarations }
function GetAlignment: TAlignment; virtual;
procedure SetAlignment(newValue: TAlignment); virtual;
procedure CreateParams(var Params: TCreateParams); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published properties and events }
property Alignment: TAlignment read GetAlignment write SetAlignment; { Published }
end; { TEoCEdit }

procedure Register;

implementation

function TEoCEdit.GetAlignment: TAlignment;
{ Returns the value of data member FAlignment. }
begin
GetAlignment := FAlignment;
end; { GetAlignment }

procedure TEoCEdit.SetAlignment(newValue: TAlignment);
{ Sets data member FAlignment to newValue. }
begin
if FAlignment <> newValue then
begin
FAlignment := newValue;
if not (csLoading in componentstate) then ReCreateWnd;
end;
end; { SetAlignment }

destructor TEoCEdit.Destroy;
begin
inherited Destroy;
end; { Destroy }

constructor TEoCEdit.Create(AOwner: TComponent);
{ Creates an object of type TEoCEdit, and initializes properties. }
begin
inherited Create(AOwner);
{ Initialize properties with default values: }
FAlignment := taLeftJustify;
end; { Create }

procedure TEoCEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of WORD = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or Alignments[FAlignment];
end;

procedure Register;
begin
RegisterComponents('EoC', [TEoCEdit]);
end; { Register }

end.
 
不知有没有更简捷的方法来直接处理?因为我的程序基本上都写好了。
 
后退
顶部