Delphi自带的DBNavigator包括许多第三方控件的DBNavigator(像Raize,DevExpress)都没有Caption属性,有时用户说我不知道这个按钮是什么意思,能不能加上个文字说明啊?
当然,我们可以设置每个Button的Hint来给客户提示,但这样客户还是无法直接看到。
我们可以创建一个新的控件来实现给DBNavigator增加Caption属性,但到了D2007之后,增加了class helper for 定义,所以我们可以以一种更巧妙的方式来实现,并且无需创建一个新的控件。
而在我们需要的地方,像平时一样使用普通的DBNavigator(也可以简单地改造现有工程),uses一下这个单元(附后),然后只需要一句代码就可以为我们的DBNavigator的每个按钮增加Caption:
DBNavigator2.AddCaption;
或者你干脆使用连DBNavagator的Create也利用这个方法来改造一下,上面的代码也可以省略了!
好吧,让我们直接来看实现代码:
代码
unit DBNavPlusHelper;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Buttons;
var
DoCaptions: boolean;
FLayout: TButtonLayout;
FSpacing: Integer;
FMargin: Integer;
type
TDBNavPlusHelper = class helper for TDBNavigator
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
procedure AddCaption;
procedure SetLayout(value: TButtonLayout);
procedure SetSpacing(value: Integer);
procedure SetMargin(value: Integer);
procedure SetCaptions;
end;
implementation
const
DefaultCaption: array [0 .. 9] of string = ('首条', '上条', '下条', '末条', '插入',
'删除', '编辑', '保存', '撤消', '刷新');
{ ****************************************************************************** }
procedure TDBNavPlusHelper.AddCaption;
var
x: Integer;
begin
{ initialize values }
DoCaptions := True;
FSpacing := 0;
SetSpacing(2);
FMargin := 0;
SetMargin(-1);
FLayout := blGlyphBottom;
SetLayout(blGlyphTop);
SetCaptions;
end; { of Create }
{ ****************************************************************************** }
procedure TDBNavPlusHelper.SetCaptions;
var
t: TNavigateBtn;
begin
{ set captions or default if not assigned }
for t := low(TNavigateBtn) to high(TNavigateBtn) do
Buttons[t].caption := DefaultCaption[ord(t)];
Invalidate;
end; { of setCaptions }
{ ****************************************************************************** }
procedure TDBNavPlusHelper.SetLayout(value: TButtonLayout);
var
t: TNavigateBtn;
begin
if (value = FLayout) and not(csLoading in ComponentState) then
exit;
FLayout := value;
for t := low(TNavigateBtn) to high(TNavigateBtn) do
Buttons[t].Layout := value;
Invalidate;
end; { of SetLayout }
{ ****************************************************************************** }
procedure TDBNavPlusHelper.SetSpacing(value: Integer);
var
t: TNavigateBtn;
begin
if (value = FSpacing) and not(csLoading in ComponentState) then
exit;
FSpacing := value;
for t := low(TNavigateBtn) to high(TNavigateBtn) do
Buttons[t].Spacing := value;
Invalidate;
end; { of SetSpacing }
{ ****************************************************************************** }
procedure TDBNavPlusHelper.SetMargin(value: Integer);
var
t: TNavigateBtn;
begin
if (value = FMargin) and not(csLoading in ComponentState) then
exit;
if (csLoading in ComponentState) then
SetCaptions;
FMargin := value;
for t := low(TNavigateBtn) to high(TNavigateBtn) do
Buttons[t].margin := value;
Invalidate;
end; { of SetMargin }
end.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Buttons;
var
DoCaptions: boolean;
FLayout: TButtonLayout;
FSpacing: Integer;
FMargin: Integer;
type
TDBNavPlusHelper = class helper for TDBNavigator
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
procedure AddCaption;
procedure SetLayout(value: TButtonLayout);
procedure SetSpacing(value: Integer);
procedure SetMargin(value: Integer);
procedure SetCaptions;
end;
implementation
const
DefaultCaption: array [0 .. 9] of string = ('首条', '上条', '下条', '末条', '插入',
'删除', '编辑', '保存', '撤消', '刷新');
{ ****************************************************************************** }
procedure TDBNavPlusHelper.AddCaption;
var
x: Integer;
begin
{ initialize values }
DoCaptions := True;
FSpacing := 0;
SetSpacing(2);
FMargin := 0;
SetMargin(-1);
FLayout := blGlyphBottom;
SetLayout(blGlyphTop);
SetCaptions;
end; { of Create }
{ ****************************************************************************** }
procedure TDBNavPlusHelper.SetCaptions;
var
t: TNavigateBtn;
begin
{ set captions or default if not assigned }
for t := low(TNavigateBtn) to high(TNavigateBtn) do
Buttons[t].caption := DefaultCaption[ord(t)];
Invalidate;
end; { of setCaptions }
{ ****************************************************************************** }
procedure TDBNavPlusHelper.SetLayout(value: TButtonLayout);
var
t: TNavigateBtn;
begin
if (value = FLayout) and not(csLoading in ComponentState) then
exit;
FLayout := value;
for t := low(TNavigateBtn) to high(TNavigateBtn) do
Buttons[t].Layout := value;
Invalidate;
end; { of SetLayout }
{ ****************************************************************************** }
procedure TDBNavPlusHelper.SetSpacing(value: Integer);
var
t: TNavigateBtn;
begin
if (value = FSpacing) and not(csLoading in ComponentState) then
exit;
FSpacing := value;
for t := low(TNavigateBtn) to high(TNavigateBtn) do
Buttons[t].Spacing := value;
Invalidate;
end; { of SetSpacing }
{ ****************************************************************************** }
procedure TDBNavPlusHelper.SetMargin(value: Integer);
var
t: TNavigateBtn;
begin
if (value = FMargin) and not(csLoading in ComponentState) then
exit;
if (csLoading in ComponentState) then
SetCaptions;
FMargin := value;
for t := low(TNavigateBtn) to high(TNavigateBtn) do
Buttons[t].margin := value;
Invalidate;
end; { of SetMargin }
end.
当然,我们还可以设置字体和图片的位置:
DBNavigator2.SetLayout(blGlyphLeft);
也是一句代码。
下面是效果:
借用这种方法,我们可以方便地扩展现有类或组件的功能而不用创建新的控件!
(注:编译环境:Delphi2010)