The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Delphi …hide the scrollbars of a DBGrid?

Posted by jpluimers on 2021/05/04

Recently I needed a plain TDBGrid without a horizontal scrollbar. I based it on the below solutions, but using an interposer class (type TDBGrid = class(TDBGrid) ... end;).

Another solution is to redirect the WinProc for a single grid component to a different method (you can apply it to the similar TDBCtrlGrid class as well):

unit DBGridFormUnit;
uses
  Winapi.Messages,
  Vcl.DBGrids,
  Vcl.Forms;

type
  TDBGridForm = class(TForm)
    DBGrid: TDBGrid;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  strict private
    OriginalDBGridWindowProc : TWndMethod;
    procedure DBGridWindowProc(var Message: TMessage);
  end;

implementation

uses
  Winapi.Windows;
procedure TDBGridForm.FormCreate(Sender: TObject);
begin
  OriginalDBGridWindowProc := DBGrid.WindowProc;
  DBGrid.WindowProc := DBGridWindowProc;
end;

procedure TDBGridForm.FormDestroy(Sender: TObject);
begin
  DBGrid.WindowProc := OriginalDBGridWindowProc;
end;

procedure TDBGridForm.DBGridWindowProc(var Message: TMessage);
const 
  ScrollStylesToExclude = WS_VSCROLL or WS_HSCROLL;
var 
  Style: Integer;
begin
  if Message.Msg = WM_NCCALCSIZE then
  begin
    Style := GetWindowLong(Handle, GWL_STYLE);
    if (Style and ScrollStylesToExclude) <> 0 then // any scroll style to exclude present?
      SetWindowLong(Handle, GWL_STYLE, Style and not ScrollStylesToExclude);
  end
  else
    OrigWndProc(Message);
end;
end.

I think the SwissDelphiCenter solution in [WayBack] …hide the scrollbars of a DBGrid? originally has been copied from the 2003 post by Peter Below at [WayBack] how to hide DBCtrlGrid scrollbars – delphi.

(*
Q:
I want to hide the vertical scrollbar on a dbgrid when the record count
exceed a number. How can I do that?

A:
Make a descendent of the TDBGrid class. Add a handler for the
WM_NCCALCSIZE message.

Title: ...hide the scrollbars of a DBGrid?

Author: P. Below 

Category: VCL
*)
type
  TNoScrollDBGrid = class(TDBGrid)
  private
    procedure WMNCCalcSize(var Msg: TMessage);
    message WM_NCCALCSIZE;
  end;

procedure TNoScrollDBGrid.WMNCCalcSize(var Msg: TMessage);
const
  Scrollstyles = WS_VSCROLL or WS_HSCROLL;
var
  Style: Integer;
begin
  Style := GetWindowLong(Handle, GWL_STYLE);
  if (Style and Scrollstyles) <> 0 then
    SetWindowLong(Handle, GWL_STYLE, Style and not Scrollstyles);
  inherited;
end;

//This removes both scrollbars. If you want to remove only the vertical one
//change the scrollstyles constant accordingly.

I like this derived class from [WayBack] Delphi VCL Component • View topic • Hiding Scrollbars in DBGRID by Chris Luck based on the code from Peter Below more (I slightly condensed it from non-relevant code):

unit NoScrollDBGrid;

interface

uses
  Windows, Messages, DBGrids;

type
  TNoScrollDBGrid = class(TDBGrid)
  private
    FVertScroll: Boolean;
    FHorzScroll: Boolean;
    procedure WMNCCalcSize(var msg: TMessage); message WM_NCCALCSIZE;
    procedure SetVertScroll(Value: Boolean);
    procedure SetHorzScroll(Value: Boolean);
  published
    property VertScroll: Boolean read FVertScroll write SetVertScroll;
    property HorzScroll: Boolean read FHorzScroll write SetHorzScroll;
  end;

procedure Register;

implementation

uses
  Classes;
procedure TNoScrollDBGrid.SetVertScroll(Value: Boolean);
begin
  if FVertScroll <> Value then
  begin
    FVertScroll := Value;
    RecreateWnd;
  end;
end;

procedure TNoScrollDBGrid.SetHorzScroll(Value: Boolean);
begin
  if FHorzScroll <> Value then
  begin
    FHorzScroll := Value;
    RecreateWnd;
  end;
end;

procedure TNoScrollDBGrid.WMNCCalcSize(var msg: TMessage);
var
  style: Integer;
begin
  style := getWindowLong( handle, GWL_STYLE );

  if (NOT(FHorzScroll)) AND ((style and WS_HSCROLL) <> 0) then
    SetWindowLong( handle, GWL_STYLE, style and not WS_HSCROLL );

  if (NOT(FVertScroll)) AND ((style and WS_VSCROLL) <> 0) then
    SetWindowLong( handle, GWL_STYLE, style and not WS_VSCROLL );

  inherited;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TNoScrollDBGrid]);
end;

end.

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.