Mega Code Archive

 
Categories / Delphi / ADO Database
 

Remove the horizontal andor vertical scrollbar of a TDBGrid

Title: Remove the horizontal and/or vertical scrollbar of a TDBGrid Question: How to get rid of those pesky scrollbars of a TDBGrid when you don't need them ? Answer: This example component shows how to get rid off the horizontal and/or vertical scrollbar of a TDBGrid. The code can ofcourse be used for other types of grid. I just used a TDBGrid for this example. You can control the horizontal and vertical scrollbar seperatly. There is a boolean propery made available for each of the scrollbars. You can ofcourse control these settings in a more effecient way like using the scrollbars set property or a set of your own. The removing of the scrollbars is done by trapping the WM_NCCALCSIZE message. In here you check the style and adjust it to your need. This method will work for the most controls which use the common horizontal scrollbars, but there might be some slight problems with some controls which work in a slightly different way. As an added bonus, the code for this example component also shows how to give each column of the TDBGrid a tag property like most controls have. This is achieved by derriving a class from TColumn and adding a Tag property to it. Then all that remains is to override the CreateColumns function of the TDBGrid and specify that each column that is created is of an object of the newly created column class. Tested with Delphi 6 Professional on Windows 2000 Professional. ------------------------------------------------------------------------------- unit ExtentedDBGrid; interface uses Windows, Messages, SysUtils, Classes, DBGrids, Math, Controls; type TExtendedColumn = class(TColumn) private { Private declarations } FTag: Integer; protected { Protected declarations } published { Published declarations } property Tag: Integer read FTag write FTag; end; TExtendedDBGrid = class(TDBGrid) private { Private declarations } FVerticalBar : Boolean; FHorizontalBar: Boolean; procedure SetVerticalBar(AValue: Boolean); procedure SetHorizontalBar(AValue: Boolean); procedure WMNCCalcSize(var msg: TMessage); message WM_NCCALCSIZE; protected { Protected declarations } function CreateColumns: TDBGridColumns; override; public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Published declarations } property VerticalBar: Boolean read FVerticalBar write SetVerticalBar default True; property HorizontalBar: Boolean read FHorizontalBar write SetHorizontalBar default False; end; procedure Register; implementation uses Grids; procedure Register; begin RegisterComponents('Own Components', [TExtendedDBGrid]); end; { TExtentedDBGrid } constructor TExtendedDBGrid.Create(AOwner: TComponent); begin inherited Create(AOwner); FVerticalBar := True; FHorizontalBar := False; end; function TExtendedDBGrid.CreateColumns: TDBGridColumns; begin Result := TDBGridColumns.Create(Self,TExtendedColumn); end; destructor TExtendedDBGrid.Destroy; begin inherited Destroy; end; procedure TExtendedDBGrid.SetHorizontalBar(AValue: Boolean); begin FHorizontalBar := AValue; RecreateWnd; end; procedure TExtendedDBGrid.SetVerticalBar(AValue: Boolean); begin FVerticalBar := AValue; RecreateWnd; end; procedure TExtendedDBGrid.WMNCCalcSize(var msg: TMessage); var style: Integer; begin style := getWindowLong( handle, GWL_STYLE ); if (style and WS_HSCROLL) 0 then if not FHorizontalBar then SetWindowLong( handle, GWL_STYLE, style and not WS_HSCROLL ); if (style and WS_VSCROLL) 0 then if not FVerticalBar then SetWindowLong( handle, GWL_STYLE, style and not WS_VSCROLL ); inherited; end; end. -------------------------------------------------------------------------------