把TDBNavigator放在TDBGrid的水平Scrollbar上(把Scrollbar缩短)(100分)

  • 主题发起人 主题发起人 kun
  • 开始时间 开始时间
K

kun

Unregistered / Unconfirmed
GUEST, unregistred user!
把TDBNavigator放在TDBGrid的水平Scrollbar上(把Scrollbar缩短)
组成新控件,同时显示记录总数在Tlable上(如"共1000条记录",当前记录位置
是“第56条“(就如ACCEESS)。如何实现?

 
1. 去掉DBGrid的水平滚动条,把ScrollBars设置为ssNone
2. 创建内置的水平滚动条FScr和FNav, 和其它控件
3. 控制FScr和FNav的位置(和宽度)
我认为至此外观上已搞定

type
TTestGrid = class(TDBGrid)
private
FNav: TDBNavigator;
FScr: TScrollBar;
...
protected
procedure Loaded; override;
procedure Resize; override;
...
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
...
published
...
end;

implementation

constructor TTestGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width := 400;
ScrollBars := ssNone;

FScr := TScrollBar.Create(Self);
FScr.Parent := Self;
FScr.Left := 0;
FScr.Width := 100;
FScr.Top := Height - FScr.Height - 3;

FNav := TDBNavigator.Create(Self);
FNav.Parent := Self;
FNav.Left := 101;
FNav.Height := FScr.Height;
FNav.Top := Height - FNav.Height - 3;
FNav.Width := 200;
...
end;

destructor TTestGrid.Destroy;
begin
...
FNav.Free;
FScr.Free;

inherited Destroy;
end;

procedure TTestGrid.Loaded;
begin
inherited Loaded;

Resize;
end;

procedure TTestGrid.Resize;
begin
inherited;

FScr.Top := Height - FScr.Height - 3;
FNav.Top := Height - FNav.Height - 3;
end;
 
谢谢先让我试一试。
 
但有闪烁,如何重画,如popeye 的imagedbgrid,Tnavigator的属性如何inherited,请指教。
 
> 闪烁:原来的方案可能无法避免闪烁,试一试下面的方案。
> 重画:可重载 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.
 
从新编控件最好
 
Croco:非常感谢您。
LeeChange: 可否具体些?
 
thanks a lot for croco and leechage 的 回答。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
652
import
I
I
回复
0
查看
816
import
I
后退
顶部