要实现左、中、右对奇很简单,只要重载其CreateParams函数就可以了,设成只能输入
数字的也是这样。但要实现透明度的控制就比较麻烦了。你可以参考一下我做的这个控件,
如果有不满意的地方自己改改试试(例如在把Style设为stInteger时自动把Edit的文本清空)
unit EditEx;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TEditStyle = (esAnything, esInteger);
TEditEx = class(TEdit)
private
FAlignment: TAlignment;
FStyle: TEditStyle;
procedure SetAlignment(Value: TAlignment);
procedure SetEditStyle(Value: TEditStyle);
procedure CMEnter(var Message: TCMGotFocus); message CM_ENTER;
procedure CMExit(Sender: TObject);
protected
procedure CreateParams(var Params: TCreateParams); override;
public
constructor Create(AOwner: TComponent); override;
published
property Alignment: TAlignment read FAlignment write SetAlignment;
property Style: TEditStyle read FStyle write SetEditStyle default esAnything;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TEditEx]);
end;
procedure TEditEx.CMEnter(var Message: TCMGotFocus);
begin
Color := clInfoBk;
Font.Color := clNavy;
end;
procedure TEditEx.CMExit(Sender: TObject);
begin
Color := clWindow;
Font.Color := clWindowText;
end;
constructor TEditEx.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
OnExit := CMExit;
end;
procedure TEditEx.CreateParams(var Params: TCreateParams);
var PrevParams: TCreateParams;
begin
inherited CreateParams(Params);
PrevParams.Style := Params.Style;
case FAlignment of
taLeftJustify:
begin
Params.Style := PrevParams.Style;
Params.Style := Params.Style + ES_LEFT;
end;
taRightJustify:
begin
Params.Style := PrevParams.Style;
Params.Style := Params.Style + ES_RIGHT;
end;
taCenter:
begin
Params.Style := PrevParams.Style;
Params.Style := Params.Style + ES_CENTER;
end;
end;
PrevParams.Style := Params.Style;
if FStyle = esInteger then
begin
Params.Style := PrevParams.Style;
Params.Style := Params.Style + ES_NUMBER;
end;
end;
procedure TEditEx.SetAlignment(Value: TAlignment);
begin
if FAlignment<>Value then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
procedure TEditEx.SetEditStyle(Value: TEditStyle);
begin
if FStyle<>Value then
begin
FStyle := Value;
RecreateWnd;
end;
end;
end.