How to add mouse wheel support to a component descended from TGraphicControl?
Asked Answered
A

6

4

I have created a delphi component which descends from TGraphicControl. Is it possible to add support for mouse wheels?

--- Edit ---

I've exposed the MouseWheel events as shown below but they aren't called.

TMyComponent = class(TGraphicControl)
published
  property OnMouseWheel;
  property OnMouseWheelDown;
  property OnMouseWheelUp;
end;

--- Edit ---

As suggested below, I've tried to trap the WM_MOUSEWHEEL and CM_MOUSEWHEEL messages, but it doesn't seem to work. However I've managed to trap the CM_MOUSEENTER message. I don't understand why i can trap one type of message, but not the other.

Adamo answered 19/1, 2009 at 3:58 Comment(0)
K
6

Due to several VCL constructs (whether they are deliberate implementation choices or may possibly be bugs1), I leave in the middle) only the focused control and all its parents get mouse wheel messages, as well as the control which has the mouse captured ánd has a focused parent.

At the TControl level, the latter condition can be enforced. A control receives a CM_MOUSEENTER message from the VCL when the mouse enters the client space of the control. To force it to receive mouse wheel messages, focus its parent and capture the mouse in that message handler:

procedure TWheelControl.CMMouseEnter(var Message: TMessage);
begin
  FPrevFocusWindow := SetFocus(Parent.Handle);
  MouseCapture := True;
  inherited;
end;

But these settings must be undone when the mouse exits the control. Since the control is now capturing the mouse, CM_MOUSELEAVE is not received by it, so you have to manually check this, for example in the WM_MOUSEMOVE message handler:

procedure TWheelControl.WMMouseMove(var Message: TWMMouseMove);
begin
  if MouseCapture and
    not PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
  begin
    MouseCapture := False;
    SetFocus(FPrevFocusWindow);
  end;
  inherited;
end;

Now, you would assume the wheel messages received by the control will subsequently fire the OnMouseWheel, OnMouseWheelDown and OnMouseWheelUp events. But noooo, one more intervention is needed. The message enters the control in MouseWheelHandler which happens to pass the message on to either the form or active control. To get these events fired, a CM_MOUSEWHEEL control message should be sent:

procedure TWheelControl.MouseWheelHandler(var Message: TMessage);
begin
  Message.Result := Perform(CM_MOUSEWHEEL, Message.WParam, Message.LParam);
  if Message.Result = 0 then
    inherited MouseWheelHandler(Message);
end;

Which results in this final code:

unit WheelControl;

interface

uses
  System.Classes, Winapi.Windows, Winapi.Messages, Vcl.Controls;

type
  TWheelControl = class(TGraphicControl)
  private
    FPrevFocusWindow: HWND;
    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
    procedure WMMouseMove(var Message: TWMMouseMove); message WM_MOUSEMOVE;
  public
    procedure MouseWheelHandler(var Message: TMessage); override;
  published
    property OnMouseWheel;
    property OnMouseWheelDown;
    property OnMouseWheelUp;
  end;

implementation

{ TWheelControl }

procedure TWheelControl.CMMouseEnter(var Message: TMessage);
begin
  FPrevFocusWindow := SetFocus(Parent.Handle);
  MouseCapture := True;
  inherited;
end;

procedure TWheelControl.MouseWheelHandler(var Message: TMessage);
begin
  Message.Result := Perform(CM_MOUSEWHEEL, Message.WParam, Message.LParam);
  if Message.Result = 0 then
    inherited MouseWheelHandler(Message);
end;

procedure TWheelControl.WMMouseMove(var Message: TWMMouseMove);
begin
  if MouseCapture and
    not PtInRect(ClientRect, SmallPointToPoint(Message.Pos)) then
  begin
    MouseCapture := False;
    SetFocus(FPrevFocusWindow);
  end;
  inherited;
end;

end.

As you see, this changes the focused control, which is against the user experience guidelines for Windows-based desktop applications and might result in visual distractions when the focused control had an explicit focused state.

As an alternative, you can bypass all default VCL mouse wheel handling by overriding Application.OnMessage and deal with it there. This might be done as follows:

unit WheelControl2;

interface

uses
  System.Classes, Winapi.Windows, Winapi.Messages, Vcl.Controls, Vcl.AppEvnts,
  Vcl.Forms;

type
  TWheelControl = class(TGraphicControl)
  published
    property OnMouseWheel;
    property OnMouseWheelDown;
    property OnMouseWheelUp;
  end;

implementation

type
  TWheelInterceptor = class(TCustomApplicationEvents)
  private
    procedure ApplicationMessage(var Msg: tagMSG; var Handled: Boolean);
  public
    constructor Create(AOwner: TComponent); override;
  end;

procedure TWheelInterceptor.ApplicationMessage(var Msg: tagMSG;
  var Handled: Boolean);
var
  Window: HWND;
  WinControl: TWinControl;
  Control: TControl;
  Message: TMessage;
begin
  if Msg.message = WM_MOUSEWHEEL then
  begin
     Window := WindowFromPoint(Msg.pt);
     if Window <> 0 then
     begin
       WinControl := FindControl(Window);
       if WinControl <> nil then
       begin
         Control := WinControl.ControlAtPos(WinControl.ScreenToClient(Msg.pt),
           False);
         if Control <> nil then
         begin
           Message.WParam := Msg.wParam;
           Message.LParam := Msg.lParam;
           TCMMouseWheel(Message).ShiftState :=
             KeysToShiftState(TWMMouseWheel(Message).Keys);
           Message.Result := Control.Perform(CM_MOUSEWHEEL, Message.WParam,
             Message.LParam);
           Handled := Message.Result <> 0;
         end;
       end;
     end;
  end;
end;

constructor TWheelInterceptor.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  OnMessage := ApplicationMessage;
end;

initialization
  TWheelInterceptor.Create(Application);

end.

Be careful to set the Handled parameter of the MouseWheel* event to True, otherwise the focused control will scroll as well.

See also How to direct the mouse wheel input to control under cursor instead of focused? for more background on mouse wheel handling and a more general solution.

1) See Quality Central bug report #135258, and Quality Central bug report #135305.

Kerbela answered 25/12, 2015 at 13:29 Comment(0)
M
3

TGraphicControl descends from TControl, which already has mouse-wheel support. See the wm_MouseWheel message, the DoMouseWheel, DoMouseWheelDown, DoMouseWheelUp, and MouseWheelHandler methods, and the WheelAccumulator property.

Moyer answered 19/1, 2009 at 5:38 Comment(2)
Something is still missing, after re-declaring the MouseWheel events as shown above, they still aren't called.Adamo
Looks like this is more complicated than I thought. I'll try writing some code to figure this out. Try back in a couple of days.Moyer
S
1

Only TWinControl descendants can receive mouse wheel messages. TGraphicControl is not a Window based control and therefore can not. It could work if the VCL routes the messages to the TGraphicControl, but apparently does not. You could descend from TCustomControl and then it would work.

Scorecard answered 19/1, 2009 at 12:9 Comment(3)
TGraphicControl gets messages all the time, just not directly from the OS. The parent control catches and forwards them. Sometimes, they arrive as CM_ messages instead of WM_. It looks like TControl.MouseWheelHandler forwards wheel messages to the form, though, so Shannon may need to override that.Moyer
I tried overriding the MouseWheelHandler method, but that wasn't being called consistently. Sometimes the messages would still be passed on to the underlying form.Adamo
I changed my component to descend from TCustomControl (my control needs a canvas to draw on) but the MouseWheel event handlers were still ignored. (I'm starting to feel a bit dumb here. lol) – Shannon (0 secs ago)Adamo
T
1

I have the same problem. No luck finding solution yet, but maybe this will be helpful:

I suspect the other component is calling the Win API method SetCapture, which according to the API help:

"The SetCapture function sets the mouse capture to the specified window belonging to the current thread. Once a window has captured the mouse, all mouse input is directed to that window, regardless of whether the cursor is within the borders of that window. Only one window at a time can capture the mouse. "

As a new user I can not post a link to the full thread.

EDITED

If you create your component as a descendant from TCustomControl you can solve your problem as described below:

  1. Use OnMouseEnter event to detect when mouse enters your component.
  2. In OnMouseEnter call SetFocus method to make your component focused. Now your component can receive WM_MOUSEWHEEL message
Tiffa answered 27/5, 2009 at 12:54 Comment(5)
I've experimented a bit more and the problem is that mine (and probably yours) component doesn't have a focus. if you call a SetFocusedControl(YourComponent) method, then your component will start receiving messages from the mouse wheel.Tiffa
Solution for this problem could be like this: 1. Use OnMouseEnter event to detect when mouse enters your component. 2. In OnMouseEnter call SetFocus method to make your component focused. Now your component can receive WM_MOUSEWHEEL message.Tiffa
@Wodzu: A TGraphicControl can not receive focus, as it has no window handle.Giliane
@mghie: Shannon mentioned that he changed parent class to TCustomControl so it shouldn't be a problem.Tiffa
+1, looks like focus is needed to receive mousewheel events (I've just tried)Godmother
K
0

Trap the WM_MOUSEWHEEL message.

Koo answered 19/1, 2009 at 5:36 Comment(1)
I tried this, but my control wouldn't trap the WM_MOUSEWHEEL or CM_MOUSEWHEEL message. But it worked as expected when attempting to trap the CM_MOUSEENTER message.Adamo
P
0

I'm using the following technique, I subscribe to the form event MouseWheelUp() and inside it, I search for widget with WindowFromPoint() (win32 api function) and Vcl.Controls.FindControl(), then I check if I got the right UI widget, when I don't I check for ActiveControl (widget on the form which currently has focus).

This technique ensures, that the mouse wheel up/down event works when the widget is under the cursor or when it's not under the cursor, but has focus.

The example below reacts to the mouse wheel up event and increments TSpinEdit when TSpinEdit is under the cursor or has a focus.

function TFormOptionsDialog.FindSpinEdit(const AMousePos: TPoint): TSpinEdit;
var
  LWindow: HWND;
  LWinControl: TWinControl;
begin
  Result := nil;

  LWindow := WindowFromPoint(AMousePos);
  if LWindow = 0 then
    Exit;

  LWinControl := FindControl(LWindow);
  if LWinControl = nil then
    Exit;

  if LWinControl is TSpinEdit then
    Exit(LWinControl as TSpinEdit);

  if LWinControl.Parent is TSpinEdit then
    Exit(LWinControl.Parent as TSpinEdit);

  if ActiveControl is TSpinEdit then
    Exit(ActiveControl as TSpinEdit);
end;

procedure TFormOptionsDialog.FormMouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint;
  var Handled: Boolean);
var
  LSpinEdit: TSpinEdit;
begin
  LSpinEdit := FindSpinEdit(MousePos);
  if LSpinEdit = nil then
    Exit;

  LSpinEdit.Value := LSpinEdit.Value + LSpinEdit.Increment;
  Handled := True;
end;
Puffery answered 2/9, 2020 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.