关于焦点,非常简单(20分)

  • 主题发起人 主题发起人 SmallA
  • 开始时间 开始时间
S

SmallA

Unregistered / Unconfirmed
GUEST, unregistred user!
程序中有10多个DBEDIT,我想在DBEDIT或其他可编辑的控件获得焦点时COLOR 属性变
色,失去焦点是恢复原来的COLOR,我不想每个控件都写代码(这样太笨了),不知高手
有没有好方法呢?
 
写两个过程
procedure TForm1.DBEdit1Enter(Sender: TObject);
begin
TDBEDIT(Sender).color:=clRed;
end;

procedure TForm1.DBEdit1Exit(Sender: TObject);
begin
TDBEDIT(Sender).color:=clWindow;
end;

然后在把所有的DBEDIT的OnEnter和OnExit两个事件分别联接到这两个过程上即可。

你试试吧!一定可以。
 
我想另外写一控件更好了

从TDBEdit继承,处理wm_setfocus及wm_killfocus消息就可以了

unit MyDBEdit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask, DBCtrls;

type
TMyDBEdit = class(TDBEdit)
private
FFocusColor: tcolor;
FNonFocusColor: Tcolor;
{ Private declarations }
procedure wmsetfocus(var msg:twmsetfocus);message wm_setfocus;
procedure wmkillfocus(var msg:twmkillfocus);message wm_killfocus;
procedure SetFocusColor(const Value: tcolor);
procedure SetNonFocusColor(const Value: Tcolor);
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property focuscolor:tcolor read FFocusColor write SetFocusColor;
property NonFocusColor:Tcolor read FNonFocusColor write SetNonFocusColor;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TMyDBEdit]);
end;

{ TMyDBEdit }

procedure TMyDBEdit.SetFocusColor(const Value: tcolor);
begin
FFocusColor := Value;
end;

procedure TMyDBEdit.SetNonFocusColor(const Value: Tcolor);
begin
FNonFocusColor := Value;
color:=FNonFocusColor;
end;

procedure TMyDBEdit.wmkillfocus(var msg: twmkillfocus);
begin
color:=FNonFocusColor;
end;

procedure TMyDBEdit.wmsetfocus(var msg: twmsetfocus);
begin
color:=FFocusColor;
end;

end.
 
用sender可以节约很多
if (sender is Tdbedit) then 调用公用的procedure......
 
多人接受答案了。
 
后退
顶部