精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>数据库技术>>关于TDBGrid的使用>>关于TDbgrid的颜色的问题(1)

主题:关于TDbgrid的颜色的问题(1)
发信人: nosound()
整理人: huxley(2001-12-17 20:13:49), 站内信件
    刚才没看清,以为你是要将不同的列用不同的颜色显示出来,现在仔细一
看,原来是要显示不同的行,这要困难一些,我暂时想到下面这个办法来凑数,

只是表明一种可行的方法,也许还有更好的,等我想到了再来一张贴子吧:

    从TDBGrid继承一个新的构件(比如就叫TMyDBGrid吧),重载如下的两个
方法即可实现你的要求。不过下列代码只是一个示例,还没有优化,因此效果
嘛……有点晃眼睛^-^,如果要做得更好,只需完全重写DrawCell方法就行了,

现在用的是偷懒的笨办法,即先让TDBGrid绘出Cell来,然后再用兰绿色来涂
底子……,要重写DrawCell也不难,不过大段大段的代码还是留给老兄你自己
来吧,嘿嘿

type
  TMyDBGrid = class(TDBGrid)
  protected
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGri
dDrawState); override;
    procedure Scroll(Distance: Integer); override;
  end;

implementation

procedure TMyDBGrid.DrawCell(ACol, ARow: Integer; ARect: TRect; AState
: TGridDrawState);
var
  bmp: TBitmap;
begin
  inherited DrawCell(ACol, ARow, ARect, AState);
  if ((ARow mod 2)<>0) and (ACol<>0) then
  begin
    bmp := TBitmap.Create;
    try
      bmp.Width := ARect.Right - ARect.Left;
      bmp.Height:= ARect.Bottom- ARect.Top;
      BitBlt(bmp.Canvas.Handle, 0, 0, bmp.Width, bmp.Height,
        Canvas.Handle, ARect.Left, ARect.Top, SRCCOPY);
      bmp.TransparentMode := tmAuto;
      bmp.Transparent := True;
      Canvas.Brush.Color := clTeal;
      Canvas.FillRect(ARect);
      Canvas.Draw(ARect.Left, ARect.Top, bmp);
    finally
      bmp.Free;
    end;
  end;
end;

procedure TMyDBGrid.Scroll(Distance: Integer);
begin
  inherited Scroll(Distance);
  Repaint;
end;

--
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 61.139.14.221]

[关闭][返回]