Ige*_*bam 3 delphi delphi-7 delphi-2009 delphi-2010
我现在正在做一个小项目,我想同步 5 个列表框一起滚动。列表框的名称是:
KidList
PointList
NoteList
CommentList
CommentListKid
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
And*_*and 10
您可以尝试以下技术。
首先,添加一个私有字段
private
SyncBoxes: TArray<TListBox>;
Run Code Online (Sandbox Code Playgroud)
到您的表单并在创建表单时对其进行初始化:
procedure TForm1.FormCreate(Sender: TObject);
begin
SyncBoxes := [ListBox1, ListBox2, ListBox3, ListBox4];
end;
Run Code Online (Sandbox Code Playgroud)
然后定义以下中介层类:
type
TListBox = class(Vcl.StdCtrls.TListBox)
strict private
procedure Sync;
protected
procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;
procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
procedure WMMouseWheel(var Message: TWMMouseWheel); message WM_MOUSEWHEEL;
end;
Run Code Online (Sandbox Code Playgroud)
实施为
procedure TListBox.CNCommand(var Message: TWMCommand);
begin
inherited;
if Message.NotifyCode = LBN_SELCHANGE then
Sync;
end;
procedure TListBox.Sync;
var
LB: TListBox;
begin
for LB in Form1.SyncBoxes do
if LB <> Self then
LB.TopIndex := Self.TopIndex;
end;
procedure TListBox.WMMouseWheel(var Message: TWMMouseWheel);
begin
inherited;
Sync;
end;
procedure TListBox.WMVScroll(var Message: TWMVScroll);
begin
inherited;
Sync;
end;
Run Code Online (Sandbox Code Playgroud)
当然,在真正的应用程序中,你会重构它。
结果可能已经足够好了:
然而,列表框的滚动动画使同步有点延迟。