Drag content of ScrollBox

Hi Experts, I need drag content of a ScrollBox somethink like this:

procedure TForm1.ScrollBox1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
     if ssRight in shift then
     with scrollBox1 do
     begin
          horzScrollBar.Position := horzScrollBar.Position + org.x-x;
          vertScrollBar.Position := horzScrollBar.Position + org.y-y;
          org := point(x,y);
     end;
end;

procedure TForm1.ScrollBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
     if ssRight in shift then
        org := point(x,y);
end;

But I need to do this with ScrollBy because the scrollbars of ScrollBox is invisible (I do not want the scrollbars appear)

Some Idea
Thanks
alpiresAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ephraim WangoyaCommented:
Here is an example using a TTimer, I left the scrollbars visible so you can see the effect

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TForm1 = class(TForm)
    ScrollBox1: TScrollBox;
    Panel1: TPanel;
    Timer1: TTimer;
    Panel2: TPanel;
    Panel3: TPanel;
    procedure ScrollBox1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure ScrollBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure Timer1Timer(Sender: TObject);
    procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ScrollBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Source is TPanel);
end;

procedure TForm1.ScrollBox1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
  if Source is TPanel then
  begin
    TPanel(Source).Left := X;
    TPanel(Source).Top := Y;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var 
  P: TPoint;
begin
  GetCursorPos(P);
  P:= ScreenToClient(P);
  with Scrollbox1.BoundsRect, P do
    if X < Left then
      Scrollbox1.Perform(WM_HSCROLL, SB_LINELEFT, 0)
    else if X > Right then
      Scrollbox1.Perform(WM_HSCROLL, SB_LINERIGHT, 0)
    else if Y < Top then
      Scrollbox1.Perform(WM_VSCROLL, SB_LINEUP, 0)
    else if y > bottom then
      Scrollbox1.Perform(WM_VSCROLL, SB_LINEDOWN, 0)
    else
      Timer1.enabled := false;
end;

procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  EnableTimer: Boolean;
begin
  Accept := False;
  if State = dsDragLeave then
    Timer1.Enabled := false
  else
    if (Source is TPanel) then
    begin
      with Scrollbox1.BoundsRect do
        EnableTimer := (X < Left) or (X > Right) or (Y < Top) or (Y > Bottom);
      Timer1.Enabled := EnableTimer;
    end;
end;

end.

Open in new window


Unit1.dfm
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 615
  Height = 403
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnDragOver = FormDragOver
  PixelsPerInch = 96
  TextHeight = 13
  object ScrollBox1: TScrollBox
    Left = 136
    Top = 24
    Width = 337
    Height = 297
    DragMode = dmAutomatic
    TabOrder = 0
    OnDragDrop = ScrollBox1DragDrop
    OnDragOver = ScrollBox1DragOver
    object Panel1: TPanel
      Left = 40
      Top = 24
      Width = 185
      Height = 41
      Caption = 'Panel1'
      DragMode = dmAutomatic
      TabOrder = 0
    end
    object Panel2: TPanel
      Left = 40
      Top = 573
      Width = 35
      Height = 24
      Caption = 'Panel2'
      TabOrder = 1
    end
    object Panel3: TPanel
      Left = 485
      Top = 24
      Width = 25
      Height = 25
      Caption = 'Panel3'
      TabOrder = 2
    end
  end
  object Timer1: TTimer
    Interval = 200
    OnTimer = Timer1Timer
    Left = 72
    Top = 24
  end
end

Open in new window

0
alpiresAuthor Commented:
Hi ewangoya, thanks for you example, but i dont need drag and drop objects inside scrollbox, I need to drag and drop the entire scrollbox just like scrollbars do, but I dont want use the scrollbars, the scrollbars must be invisible.

Thanks
0
Sinisa VukSoftware architectCommented:
simple example to move contents of scrollbox:

  TForm1 = class(TForm)
....
  public
    { Public declarations }
    OldBoxPos: TPoint;
  end;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
  ScrollBox1.Tag := 0;
  ScrollBox1.VertScrollBar.Visible := False;
  ScrollBox1.HorzScrollBar.Visible := False;
end;

procedure TForm1.ScrollBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  ScrollBox1.Tag := 1;
  OldBoxPos := Point(X, Y);
end;

procedure TForm1.ScrollBox1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  if ScrollBox1.Tag > 0 then
  begin
    ScrollBox1.ScrollBy(X - OldBoxPos.X, Y - OldBoxPos.Y);
    //ScrollBox1.ScrollBy(0, Y - OldBoxPos.Y); //only vertical
    OldBoxPos := Point(X, Y);
  end;
end;

procedure TForm1.ScrollBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  ScrollBox1.Tag := 0;
end;

Open in new window


if you want to scroll with limits (min/max point to left/rigth/top/bottom) then some checking must be add in mousemove event.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.