Как отключить MouseWheel, если мышь не находится над VirtualTreeView (TVirtualStringTree) - PullRequest
7 голосов
/ 02 декабря 2011

TVirtualStringTree ведет себя по умолчанию, если оно сфокусировано - оно будет прокручиваться на колесе мыши, даже если мышь не находится под контролем (кроме случаев, когда она находится над другим TVirtualStringTree).

Существует ли быстрый и элегантный способ отключитьэто поведение?

Я уже делал это с событием OnMouseWheel и проверял с помощью PtInRect, если Mouse.CursorPos, если он находится над контролем, но у меня есть ощущение, что есть лучший способ сделать то же самое, потому чтотаким образом, я должен был бы определить новое событие для каждого TreeView, который я добавляю, а также обработать, когда нужно сфокусировать / расфокусировать элемент управления, так что я надеюсь, что должен быть лучший способ отключить это.

Итак, чтобы было ясно,Я хочу, чтобы функция колесика мыши работала как обычно, но только когда мышь находится над VirtualTreeView.

Ответы [ 2 ]

3 голосов
/ 02 декабря 2011

Или вы можете попытаться немного изменить VirtualTree.В следующем примере используется вставленный класс.Если вы вставите этот код в свой модуль, то все ваши виртуальные деревья будут вести себя таким образом в форме.

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;
3 голосов
/ 02 декабря 2011

Выпадающий элемент управления 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;
...