4

I wrote the following code: procedure MouseWheel(var Msg:TWMMouseWheel);message WM_MOUSEHWHEEL; I used it for a component based on TPanel (TMyP=class(TPanel)) (Notice that I don't want to use TCustomPanel due to my own reasons)

But anyway the event is not called when I use mouse wheel on the panel. Please Help me!

Javid
  • 2,755
  • 2
  • 33
  • 60
  • http://stackoverflow.com/questions/5297234/tlistview-and-mouse-wheel-scrolling – Shannon Matthews Jul 02 '13 at 01:49
  • See [How to add mouse wheel support to a component descended from TGraphicControl?](http://stackoverflow.com/a/34463279/757830), and [How to direct the mouse wheel input to control under cursor instead of focused?](http://stackoverflow.com/a/34386680/757830) – NGLN Dec 25 '15 at 19:15

2 Answers2

13

The mouse wheel messages are sent to the control with the focus. And panels usually aren't focusable.

I use this TApplicationEvents.OnMessage handler in my applications to send the mouse wheel message to the window under the mouse cursor instead of the focused control.

procedure TMainDataModule.ApplicationEventsMessage(var Msg: tagMSG; var Handled: Boolean);
var
  Wnd: HWND;
begin
  if Msg.message = WM_MOUSEWHEEL then
  begin
    Wnd := WindowFromPoint(Msg.pt);
    // It must be a VCL control otherwise we could get access violations
    if IsVCLControl(Wnd) then
      Msg.hwnd := Wnd; // change the message receiver to the control under the cursor
  end;
end;
NGLN
  • 43,011
  • 8
  • 105
  • 200
Andreas Hausladen
  • 8,081
  • 1
  • 41
  • 44
  • I wonder if this doesn't break consistent user experience, where the user is used to have mouse wheel messages going to the focused control and apparently in your application they go somewhere else. – Eugene Mayevski 'Callback Dec 18 '10 at 12:14
  • Actually this was a user request brought to me by our product management. – Andreas Hausladen Dec 18 '10 at 17:14
  • I tried this in C++ Builder but it doesn't work. I also tried a couple of variants similar to this but none worked. I guess it works in Delphi but not in C++ Builder. I also checked for WM_VSCROLL and WM_HSCROLL but no effect. Any ideas why? – Coder12345 Jul 10 '13 at 15:38
4

In addition to Andreas Hausladen's answer you need to know, that some mouse drivers don't send WM_MOUSEWHEEL and instead send several WM_VSCROLL messages. You need to check this as well.

Upd: Note, that there exist also WM_HSCROLL messages which can also be sent by some mice which have two wheels or a tilting wheel. That's why I wrote WM_SCROLL initially.

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121