设置获得焦点的控件改变颜色,代码如下,但需要在Close或者Destroy之前设置OnActiveControlChange为nil,在不使用事件下如何设置?
注意,下面代码是在窗体中,我需要写组件unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,TypInfo, StdCtrls, ClipBrd, ComCtrls, Mask;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
NoChanged: Boolean;
{ Private declarations }
public
{ Public declarations }
procedure ActiveControlChanged(Sender: TObject);
end;var
Form1: TForm1;
old: TWinControl;implementationuses Unit2;{$R *.dfm}procedure TForm1.ActiveControlChanged(Sender: TObject);
var
Control: TWinControl;
ClipBard: TClipBoard;
begin
Control := Screen.ActiveControl;
if Control is TForm then Exit;
if GetPropInfo(Control.ClassInfo, 'Color') <> nil then
begin
Control.Invalidate;
SetPropValue(Control, 'Color', clRed);
end;
if Assigned(old) then
begin
if GetPropInfo(old.ClassInfo, 'Color') <> nil then
begin
old.Invalidate;
SetPropValue(old, 'Color', clWhite);
end;
end;
old := Control;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Screen.OnActiveControlChange:=ActiveControlChanged;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Screen.OnActiveControlChange := nil;
end;
end.
注意,下面代码是在窗体中,我需要写组件unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,TypInfo, StdCtrls, ClipBrd, ComCtrls, Mask;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
NoChanged: Boolean;
{ Private declarations }
public
{ Public declarations }
procedure ActiveControlChanged(Sender: TObject);
end;var
Form1: TForm1;
old: TWinControl;implementationuses Unit2;{$R *.dfm}procedure TForm1.ActiveControlChanged(Sender: TObject);
var
Control: TWinControl;
ClipBard: TClipBoard;
begin
Control := Screen.ActiveControl;
if Control is TForm then Exit;
if GetPropInfo(Control.ClassInfo, 'Color') <> nil then
begin
Control.Invalidate;
SetPropValue(Control, 'Color', clRed);
end;
if Assigned(old) then
begin
if GetPropInfo(old.ClassInfo, 'Color') <> nil then
begin
old.Invalidate;
SetPropValue(old, 'Color', clWhite);
end;
end;
old := Control;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Screen.OnActiveControlChange:=ActiveControlChanged;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Screen.OnActiveControlChange := nil;
end;
end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TypInfo;type TFocusControl = class(TComponent)
private
old: TWinControl;
oldColor: TColor;
FColorBackground: TColor;
procedure SetColorBackground(const Value: TColor);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
protected
procedure ActiveControlChanged(Sender: TObject);
published
property ColorBackground: TColor read FColorBackground write
SetColorBackground;
end;procedure Register;implementation{ TFocusTest }procedure Register;
begin
RegisterComponents('S.F.', [TFocusControl]);
end;procedure TFocusControl.ActiveControlChanged(Sender: TObject);
var
Control : TWinControl;
begin if csDesigning in ComponentState then
exit; if Screen.ActiveControl = nil then
begin
Screen.OnActiveControlChange := nil;
Exit;
end; Control := Screen.ActiveControl;
if GetPropInfo(Control.ClassInfo, 'Color') <> nil then
oldColor := GetPropValue(Control, 'Color'); if GetPropInfo(Control.ClassInfo, 'Color') <> nil then
begin
Control.Invalidate;
SetPropValue(Control, 'Color', FColorBackground);
end;
if Assigned(old) then
begin
if GetPropInfo(old.ClassInfo, 'Color') <> nil then
begin
old.Invalidate;
SetPropValue(old, 'Color', oldColor);
end;
end; old := Control;end;constructor TFocusControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FColorBackground := clRed;
Screen.OnActiveControlChange := ActiveControlChanged;
end;destructor TFocusControl.Destroy;
begin
if Assigned(Screen.OnActiveControlChange) then
Screen.OnActiveControlChange := nil;
inherited;
end;procedure TFocusControl.SetColorBackground(const Value: TColor);
begin
FColorBackground := Value;
end;end.