type TForm1 = class(TForm) Edit1: TEdit; Memo1: TMemo; Button1: TButton; procedure FormCreate(Sender: TObject); private { Private declarations } public procedure ColorControl(Sender: TObject); end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.ColorControl(Sender: TObject); var I : Integer; begin for I:= 0 to ControlCount -1 do begin if Controls[I] is TWinControl then begin if (Controls[I] as TWinControl).Focused then Controls[I].Color := clRed else Controls[I].Color := clWindow; end; end;procedure TForm1.FormCreate(Sender: TObject); begin Screen.OnActiveControlChange := ColorControl; end;
Sorry!没看清楚! 这个行不行? function GetSysFocus : integer; Var hFgWin,FgThreadID,hFocusWin : integer; Begin hFgWin := GetForegroundWindow; FgThreadID := GetWindowThreadProcessID(hFgWin, nil); If AttachThreadInput(GetCurrentThreadID, FgThreadID, true) Then Begin hFocusWin := GetFocus; result := GetFocus; AttachThreadInput(GetCurrentThreadID, FgThreadID, False); End else result := GetFocus; End;
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
procedure ColorControl(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ColorControl(Sender: TObject);
var
I : Integer;
begin
for I:= 0 to ControlCount -1 do begin
if Controls[I] is TWinControl then
begin
if (Controls[I] as TWinControl).Focused then
Controls[I].Color := clRed
else
Controls[I].Color := clWindow;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Screen.OnActiveControlChange := ColorControl;
end;
=========================================================
var
HostThreadID,SelfThreadID : dword;
len : integer;
lpoint : TPoint;
begin
//获得系统当前激活的窗口句柄
hwnd_foreGround := GetForegroundWindow;
//获得当前激活的窗口句柄所对应的进程中有编辑输入焦点的对象句柄
HostThreadID := GetWindowThreadProcessId(hwnd_foreGround,nil);
SelfThreadID := GetCurrentThreadId();
AttachThreadInput(HostThreadID,SelfThreadID,true);
//获得焦点对象句柄
hwnd_focused := GetFocus();
//获得脚本编辑区域的当前光标位置的窗口坐标
GetCaretPos(SystemCaretPos); AttachThreadInput(HostThreadID,SelfThreadID,false);
if hwnd_focused = 0 then exit;
GetFocus我也试了,如果是我自己的程序可以获得控件句柄,但是对于其他程序不行!
这个行不行?
function GetSysFocus : integer;
Var
hFgWin,FgThreadID,hFocusWin : integer;
Begin
hFgWin := GetForegroundWindow;
FgThreadID := GetWindowThreadProcessID(hFgWin, nil);
If AttachThreadInput(GetCurrentThreadID, FgThreadID, true) Then
Begin
hFocusWin := GetFocus;
result := GetFocus;
AttachThreadInput(GetCurrentThreadID, FgThreadID, False);
End
else
result := GetFocus;
End;
form.activeconrol属性