6

Hi I have a form with several frames inside.

For some of the frames, i wish to scroll the contents (or at least handle the mousewheel event).

I have tried the following:

Simply assigning a OnMouseWheel event handler for each frame

Overriding the MouseWheel event for the parent form:

procedure TFmReview.MouseWheelHandler(var Message: TMessage);
var   Control: TControl;
begin
    Control := ControlAtPos(ScreenToClient(SmallPointToPoint(TWMMouseWheel(Message).Pos)), False, True);
    if Assigned(Control) and (Control <> ActiveControl) then
    begin
         ShowMessage(Control.Name);
         Message.Result := Control.Perform(CM_MOUSEWHEEL, Message.WParam, Message.LParam);
         if Message.Result = 0 then
            Control.DefaultHandler(Message);
     end else inherited MouseWheelHandler(Message);
end;

Unfortunately both dont seem to work.

  • In case 1, the event is never triggered, however the parent forms mouse wheel handler is triggered.
  • In case 2, the control that receives focus is the panel that holds the frame i wish to send the mousewheel event to.

So, put simply, how can i direct the mousewheel event to the top most control that the mouse cursor is over (regardless of which frame/parent/form etc the cursor is in)?

menjaraz
  • 7,551
  • 4
  • 41
  • 81
Simon
  • 9,197
  • 13
  • 72
  • 115
  • Take a look at [these SO answers][1] they will probably help. [1]: http://stackoverflow.com/questions/2472743/what-is-the-best-method-for-implementing-mouse-wheel-activity-in-delphi-vcl-form – Brian Frost Nov 10 '11 at 08:52
  • possible duplicate of [How to direct the mouse wheel input to control under cursor instead of focused?](http://stackoverflow.com/questions/2251019/how-to-direct-the-mouse-wheel-input-to-control-under-cursor-instead-of-focused) – Kromster Feb 24 '14 at 06:51

1 Answers1

1

To postpone mouse wheel handling to a TWinControl over which is currently mouse cursor, override in your main frame form the MouseWheelHandler method using a code like this:

type
  TMainForm = class(TForm)
  private
    procedure MouseWheelHandler(var AMessage: TMessage); override;
  public
    { Public declarations }
  end;

implementation

procedure TMainForm.MouseWheelHandler(var AMessage: TMessage);
var
  Control: TWinControl;
begin
  Control := FindVCLWindow(SmallPointToPoint(TWMMouseWheel(AMessage).Pos));
  if Assigned(Control) then
  begin
    AMessage.Result := Control.Perform(CM_MOUSEWHEEL, AMessage.WParam,
      AMessage.LParam);
    if AMessage.Result = 0 then
      Control.DefaultHandler(AMessage);
  end
  else
    inherited MouseWheelHandler(AMessage);
end;
TLama
  • 75,147
  • 17
  • 214
  • 392