按下shift键处理鼠标滚轮事件

时间:2017-07-14 09:48:09

标签: delphi delphi-10.1-berlin

如果在按下Shift键的同时使用鼠标滚轮,我想实现水平滚动。但在这种情况下,我没有收到任何WM_MOUSEWHEEL条消息:

procedure WMMouseWheel(var Msg: TMessage); message WM_MOUSEWHEEL;  // is not called

根据documentationWM_MOUSEWHEEL WPARAM应该有MK_SHIFT条消息。

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

我在代码库中找到了这段代码:

procedure TMyScrollBox.WndProc(var Message: TMessage);
begin
  if Message.Msg=WM_MOUSEHWHEEL then begin
    (* For some reason using a message handler for WM_MOUSEHWHEEL doesn't work.
       The messages don't always arrive. It seems to occur when both scroll bars
       are active. Strangely, if we handle the message here, then the messages
       all get through. Go figure! *)
    if TWMMouseWheel(Message).Keys=0 then begin
      HorzScrollBar.Position := HorzScrollBar.Position + TWMMouseWheel(Message).WheelDelta;
      Message.Result := 0;
    end else begin
      Message.Result := 1;
    end;
  end else begin
    inherited;
  end;
end;

所以,你有它。我不明白为什么会这样,但您应该能够像我一样做,并覆盖WndProc来处理此消息。