DBGrid中一个文本字段显示为“图标+字段内容”,如何做到?(100分)

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

kuikui

Unregistered / Unconfirmed
GUEST, unregistred user!
DBGrid中一个文本字段显示为“图标+字段内容”,如何做到?

先谢谢!
 
procedure DrawColumnCell(const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState); override;
自己实现画图标和字段的文字内容
 
procedure TForm1.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var Bitmap:TBitMap;
K1,K2:TRect;
begin
if Column.Index=0 then
begin
with (Sender As TDBGrid).DataSource.DataSet do
if RecordCount>0 then
begin
BitMap:=TBitMap.Create;
ImageList1.GetBitmap(0,BitMap);
K1.TopLeft:=Point(0,0);K1.BottomRight:=Point(10,11);
K2.TopLeft:=Point(Rect.Left+(Rect.Right-Rect.Left-10) Div 2,Rect.Top+(Rect.Bottom-Rect.Top-11)Div 2);
K2.BottomRight:=Point(K2.Left+10,K2.Top+11);
(Sender As TDBGrid).Canvas.Brush.Color:=clWhite;
(Sender As TDBGrid).Canvas.FillRect(Rect);
(Sender As TDBGrid).Canvas.CopyRect(k2,Bitmap.Canvas,K1);
FreeAndNil(BitMap);
end;
end
else (Sender As TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
 
to TYZhang:
我测试拉以下,你显示了图标,可是文字被遮住拉
 
我的意思是DBGrid中一个String字段显示为“图标+字段内容”,如何做到?
 
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var Bitmap:TBitMap;
K1,K2:TRect;
C:TColor;
begin
if Column.Index=0 then
begin
with (Sender As TDBGrid).DataSource.DataSet do
if RecordCount>0 then
begin
BitMap:=TBitMap.Create;
ImageList1.GetBitmap(0,BitMap);
K1.TopLeft:=Point(0,0);K1.BottomRight:=Point(16,16);
K2.TopLeft:=Rect.TopLeft;K2.BottomRight:=Point(Rect.Left+16,Rect.Bottom);
C:=(Sender As TDBGrid).Canvas.Brush.Color;
(Sender As TDBGrid).Canvas.Brush.Color:=clWhite;
(Sender As TDBGrid).Canvas.FillRect(Rect);
(Sender As TDBGrid).Canvas.CopyRect(k2,Bitmap.Canvas,K1);
FreeAndNil(BitMap);
K1:=Rect;
K1.Left:=Rect.Left+16;
end;
(Sender As TDBGrid).Canvas.Brush.Color:=C;
(Sender As TDBGrid).DefaultDrawColumnCell(K1,DataCol,Column,State);
end
else (Sender As TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
 
试过,基本可以拉,谢谢TYZhang!分数全送给你

如果这个字段的文字左对齐就好拉,你现在是右对齐,一般的文字都是左对齐是不是?
可以帮我看看其他的问题吗?
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2638931

 
后退
顶部