Form uzerindeki bir nesneyi mouse ile tutup suruklemek

Başlatan bunalmis, 26 Nisan 2010, 10:48:45

z

Form uzerindeki bir nesneyi mouse ile tutup suruklemek istiyorum.

Amacim standart scrollbar komponentine gore gorsel olarak daha yakisikli bir sey olusturmak.

Surugulu cubugu olusturdum ancak surgusunu mouse ile tuttuktan sonra nasil saga sola kaydiracagim bilemiyorum.
Bana e^st de diyebilirsiniz.   www.cncdesigner.com

SpeedyX

#1
Mouse tıklanma eventinde eğer mouse scrollbarın üzerinde ise Y ye dokunmayıp X i mouse X i ile orantılı değiştirebilirsin.

Drag and drop dan da yararlanabilirsin.

Buradaki gibi mesajlaşma yolu ile de yapılabilir.

type
  TForm1 = class(TForm)
  ...
  private
    { Private declarations }
    Dragged: Boolean;
    OldPos: TPoint;
  ...

procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if RadioGroup1.ItemIndex=0 then
  begin
    Dragged:=True;
    GetCursorPos(OldPos);
    SetCapture(Button1.Handle);
  end;
end;

procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  NewPos: TPoint;
begin
  if Dragged then
    with Button1 do
    begin
      GetCursorPos(NewPos);
      Left:=Left-OldPos.X+NewPos.X;
      Top:=Top-OldPos.Y+NewPos.Y;
      OldPos:=NewPos;
    end;
end;

procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Dragged then
  begin
    ReleaseCapture;
    Dragged:=False;
  end;
end;


http://delphi.about.com/library/weekly/aa102505a.htm

This code will move any control at runtime, even non TWinControl descendants.

Make sure ExtCtrls is in your USES clause.
Then set the OnMouseDown event of your controls to the following code.

procedure TForm1.MoveControl(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  TempPanel : TPanel;
  Control : TControl;
begin
  //Release the MOUSEDOWN status
  ReleaseCapture;
  if Sender is TWinControl then
    //Component has a Handle, move it directly
    TWincontrol(Sender).Perform(WM_SYSCOMMAND,$f019,0)
  else
    //Component has no handle, move it in a TPanel
    try
      Control := TControl(Sender);
      TempPanel := TPanel.Create(Self);
      with TempPanel do
      begin
     //Replace component with TempPanel
        Caption := '';
        BevelOuter := bvNone;
        SetBounds(Control.Left,Control.Top,
        Control.Width,Control.Height);
        Parent := Control.Parent;

        //Put our control in the TempPanel
        Control.Parent := TempPanel;

        //Move TempPanel with the control inside it
        Perform(WM_SYSCOMMAND,$F019,0);

        //Put the component where the panel was dropped
      Control.Parent := Parent;
        Control.Left := Left;
        Control.Top := Top;
      end;
    finally
      TempPanel.Free;
    end;
end;