如何在Delphi中跟踪TScrollBox的滚动 [英] How to track scrolling of TScrollBox in Delphi
问题描述
有没有一种简单的方法可以使用其滚动条来跟踪TScrollbox内容的滚动?
我有几个TScrollBox组件(每个组件内部都有一些组件),并希望保持它们同步。如果其中一个滚动框滚动(垂直或水平),则需要同步滚动其他滚动框。这就是为什么我需要知道滚动条位置何时更改。
很奇怪,但是Delphi的TScrollbox组件没有此类事件。
Is there any simple way to track scrolling of TScrollbox content with his scrollbars ? I have several TScrollBox components (each of them has some components inside) and would like to keep them synchronous. If one of scrollboxes scrolled (vertically or horizontally) i need to scroll other scrollboxes synchronously. That is why i need to know when scrollbars positions are changed. It is strange, but Delphi's TScrollbox component doesn't have such events.
推荐答案
这可以通过为消息 WM_HSCROLL
添加自己的事件来完成和 WM_HSCROLL
。
该示例使用插入器类,也可以由自己的组件创建。
如果
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.
这篇关于如何在Delphi中跟踪TScrollBox的滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!