3

好吧,我对 DBGrid 垂直滚动有疑问。当我用鼠标滚轮或垂直滚动​​条垂直滚动它时,它会上下移动选定的行。我想让它滚动的不是选定的行,而是整个网格。就像它在 Microsoft Excel 中工作一样(只是为了让你知道我的意思)。有什么建议么?

4

2 回答 2

1

嗯,几乎是我想看到的。在 swissdelhicenter.ch 上找到了 hanuleye 的帖子。此代码让您可以使用鼠标滚轮自由滚动 DBGrid。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, DBTables;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Table1: TTable;
    DBGrid1: TDBGrid;
    procedure FormCreate(Sender: TObject);
    procedure DBGridMouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TWheelDBGrid = class(TDBGrid)
  public
    property OnMouseWheel;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TWheelDBGrid(DBGrid1).OnMouseWheel := DBGridMouseWheel;
end;

function GetNumScrollLines: Integer;
begin
  SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, @Result, 0);
end;

procedure TForm1.DBGridMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  Direction: Shortint;
begin
  Direction := 1;
  if WheelDelta = 0 then
    Exit
  else if WheelDelta > 0 then
    Direction := -1;

  with TDBGrid(Sender) do
  begin
    if Assigned(DataSource) and Assigned(DataSource.DataSet) then
      DataSource.DataSet.MoveBy(Direction * GetNumScrollLines);
    Invalidate;
  end;
end;

end.
于 2009-08-10T07:54:38.273 回答
0

我认为这是不可能的,因为在我看来,DBGrids 上的滚动条更像是进度指示器而不是滚动条。它的行为与滚动“页面”的 ListViews 中的滚动不同,在 db 控件中,即使您向上或向下移动单行,滚动条也会更改以反映“当前行”/“总行”分数

于 2009-08-09T11:57:28.410 回答