Bạn có thể tạo TTimer để thăm dò vị trí chuột hiện tại sau mỗi 0,10 giây hoặc lâu hơn, sau đó vị trí "AnotherComponent" theo vị trí chuột hiện tại.
Sau đó, bạn sẽ không kích hoạt sự kiện của mình cho mỗi pixel di chuyển chuột - bạn sẽ không cần bất kỳ sự kiện OnMouseMove nào trên thành phần điều khiển của bạn cả.
Trên máy tính của tôi, điều này về cơ bản không có tác động hiệu suất nào cả.
procedure TForm1.Timer1Timer(Sender: TObject);
var
pt: TPoint;
begin
//Is the cursor inside the controlling component? if so, position some
//other control based on that mouse position.
GetCursorPos(pt);
if MouseWithin(pt.x,pt.y,MyComponent,Form1.Left,Form1.Top) then begin
//replace with whatever real positioning logic you want
AnotherComponent.Top := pt.y;
AnotherComponent.Left := pt.x;
end;
end;
function TForm1.MouseWithin(mouseX, mouseY: integer;
const comp: TWinControl; const ParentWindowLeft: integer;
const ParentWindowTop: integer): boolean;
var
absoluteCtrlX, absoluteCtrlY: integer;
begin
//take a control, and the current mouse position.
//tell me whether the cursor is inside the control.
//i could infer the parent window left & top by using ParentwindowHandle
//but I'll just ask the caller to pass them in, instead.
//get the absolute X & Y positions of the control on the screen
//needed for easy comparison to mouse position, which will be absolute
absoluteCtrlX := comp.Left + ParentWindowLeft;
absoluteCtrlY := comp.Top + ParentWindowTop +
GetSystemMetrics(SM_CYCAPTION);
Result := (mouseX >= absoluteCtrlX)
and (mouseX < absoluteCtrlX + comp.Width)
and (mouseY >= absoluteCtrlY)
and (mouseY <= absoluteCtrlY + comp.Height);
end;
thể bạn vui lòng thêm liệu thành phần và điều khiển bên dưới nó là điều khiển cửa sổ hay không? Họ có mã vẽ phức tạp không? – mghie