-1

I have a form with a horizontal scroll bar, but I want to remove the scroll bar by using mouse movement(horizaontally) on the form as a way to scroll. I want that with the movement of the mouse my form will scroll but without lag and smoothly and when I reach the end it stops scrolling. So if anyone could help me then that will be a huge help. Thanks in Advance.

unknown.prince
  • 710
  • 6
  • 19
  • Is the scroll bar explicitly used, or part of another control? Can you give a small example showing what you currently do? – Dsm Jun 13 '17 at 13:24
  • the scroll bar are the parts of the form itself, my form has some extra elements that can't be seen without scrolling – unknown.prince Jun 13 '17 at 13:27
  • It is easy enough to do *except* for hiding the scroll bars. Are you dead set on that? – Dsm Jun 13 '17 at 15:09
  • I am able to hide via Tscrollbar.visible – unknown.prince Jun 13 '17 at 16:10
  • Sadly that doesn't work. Yes you can hide the scroll bars, but despite what the documentation says, that stops the scroll bars working altogether, making the simple solution of moving the scroll bars programmatically unworkable. The documentation says this should work, but It certainly doesn't on 10.1 Berlin. It may on earlier versions. If you want to try it I will post. – Dsm Jun 13 '17 at 18:00
  • i tested its working ( ScrollBy) perhaps on XE8 – unknown.prince Jun 13 '17 at 18:17

1 Answers1

1

This does the screen moving bit nicely on 10.1 Berlin as long as you don't hide the scroll bars. The documentation suggests it should work if you do hide the scroll bars, so maybe on an earlier version of Delphi it will.

OnMouseDown, OnMouseMove and OnMouseUp are used, and 3 local variables.

unit Unit10;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.StrUtils, Vcl.Mask;

type
  TForm10 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    fIsDown : boolean;
    fX, fY : integer;
  public
    { Public declarations }
  end;

var
  Form10: TForm10;

implementation

{$R *.dfm}

procedure TForm10.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Shift = [ssLeft] then  // if ONLY left down
  begin
    // Save co-ordinates
    fIsDown := TRUE;
    fX := X;
    fY := Y;
  end;
end;

procedure TForm10.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if Shift = [ssLeft] then  // if ONLY left down
  begin
    if fIsDown then
    begin
      HorzScrollBar.Position := HorzScrollBar.Position + fX - X;
      VertScrollBar.Position := VertScrollBar.Position + fY - Y;
    end
    else
    begin
      fIsDown := TRUE;
    end;
    fX := X;
    fY := Y;
  end
  else
  begin
    fIsDown := FALSE;
  end;
end;

procedure TForm10.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  fIsDown := FALSE; // regardless of shift state!
end;

end.

Please let us know if hiding the scroll bars works on XE8, as this would be useful for future readers.

Dsm
  • 5,870
  • 20
  • 24
  • Scroll bars can move the content while dragging the scroll thumb if you set the `Tracking` property to True without any extra code. This code will scroll form content when dragging non-focusable client area. If that's what OP wants, maybe it's time to clarify the question a bit. – Victoria Jun 14 '17 at 04:15
  • For something you can call really _"visually smooth scrolling"_ I would follow [this post](https://stackoverflow.com/a/9503002/8041231). – Victoria Jun 14 '17 at 04:27
  • @Victoria. This gives really *"visually good scrolling"* and is no different to the first of the 3 solutions in your link (TForm and TScrollingWindow" have the same ancestor responsible for scrolling. The 2nd and 3rd forms are good alternatives but require extra code to make sure that you don't travel too far.. Using a timer does not give you any improvement in responsiveness. – Dsm Jun 14 '17 at 08:43
  • How? What you provided here just allows you to drag the content by client non-focusable rectangle. Code from that link provides a cool animation :) – Victoria Jun 14 '17 at 08:47