7

TVirtualStringTree 默认情况下,如果它被聚焦,它会在鼠标滚轮上滚动,即使鼠标没有被控制(除非它在另一个 TVirtualStringTree 上)。

有没有一种快速而优雅的方法来禁用这种行为?

我已经用OnMouseWheel事件做了这个,并检查它PtInRect是否Mouse.CursorPos在一个控件上,但我觉得有更好的方法来做同样的事情,因为这样我必须为我添加的每个 TreeView 定义一个新事件,而且处理何时聚焦/取消聚焦控件,所以我希望必须有更好的方法来禁用它。

所以要清楚,我希望鼠标滚轮功能像往常一样工作,但只有当鼠标悬停在 VirtualTreeView 上时。

4

2 回答 2

3

或者您可以尝试稍微修改一下 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;
于 2011-12-02T07:43:20.687 回答
3

将 TApplicationEvents 控件下拉到窗体

在 TApplicationEvents onMessage

 procedure TForm5.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
 var
  pnt: TPoint;
  ctrl: TWinControl;
 begin
  if Msg.message = WM_MOUSEWHEEL then
  begin
    if not GetCursorPos(pnt) then Exit;
    ctrl := FindVCLWindow(pnt);
    if Assigned(ctrl) then
      Msg.hwnd := ctrl.Handle;
  end;
 end;
于 2011-12-02T04:18:07.460 回答