Einzelnen Beitrag anzeigen

Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.429 Beiträge
 
Delphi 10.4 Sydney
 
#9

AW: Objektrotation in Abhängigkeit

  Alt 16. Mai 2014, 13:30
Das Problem lässt sich in zwei Teile untergliedern.
1. Das Steuerelement (TImage) muss an seine neue Position verschoben werden.
Für die Drehung um 90 und 270 Grad müssen Höhe und Breite getauscht werden.
2. Die enthaltene Grafik (TBitmap) muss gedreht werden.
Delphi-Quellcode:
function RotatePoint(APoint, ARotPoint: TPoint; AValue: Word): TPoint;
var
  w, cosw, sinw: Double;
begin
  APoint.x := APoint.x - ARotPoint.x;
  APoint.y := APoint.y - ARotPoint.y;

  w := DegToRad(AValue);
  sinw := sin(w);
  cosw := cos(w);
  Result.x := Round((APoint.x * +cosw) + (APoint.y * sinw));
  Result.y := Round((APoint.x * -sinw) + (APoint.y * cosw));

  Result.x := Result.x + ARotPoint.x;
  Result.y := Result.y + ARotPoint.y;
end;

procedure RotateWinCtrl(AWinCtrl: TWinControl; ARotPoint: TPoint; AValue: Word);
var
  lR: TRect;
  lP: TPoint;
  dx, dy: Integer;
begin
  AValue := AValue mod 360;
  {den Eckpunkt bestimmen, der nach der Rotation links-oben ist}
  lR := AWinCtrl.BoundsRect;
  case AValue div 90 of
    0: {keine Drehung}
      begin
        Exit;
      end;
    1: {90 Grad}
      begin
        lP.x := lR.Left;
        lP.y := lR.Bottom;
        dx := lR.Bottom - lR.Top;
        dy := lR.Right - lR.Left;
      end
    2: {180 Grad}
      begin
        lP.x := lR.Right;
        lP.y := lR.Bottom;
        dx := lR.Right - lR.Left;
        dy := lR.Bottom - lR.Top;
      end
    3: {270 Grad}
      begin
        lP.x := lR.Right;
        lP.y := lR.Top;
        dx := lR.Bottom - lR.Top;
        dy := lR.Right - lR.Left;
      end;
  end;

  lP := RotatePoint(lP, ARotPoint, AValue);
  
  AWinCtrl.Left := lP.x;
  AWinCtrl.Top := lP.y;
  AWinCtrl.Width := dx;
  AWinCtrl.Height := dy;

  if AWinCtrl is TImage then
  begin
    bmp1 := TBitmap.Create;
    bmp2 := TBitmap.Create;
    try
      bmp1.Assign(TImage(AWinCtrl).Picture.Graphic);
      DreheBmp(AValue, bmp1, bmp2);
      TImage(AWinCtrl).Picture.Assign(bmp2);
    finally
      bmp1.Free;
      bmp2.Free;
    end;
  end;
end;

procedure Tform_main.Button1Click(Sender: TObject);
var
  lRotRoint: TPoint;
begin
  // Mittelpunkt des T-Stücks
  lRotRoint.x := Image1.Left + (Image1.Width div 2);
  lRotRoint.y := Image1.Top + (Image1.Height div 2);

  RotateWinCtrl(Image1, lRotRoint, 90);
end;

Geändert von Blup (16. Mai 2014 um 13:35 Uhr)
  Mit Zitat antworten Zitat