2
votes

TDBCtrlGrid does not react to the mouse wheel at all.

I tried this:

procedure TForm1.FormMouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
begin
  if DBCtrlGrid1.ClientRect.Contains(DBCtrlGrid1.ScreenToClient(MousePos)) then
  begin
    DBCtrlGrid1.ScrollBy(0, WheelDelta);
    Handled := True;
  end;
end;

The control grid now scrolls, but it does not change the position in the DataSet, but instead moves its content out of the client rect which looks pretty ugly.

How do I enable mouse wheel scrolling on a TDBCtrlGrid?

2

2 Answers

3
votes

As a workaround you can scroll the DataSet instead:

procedure TForm1.FormMouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
var
  I: Integer;
  Grid: TDBCtrlGrid;
  DataSet: TDataSet;
begin
  Grid := DBCtrlGrid1;
  if not Grid.ClientRect.Contains(Grid.ScreenToClient(MousePos)) then
    Exit;
  if not Assigned(Grid.DataSource) then
    Exit;
  DataSet := Grid.DataSource.DataSet;
  if DataSet = nil then
    Exit;
  for I := 0 to Abs(WheelDelta div 256) - 1 do 
  begin
    if WheelDelta > 0 then
      DataSet.Prior
    else
      DataSet.Next;
  end;
  Handled := True;
end;
1
votes

There is an easier way if you also have a hidden DBGrid on your form, hooked to the same datasource. In the click event of the DBCtrlGrid:

DBCtrlGrid.setfocus;

The DBGrid seems to receive the mouse wheel events as long as it is focused. The DBCtrlGrid then scrolls nicely as the record changes.