Einzelnen Beitrag anzeigen

Volker Z.

Registriert seit: 3. Dez 2012
Ort: Augsburg, Bayern, Süddeutschland
419 Beiträge
 
Delphi XE4 Ultimate
 
#6

AW: Shape soll Form nicht überschreitten.

  Alt 21. Jun 2013, 02:09
Hallo,

Zitat:
Ich habe ein einfaches Shape welches sich mit der Maus steuern lässt.
Wie du das machst wäre schon interessant.

Zitat:
Allerdings soll es nicht clientheight überschreitten oder oben, über die Form gehen. Ich hab schon vieles probiert, aber nichts davon klappte richtig. Eure Vorschläge bitte
Naja, mit Deinen Vorgaben nicht leicht, aber vielleicht hilft Dir folgendes weiter:

Delphi-Quellcode:
type
  TForm1 = class(TForm)
    Shape1: TShape;
    procedure Shape1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Shape1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    FMouseX : Integer;
    FMouseY : Integer;
    function GetShapeLeft (const X : Integer) : Integer;
    function GetShapeTop (const Y : Integer) : Integer;
    procedure SetMouseXY (const X, Y : Integer);
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetMouseXY (-1, -1)
end;

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  SetMouseXY (X, Y);
end;

procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if FMouseX = -1 then
    Exit;

  Shape1.Left := GetShapeLeft (X);
  Shape1.Top := GetShapeTop (Y);
end;

procedure TForm1.Shape1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  SetMouseXY (-1, -1)
end;

function TForm1.GetShapeLeft (const X : Integer) : Integer;
begin
  Result := 0;
  if X < 0 then
    Exit;

  if Shape1.Left - FMouseX + X < 0 then
    Exit;

  if Shape1.Left + Shape1.Width + X - FMouseX > ClientWidth then
    Result := ClientWidth - Shape1.Width
  else
    Result := Shape1.Left - FMouseX + X
end;

function TForm1.GetShapeTop (const Y : Integer) : Integer;
begin
  Result := 0;
  if Y < 0 then
    Exit;

  if Shape1.Top - FMouseY + Y < 0 then
    Exit;

  if Shape1.Top + Shape1.Height + Y - FMouseY > ClientHeight then
    Result := ClientHeight - Shape1.Height
  else
    Result := Shape1.Top - FMouseY + Y
end;

procedure TForm1.SetMouseXY (const X, Y : Integer);
begin
  FMouseX := X;
  FMouseY := Y
end;
Gruß
Volker Zeller
  Mit Zitat antworten Zitat