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

后端 未结 6 1154
悲&欢浪女
悲&欢浪女 2020-11-28 15:20

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

--- Edit ---

I\'ve exposed the Mou

6条回答
  •  囚心锁ツ
    2020-11-28 15:56

    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;
    

提交回复
热议问题