Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - How to capture WheelMouse for controls that do not support it ...

1 Posts
1 Users
0 Reactions
1,718 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2860
Topic starter  

With some help from the Lazarus Forum (thank you engkin!), I finally managed to capture mouse wheel movement for controls that do not support it. 

In the code below, we define a new class that we hook up to controls to their WindowProc, so we can intercept messages, and relay them the usual way after we're done with them.

The usage is straight forward:

  1. Make sure Controls and LMessages are added the uses clause.
  2. Copy and paste the type and implementation code below.
  3. Modify the AdjustedWindowProc to your needs.
  4. For each control that needs to catch WheelMouse events add: TWheelCatcher.Create(<componentname>);
uses
  ..., Controls, LMessages; type ...
  { TWheelCatcher }
  TWheelCatcher=class
  public
    PrvWindowProc: TWndMethod;
    WinControl:TWinControl;
    procedure AdjustedWindowProc(var TheMessage: TLMessage);
    constructor Create(Target: TWinControl);
  end;
  
...
implementation
...
{ TWheelCatcher }
procedure TWheelCatcher.AdjustedWindowProc(var TheMessage: TLMessage);
begin
  if TheMessage.msg = LM_MOUSEWHEEL then
  begin
    //Do what you want here //Read the "TheMessage" values to determine if the wheel goes up or down ShowMessage('Wheel event!');
  end;
  PrvWindowProc(TheMessage);
end;
constructor TWheelCatcher.Create(Target: TWinControl);
begin
  WinControl := Target;
  PrvWindowProc := WinControl.WindowProc;
  WinControl.WindowProc := @AdjustedWindowProc;
end;  
... // For whatever control you'd like to use it, for example myPanel is a TPanel
// so for that one you do the following during the Form onCreate event.
TWheelCatcher.Create(myPanel);
// or when you create a control during runtime:
  tmpPanel := TPanel.Create(ParentPanel);
  tmpPanel.Parent := ParentPanel;
  ...
  TWheelCatcher.Create(tmpPanel); 
  ... 
...

   
ReplyQuote
Share: