Einzelnen Beitrag anzeigen

Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#7

Re: Nächstes Control zu einem Punkt finden

  Alt 24. Sep 2009, 22:45
Probier mal Folgendes

Delphi-Quellcode:
//**
//** Gibt den am naheliegendsten Control zurück
//** - wobei die Distanz ausgehend vom Mittelpunkt
//** - der einzelnen Controls berechnet wird
//**
function NearestControl(const Position: TPoint; const MainControl: TControl): TControl;
var
  i, j: Integer;
  lDis, cDis: Single; // last distance, current distance
  function DistanceTo( Control: TControl ): Single;
  var
    x, y: Single;
  begin
    with Control do
    begin
      x := Left + ( Width div 2 ) - Position.X;
      y := Top + ( Height div 2 ) - Position.Y
    end;
    Result := SQRT( x*x + y*y );
  end;
begin
  Result := NIL;
  if MainControl.ComponentCount = 0 then
    Exit;
  j := -1;
  Result := TControl( MainControl.Components[0] );
  lDis := DistanceTo( Result );
  if MainControl.ComponentCount < 2 then
    Exit;
  for i := 1 to MainControl.ComponentCount - 1 do
  begin
    cDis := DistanceTo( TControl( MainControl.Components[i] ) );
    if cDis < lDis then
    begin
      lDis := cDis;
      j := i;
    end;
  end;
  if j > 0 then
    Result := TControl( MainControl.Components[j] );
end;

//**
//** Beispiel: Klatsch ein paar Controls auf die Form, und füge diesen Code in OnMouseDown() ein!
//**
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
var
  c: TControl;
begin
  c := NearestControl( Point( X, Y ), Form1 );
  if Assigned( c ) then
    c.Enabled := not c.Enabled;
end;
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG
  Mit Zitat antworten Zitat