> 闪烁:原来的方案可能无法避免闪烁,试一试下面的方案。
> 重画:可重载 DrawCell;popeye 的 imagedbgrid 没找到。
> 属性继承:下面给出了 Flat 和 OnClick 的例子。
unit TestGrid;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DB, DBGrids, DBCtrls, StdCtrls, ExtCtrls;
type
TInpDBGrid = class(TDBGrid)
private
FOnTopLeftChanged: TNotifyEvent;
procedure WMEraseBkgnd(var Message: TMessage); message WM_ERASEBKGND;
protected
procedure TopLeftChanged; override;
public
constructor Create(AOwner: TComponent); override;
property OnTopLeftChanged: TNotifyEvent read FOnTopLeftChanged write FOnTopLeftChanged;
property LeftCol;
property VisibleRowCount;
end;
TTestGrid = class(TCustomPanel)
private
FGrid: TInpDBGrid;
FNav: TDBNavigator;
FScr: TScrollBar;
FOnNavClick: ENavClick;
function GetDataSource: TDataSource;
procedure SetDataSource(Value: TDataSource);
function GetNavFlat: Boolean;
procedure SetNavFlat(Value: Boolean);
protected
procedure DoNavClick(Sender: TObject; Button: TNavigateBtn); virtual;
procedure DoScrClick(Sender: TObject);
procedure DoTopLeftChanged(Sender: TObject);
procedure Loaded; override;
procedure Paint; override;
procedure Resize; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Grid: TInpDBGrid read FGrid;
property Nav: TDBNavigator read FNav;
property Scr: TScrollBar read FScr;
published
property Align;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property NavFlat: Boolean read GetNavFlat write SetNavFlat;
property OnNavClick: ENavClick read FOnNavClick write FOnNavClick;
end;
procedure Register;
implementation
constructor TInpDBGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ScrollBars := ssNone;
end;
procedure TInpDBGrid.WMEraseBkgnd(var Message: TMessage);
begin
Message.Result := 0;
end;
procedure TInpDBGrid.TopLeftChanged;
begin
inherited TopLeftChanged;
if Assigned(FOnTopLeftChanged) then FOnTopLeftChanged(Self);
end;
constructor TTestGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width := 400;
Height := 200;
BevelOuter := bvNone;
FullRepaint := False;
FGrid := TInpDBGrid.Create(Self);
FGrid.Parent := Self;
FGrid.Left := 0;
FGrid.Top := 0;
FGrid.Align := alTop;
FGrid.OnTopLeftChanged := DoTopLeftChanged;
FScr := TScrollBar.Create(Self);
FScr.Parent := Self;
FScr.Height := GetSystemMetrics(SM_CYHSCROLL);
FScr.TabStop := False;
FScr.OnChange := DoScrClick;
FScr.SetParams(1, 1, 10);
FNav := TDBNavigator.Create(Self);
FNav.Parent := Self;
FNav.Height := FScr.Height;
FNav.Width := 200;
FNav.OnClick := DoNavClick;
end;
destructor TTestGrid.Destroy;
begin
FNav.Free;
FScr.Free;
FGrid.Free;
inherited Destroy;
end;
procedure TTestGrid.Loaded;
begin
inherited Loaded;
Resize;
end;
procedure TTestGrid.Paint;
begin
inherited Paint;
FScr.Max := FGrid.ColCount - 1;
end;
procedure TTestGrid.Resize;
begin
inherited;
FGrid.Height := Height - FScr.Height + 2;
FNav.Left := 0;
FNav.Top := Height - FNav.Height;
FScr.Left := FNav.Width + 100;
FScr.Top := Height - FScr.Height;
FScr.Width := ClientWidth - FNav.Width - GetSystemMetrics(SM_CYHSCROLL) - 102;
end;
function TTestGrid.GetDataSource: TDataSource;
begin
Result := FGrid.DataSource;
end;
procedure TTestGrid.SetDataSource(Value: TDataSource);
begin
FGrid.DataSource := Value;
FNav.DataSource := Value;
end;
procedure TTestGrid.DoNavClick(Sender: TObject; Button: TNavigateBtn);
begin
if Assigned(FOnNavClick) then FOnNavClick(Sender, Button);
end;
function TTestGrid.GetNavFlat: Boolean;
begin
Result := FNav.Flat;
end;
procedure TTestGrid.SetNavFlat(Value: Boolean);
begin
FNav.Flat := Value;
end;
procedure TTestGrid.DoTopLeftChanged(Sender: TObject);
begin
FScr.Position := FGrid.LeftCol;
end;
procedure TTestGrid.DoScrClick(Sender: TObject);
begin
FGrid.LeftCol := Scr.Position;
end;
procedure Register;
begin
RegisterComponents('Data Controls', [TTestGrid]);
end;
end.