简体   繁体   English

如何在Delphi中捕获DBGrid中的滚动事件

[英]How to catch scrolling event in DBGrid in Delphi

I have a DBGrid, I need to run some code, each time the horizontal scrollbar is used. 我有一个DBGrid,每次使用水平滚动条时,我需要运行一些代码。 I couldn't find such event in DBGrid. 我在DBGrid中找不到此类事件。 Can you advise something? 你能建议些什么吗?

Maybe this will help. 也许这会有所帮助。 It shows an example for trapping the scrolling events of a regular TStringGrid. 它显示了一个捕获常规TStringGrid的滚动事件的示例。 Synchronize the Scrolling of two TStringgrids? 同步两个TStringgrids的滚动?

There is a WMHScroll procedure in TCustomGrid, but it is private. TCustomGrid中有WMHScroll过程,但它是私有的。 You can't use it in a DBGrid. 您不能在DBGrid中使用它。
You would have to create your own TDBGrid descendant and do your own 您将必须创建自己的TDBGrid后代并执行自己的操作

procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;

or do some seriously bad hacking... 或进行一些严重的恶意入侵...

Update : trick/hack to sneak your code in... 更新 :欺骗/黑客将您的代码潜入...

[...]
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, ADODB, Grids, DBGrids;

    type
      // Hack to redeclare your TDBGrid here whitout the the form designer going mad
      TDBGrid = class(DBGrids.TDBGrid)
        procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
      end;

      TForm8 = class(TForm)
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        ADODataSet1: TADODataSet;
        ADOConnection1: TADOConnection;
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form8: TForm8;

    implementation

    {$R *.dfm}

    { TDBGrid }

    procedure TDBGrid.WMHScroll(var Msg: TWMHScroll);
    begin
      case Msg.ScrollCode of
        SB_ENDSCROLL: OutputDebugString('SB_ENDSCROLL') ;
        SB_LEFT:OutputDebugString('SB_LEFT');
        SB_RIGHT:OutputDebugString('SB_RIGHT');
        SB_LINELEFT:OutputDebugString('SB_LINELEFT');
        SB_LINERIGHT:OutputDebugString('SB_LINERIGHT');
        SB_PAGELEFT:OutputDebugString('SB_PAGELEFT');
        SB_PAGERIGHT:OutputDebugString('SB_PAGERIGHT');
        SB_THUMBPOSITION:OutputDebugString('SB_THUMBPOSITION');
      end;
      inherited; // to keep the expected behavior
    end;
[...]

Update2 : Note that you can move your special TDBGrid code to a separate unit (recommended), just be sure to put this unit name AFTER DBGrids in your Form's uses clause . Update2 :请注意,您可以将特殊的TDBGrid代码移动到单独的单元(推荐),只需确保在Form的uses子句中将此单元名称AFTER DBGrids放入即可

I can't check this at the moment, but if I recall correctly the event's there, but not published. 我目前无法检查,但如果我没记错的话,该活动在那里,但尚未发布。 Try creating a control that descends from TDBGrid and publishes the scroll bar event. 尝试创建一个从TDBGrid继承并发布滚动条事件的控件。

EDIT: Wrong answer, obviously. 编辑:显然,答案错误。 It catches the vertical scrollbar, but not the horizontal one. 它捕获垂直滚动条,但不捕获水平滚动条。

You don't catch it at the DBGrid level. 您不会在DBGrid级别上找到它。 You catch it at the BeforeScroll or AfterScroll of the attached TDataSet. 您可以在附加的TDataSet的BeforeScroll或AfterScroll上捕获它。 It fires with either the scrollbar, up and down arrow keys, page up and page down keys, etc. that occurs within the DBGrid. 它使用DBGrid中发生的滚动条,向上和向下箭头键,向上和向下键等触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM