Z
zjmg
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾:我是个初学者,照着《Delphi高级开发指南》一书中的一个行高度
不同的DBGrid组件的例子,在Delphi5中输入源代码,但却不能编译通过,我的
数据库设置无误,究竟是什么原因?
附:代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids, Db, DBTables;
type
TDhDbgrid = class(TDbgrid)
DataSource1: TDataSource;
Table1: TTable;
DDhDBGrid: TDBGrid;
private
{ Private declarations }
flinesperrow:integer;
procedure setlinesperrow(value:integer);
protected
procedure drawcolumncell(const rect:trect;datacol:integer;column:tcolumn;state:tgriddrawstate);override;
procedure layoutchanged;override;
public
{ Public declarations }
constructor create(aowner:tcomponent);override;
published
property linesperrow:integer
read flinesperrow write setlinesperrow
default 1;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
constructor tddhdbgrid.create(aowner:tcomponent);
begin
inherited create(aowner);
flinesperrow:=1;
end;
procedure tddhdbgrid.setlinesperrow(value:integer);
begin
if value<>flinesperrow then
begin
flinesperrow:=value;
layoutchanged;
end;
end;
procedure tddhgrid.layoutchanged;
var
pixelsperrow,pixelstitle,i:integer;
begin
inherited layoutchanged;
canvas.font:=font;
pixelsperrow:=canvas.textheight('wg')+3;
if dgrowlines in options then
inc(pixelsperrow,gridlinewidth);
canvas.font:=titlefont;
pixelstitle:=canvas.textheight('wg')+4;
if dgrowlines in options then
inc(pixelstitle,gridlinewidth);
rowcount:=1+(height-pixelstitle) div (pixelsperrow*flinesperrow);
defaultrowheight:=pixelsperrow*flinesperrow;
rowheights[0]:=pixelstitle;
for i:=1 to rowcount-1 do
rowheights:=pixelsperrow*flinesperrow;
end;
procedure tddhdbgrid.drawcolumncell(const rect:trect;datacol:integer;column:tcolumn;state:tgriddrawstate);
var
bmp:tbitmap;
outrect:trect;
begin
if flinesperrow=1 then
inherited drawcolumncell(rect,datacol,column,state)
else
begin
canvas.fillrect(rect);
outrect:=rect;
inflaterect(outrect,-2,-2);
if column.Field is tgraphicfield then
begin
bmp:=tbitmap.create;
try
bmp.assign(column.field);
canvas.stretchdraw(outrect,bmp)
finally
bmp.free;
end;
end
else if column.field is tmemofield then
begin
drawtext(canvas.handle,pchar(column.field.asstring),
length(column.field.asstring),
outrect,dt_wordbreak or dt_noprefix)
end
else
drawtext(canvas.handle,pchar(column.field.displaytext),length(column.field.displaytext),
outrect,dt_vcenter or dt_singleline or dt_noprefix);
end;
end;
end;
end.
不同的DBGrid组件的例子,在Delphi5中输入源代码,但却不能编译通过,我的
数据库设置无误,究竟是什么原因?
附:代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids, Db, DBTables;
type
TDhDbgrid = class(TDbgrid)
DataSource1: TDataSource;
Table1: TTable;
DDhDBGrid: TDBGrid;
private
{ Private declarations }
flinesperrow:integer;
procedure setlinesperrow(value:integer);
protected
procedure drawcolumncell(const rect:trect;datacol:integer;column:tcolumn;state:tgriddrawstate);override;
procedure layoutchanged;override;
public
{ Public declarations }
constructor create(aowner:tcomponent);override;
published
property linesperrow:integer
read flinesperrow write setlinesperrow
default 1;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
constructor tddhdbgrid.create(aowner:tcomponent);
begin
inherited create(aowner);
flinesperrow:=1;
end;
procedure tddhdbgrid.setlinesperrow(value:integer);
begin
if value<>flinesperrow then
begin
flinesperrow:=value;
layoutchanged;
end;
end;
procedure tddhgrid.layoutchanged;
var
pixelsperrow,pixelstitle,i:integer;
begin
inherited layoutchanged;
canvas.font:=font;
pixelsperrow:=canvas.textheight('wg')+3;
if dgrowlines in options then
inc(pixelsperrow,gridlinewidth);
canvas.font:=titlefont;
pixelstitle:=canvas.textheight('wg')+4;
if dgrowlines in options then
inc(pixelstitle,gridlinewidth);
rowcount:=1+(height-pixelstitle) div (pixelsperrow*flinesperrow);
defaultrowheight:=pixelsperrow*flinesperrow;
rowheights[0]:=pixelstitle;
for i:=1 to rowcount-1 do
rowheights:=pixelsperrow*flinesperrow;
end;
procedure tddhdbgrid.drawcolumncell(const rect:trect;datacol:integer;column:tcolumn;state:tgriddrawstate);
var
bmp:tbitmap;
outrect:trect;
begin
if flinesperrow=1 then
inherited drawcolumncell(rect,datacol,column,state)
else
begin
canvas.fillrect(rect);
outrect:=rect;
inflaterect(outrect,-2,-2);
if column.Field is tgraphicfield then
begin
bmp:=tbitmap.create;
try
bmp.assign(column.field);
canvas.stretchdraw(outrect,bmp)
finally
bmp.free;
end;
end
else if column.field is tmemofield then
begin
drawtext(canvas.handle,pchar(column.field.asstring),
length(column.field.asstring),
outrect,dt_wordbreak or dt_noprefix)
end
else
drawtext(canvas.handle,pchar(column.field.displaytext),length(column.field.displaytext),
outrect,dt_vcenter or dt_singleline or dt_noprefix);
end;
end;
end;
end.