WMMouseWheel not working in Delphi
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开发者_Python百科 reasons)
But anyway the event is not called when I use mouse wheel on the panel. Please Help me!
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;
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.
精彩评论