How to track scrolling of TScrollBox in Delphi

偶尔善良 提交于 2019-12-03 15:26:11
bummi

This can be done by adding own Events for the messages WM_HSCROLL and WM_HSCROLL.
The example is using a interposer class, this could also be done creating by an own component.
If you don't need two Events, you also can implement only one, beeing called in both message procedures.

unit Unit3;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;

type
  TScrollBox=Class(VCL.Forms.TScrollBox)
    procedure WMHScroll(var Message: TWMHScroll); message WM_HSCROLL;
    procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;
  private
    FOnScrollVert: TNotifyEvent;
    FOnScrollHorz: TNotifyEvent;
  public
   Property OnScrollVert:TNotifyEvent read FOnScrollVert Write FonScrollVert;
   Property OnScrollHorz:TNotifyEvent read FOnScrollHorz Write FonScrollHorz;
  End;

  TForm3 = class(TForm)
    ScrollBox1: TScrollBox;
    Panel1: TPanel;
    Panel2: TPanel;
    ScrollBox2: TScrollBox;
    Panel3: TPanel;
    Panel4: TPanel;
    procedure FormCreate(Sender: TObject);
  private
    procedure MyScrollHorz(Sender: TObject);
    procedure MyScrollVert(Sender: TObject);
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

{ TScollBox }

procedure TScrollBox.WMHScroll(var Message: TWMHScroll);
begin
   inherited;
   if Assigned(FOnScrollHorz) then  FOnScrollHorz(Self);
end;

procedure TScrollBox.WMVScroll(var Message: TWMVScroll);
begin
   inherited;
   if Assigned(FOnScrollVert) then  FOnScrollVert(Self);
end;

procedure TForm3.MyScrollVert(Sender: TObject);
begin
    Scrollbox2.VertScrollBar.Position := Scrollbox1.VertScrollBar.Position
end;

procedure TForm3.MyScrollHorz(Sender: TObject);
begin
    Scrollbox2.HorzScrollBar.Position := Scrollbox1.HorzScrollBar.Position
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  ScrollBox1.OnScrollVert := MyScrollVert;
  ScrollBox1.OnScrollHorz := MyScrollHorz;
end;

end.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!