或者您可以尝试稍微修改一下 VirtualTree。在下面的示例中使用了插入类。如果您将此代码粘贴到您的单元中,那么您的所有 VirtualTrees 都会在表单中以这种方式运行。
uses
VirtualTrees;
type
TVirtualStringTree = class(VirtualTrees.TVirtualStringTree)
private
FMouseInside: Boolean;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure CMMouseWheel(var Message: TCMMouseWheel); message CM_MOUSEWHEEL;
end;
implementation
procedure TVirtualStringTree.CMMouseEnter(var Message: TMessage);
begin
inherited;
// SetFocus will set the focus to the tree which is entered by mouse
// but it's probably what you don't want to, if so, just remove the
// following line. If you want to scroll the tree under mouse without
// stealing the focus from the previous control then this is not the
// right way - the tree must either be focused or you can steal it by
// the SetFocus. This only resolves the case when you have a focused
// tree and leave it with the mouse, then no scrolling is performed,
// if you enter it, you can scroll again.
SetFocus;
// set the flag which tells about mouse inside
FMouseInside := True;
end;
procedure TVirtualStringTree.CMMouseLeave(var Message: TMessage);
begin
// reset the flag about mouse inside
FMouseInside := False;
inherited;
end;
procedure TVirtualStringTree.CMMouseWheel(var Message: TCMMouseWheel);
begin
// if mouse is inside then let's wheel the mouse otherwise nothing
if FMouseInside then
inherited;
end;