unit MyEdit;
interface
uses
SysUtils, Classes, Controls, StdCtrls,Graphics;
type
TMyEdit = class(TEdit)
private
{ Private declarations }
FFontColor:TColor;
//继承并修改TCustomEdit和TWinControl中的两个事件
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyEdit]);
end;
{ TMyEdit }
procedure TMyEdit.CMEnter(var Message: TCMEnter);
begin
inherited;
//Enter时,把color改为黄色
Color:=clYellow;
end;
procedure TMyEdit.CMExit(var Message: TCMExit);
begin
inherited;
Color:=FFontColor; //还原Color
end;
constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited;
FFontColor:=Color; //记录原来的color
end;
end.
-------------------------------------
demo如下:
unit U_YellowEdit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,MyEdit, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
//这样做只是为了省去安装、卸载package(Component)的麻烦
//等测试OK,再装也不迟
with TMyEdit.Create(self) do
begin
Parent:=Self;
left:=Random(ClientWidth);
Top:=Random(ClientHeight);
Enabled:=True;
visible:=True;
end;
end;
end.