将滚动条隐藏在 Delphi dbgrid 中(即使在调整大小时)

发布于 2024-12-06 07:17:55 字数 375 浏览 8 评论 0原文

对于我们的 dbgrid,我们希望滚动条始终隐藏。 由于 TDBGrid 没有“滚动条”属性,因此我们使用:

ShowScrollBar(DBGrid1.Handle, SB_VERT, False);
ShowScrollBar(DBGrid1.Handle, SB_HORZ, False);

但是,当我们调整窗口(以及包含 dbgrid 的面板)的大小时,对于 一秒钟滚动条出现,只有在调用后才再次隐藏 以上两种方法。

解决方案是在DrawColumnCell中调用这些方法,但这会导致闪烁 dbgrid 的值,即使 DoubleBuffered 设置为 true。

有什么办法可以永久隐藏滚动条吗?

提前致谢!

For our dbgrid we want the scrollbars to be constantly hidden.
Since TDBGrid doesn't have a 'scrollbars' property, we use:

ShowScrollBar(DBGrid1.Handle, SB_VERT, False);
ShowScrollBar(DBGrid1.Handle, SB_HORZ, False);

However when we resize the window (and the panel containing the dbgrid), for
a second the scrollbars appear and becom hidden again only after recalling
the two above methods.

A solution is to call these methods in DrawColumnCell, but this causes flickering
of the dbgrid, even with DoubleBuffered set to true.

Is there any way to hide the scrollbars permanently?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最后的乘客 2024-12-13 07:17:55

隐藏 TDBGrid 中的滚动条://docwiki.embarcadero.com/Libraries/en/Vcl.Grids.TCustomGrid.CreateParams">CreateParams 具有非常短的时间效果。有一个过程 UpdateScrollBar 导致滚动条可见。发生这种情况是因为滚动条的可见性是根据显示的数据进行控制的,因此每当数据更改时都会调用此过程。

由于每当需要更新滚动条时都会调用此过程,并且由于它是虚拟的,因此是时候覆盖它了。
以下代码示例使用插入类,因此所有 TDBGrid 组件在属于该单位的表格上将表现相同:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TDBGrid = class(DBGrids.TDBGrid)
  private
    procedure UpdateScrollBar; override;
  end;

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TDBGrid.UpdateScrollBar;
begin
  // in this procedure the scroll bar is being shown or hidden
  // depending on data fetched; and since we never want to see 
  // it, do just nothing at all here
end;

end.

Hiding the scrollbar of the TDBGrid in CreateParams has a very short time effect. There's the procedure UpdateScrollBar which causes the scrollbar to be visible. It happens because the scroll bar visibility is controlled depending on the data displayed, thus this procedure is called whenever the data is changed.

And since this procedure is called whenever the scrollbar needs to be updated and because it's virtual, it's time to override it.
The following code sample uses the interposed class, so all TDBGrid components on the form which belongs to this unit will behave the same:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TDBGrid = class(DBGrids.TDBGrid)
  private
    procedure UpdateScrollBar; override;
  end;

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TDBGrid.UpdateScrollBar;
begin
  // in this procedure the scroll bar is being shown or hidden
  // depending on data fetched; and since we never want to see 
  // it, do just nothing at all here
end;

end.
最冷一天 2024-12-13 07:17:55

滚动条在 TDBGrid.UpdateScrollBar 中更新。不幸的是这个例程不是虚拟的(至少在 D7 中)。在该例程中,调用 SetScrollInfo,这是一个不发送任何可能被拦截的消息的 Windows 函数。那里没有运气。

剩下的唯一可能是重写每当控件更改大小时发送的消息的消息处理程序:

type
  TDBGrid = class(DBGrids.TDBGrid)
  private
    procedure WMWindowPosChanged(var Message: TWMWindowPosChanged);
      message WM_WINDOWPOSCHANGED;
  end;

procedure TDBGrid.WMWindowPosChanged(var Message: TWMWindowPosChanged);
begin
  inherited;
  Windows.ShowScrollBar(Handle, SB_VERT, False);
end;

尽管当数据更改或数据集处于活动状态时也会调用 UpdateScrollBar > 属性更改,这似乎在这里工作而不会闪烁。

The scroll bar is updated in TDBGrid.UpdateScrollBar. Unfortunately this routine is not virtual (in D7 at least). Within that routine, SetScrollInfo is called, a Windows function that doesn't send any message that could be intercept. No luck there.

The only possibility left is to override the message handler for the message that is send whenever the control changed size:

type
  TDBGrid = class(DBGrids.TDBGrid)
  private
    procedure WMWindowPosChanged(var Message: TWMWindowPosChanged);
      message WM_WINDOWPOSCHANGED;
  end;

procedure TDBGrid.WMWindowPosChanged(var Message: TWMWindowPosChanged);
begin
  inherited;
  Windows.ShowScrollBar(Handle, SB_VERT, False);
end;

Although UpdateScrollBar is also called when the data is changed or when the dataset's Active property changes, this seems to work here without flickering.

话少心凉 2024-12-13 07:17:55

也许重写CreateParams()方法并删除Params.Style形式的WS_HSCROLLWS_VSCROLL位会有所不同。如果您不想编写自定义后代,您可以尝试使用 类助手 来完成此操作。

您还可以将 SetWindowLongPtr API 与GWL_STYLE 更改窗口的样式,但当由于某种原因重新创建网格窗口时,更改会丢失(因此它不如覆盖可靠) CreateParams)。

Perhaps overriding CreateParams() method and removing WS_HSCROLL and WS_VSCROLL bits form Params.Style makes difference. You could try to do it with class helper if you don't want to write custom descendant.

You could also use SetWindowLongPtr API with GWL_STYLE to change window's style but then the changes are lost when grid's window is recreated for some reason (so it's not as reliable than overriding CreateParams).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文