//参考如下方法,这只能出里从TWinControl继承的控件~~ //另外一种方法,你可以根据鼠标和控件的区域计算~~ procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); var vWinControl: TWinControl; begin case Msg.message of WM_LBUTTONDOWN: begin vWinControl := FindControl(Msg.hwnd); if Assigned(vWinControl) then Caption := Format('%s:%.6f', [vWinControl.Name, Now]); end; end; end;
窗体上有一个components数组可以用来遍历所有在窗体上的控件,不知这样可否?
function GetMousePosControl: TWincontrol; var i: Integer; sc: TPoint; begin begin sc := ScreenToClient(Mouse.CursorPos); for i := 0 to ComponentCount-1 do begin if Components[i] is TWincontrol then if (sc.X > TWincontrol(Components[i]).BoundsRect.Left) and (sc.X < TWincontrol(Components[i]).BoundsRect.Right) and (sc.Y > TWincontrol(Components[i]).BoundsRect.Top) and (sc.Y < TWincontrol(Components[i]).BoundsRect.Bottom) then result := TWincontrol(Components[i]); end; end; end;
//另外一种方法,你可以根据鼠标和控件的区域计算~~
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
var
vWinControl: TWinControl;
begin
case Msg.message of
WM_LBUTTONDOWN: begin
vWinControl := FindControl(Msg.hwnd);
if Assigned(vWinControl) then
Caption := Format('%s:%.6f', [vWinControl.Name, Now]);
end;
end;
end;
var
i: Integer;
sc: TPoint;
begin
begin
sc := ScreenToClient(Mouse.CursorPos);
for i := 0 to ComponentCount-1 do
begin
if Components[i] is TWincontrol then
if (sc.X > TWincontrol(Components[i]).BoundsRect.Left) and
(sc.X < TWincontrol(Components[i]).BoundsRect.Right) and
(sc.Y > TWincontrol(Components[i]).BoundsRect.Top) and
(sc.Y < TWincontrol(Components[i]).BoundsRect.Bottom) then
result := TWincontrol(Components[i]);
end;
end;
end;