Mega Code Archive

 
Categories / Delphi / Hardware
 

Redirect Mouse Wheel Message To A Control Under The Mouse in Delphi Applications

Title: Redirect Mouse Wheel Message To A Control Under The Mouse in Delphi Applications Mouse wheel is used for scrolling. When a scrollable control (TMemo, TListView, TTreeView, ...) has the input focus, moving the wheel will result in vertical scrolling of the content of the focused control. A nice feature of the Delphi IDE is that scrolling the mouse wheel will work for elements of the IDE that are not focused: you can scroll the Unit Editor even if it does not have the input focus. Many other Windows applications have changed the default mouse wheel behaviour for input controls: when the wheel is spun, the control beneath the mouse is scrolled - never mind which control actually has the focus. Mouse Wheel Scrolling For The Control Under The Mouse Here's how to enhance the functionality of mouse wheel so that moving the mouse wheel scrolls the control directly under the mouse cursor and not the one with the input focus, which is default on Windows. Drop a TApplicationEvents component on the main form of your Delphi application, and have the OnMessage event handler look like this: //TApplicationEvents.OnMessage procedure TMainForm.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); var mousePos: TPoint; wc: TWinControl; begin //mouse wheel scrolling for the control under the mouse if Msg.message = WM_MOUSEWHEEL then begin mousePos.X := Word(Msg.lParam); mousePos.Y := HiWord(Msg.lParam); wc := FindVCLWindow(mousePos); if wc = nil then Handled := True else if wc.Handle Msg.hwnd then egin SendMessage(wc.Handle, WM_MOUSEWHEEL, Msg.wParam, Msg.lParam); Handled := True; end; end; end; The OnMessage event of the TApplicationEvents component traps all Windows messages posted to all windows/controls in the application. When the WM_MOUSEWHEEL message is posted to the application, by using the FindVCLWindow function is used to locate the control that has the input focus. If the mouse is not above the control with the input focus, the mouse wheel message is redirected to the control under the mouse.