TGraphicControl如何捕获鼠标滚轮事件(WMMouseWheel)

TGraphicControl默认情况下无法捕获鼠标滚轮事件,如果需要处理鼠标滚轮事件可以采用以下方式处理:

1、在CMMouseenter事件中捕获鼠标,以获得后续的鼠标事件处理权

procedure TTestControl.CMMouseenter(var Message: TMessage);
begin
  FPrevFocusHwnd := SetFocus(Parent.Handle);
  MouseCapture := True;
  inherited;
end;

2、在鼠标离开控件区域时释放鼠标

procedure TTestControl.WMMouseMove(var Message: TWMMouseMove);
begin
  if MouseCapture and not PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
  begin
    MouseCapture := False;
    SetFocus(FPrevFocusHwnd);
  end;
  inherited;
end;

经过上述方式处理后就可以在TGraphicControl中响应鼠标滚轮事件了(DoMouseWheel、DoMouseWheelDown、DoMouseWheelUp)。

以下是Delphi帮助文件中对属性MouseCapture的说明:

Specifies whether the control has "captured" mouse events.

Use MouseCapture to determine whether a control has captured the mouse. When a control captures the mouse,
all subsequent mouse events go to that control until the user releases the mouse button.

A control captures the mouse when the user drags an item from it. In addition, if the control has the csCaptureMouse flag set in its ControlStyle property,
it captures the mouse when the user presses the left mouse button over it, until the user releases the mouse button.

大意是说:

MouseCapture指定控件是否“捕获”了鼠标事件。
使用MouseCapture确定控件是否捕获了鼠标。当控件捕获鼠标时,所有后续的鼠标事件都将转到该控件,直到用户释放鼠标按钮为止。
控件在用户从中拖动项目时捕获鼠标。此外,如果控件在其ControlStyle属性中设置了csCaptureMouse标志,则当用户在其上按鼠标左键时,
它将捕获鼠标,直到用户释放鼠标按钮为止。

posted @ 2021-02-24 16:31  Huixch  阅读(138)  评论(0编辑  收藏  举报