DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: covid_007
今日帖子: 6
在线用户: 9
导航: 论坛 -> 数据库专区 斑竹:liumazi,waterstone  
作者:
男 dingning (丁宁) ★☆☆☆☆ -
盒子活跃会员
2004/2/1 9:55:18
标题:
急求代码:dbgrid要实现单双行不同颜色效果 浏览:2623
加入我的收藏
楼主: 好像是在dbgrid事件DBGrid1DrawColumnCell中
----------------------------------------------
-愿天下有情人终成眷属!
http://www.pro-thinking.com/bbs/
作者:
男 fan (孤独求爱) ★☆☆☆☆ -
盒子活跃会员
2004/2/1 10:12:37
1楼: 搜索一下,肯定有
----------------------------------------------
我不懂就发帖子!大家帮我哦!
作者:
女 lovelypp (haha) ★☆☆☆☆ -
盒子活跃会员
2004/2/1 11:45:51
2楼: ehlib
或者在DBGridDrawColumnCell
----------------------------------------------
-
作者:
男 dingning (丁宁) ★☆☆☆☆ -
盒子活跃会员
2004/2/1 12:19:32
3楼: 讲得详细点好吗,大哥啊
----------------------------------------------
-愿天下有情人终成眷属!
http://www.pro-thinking.com/bbs/
作者:
男 dingning (丁宁) ★☆☆☆☆ -
盒子活跃会员
2004/2/1 16:47:51
4楼: 有一个现成的控件,但是我不知道怎样使用,请教高手啊
此帖子包含附件:dingning_200421164751.rar 大小:27.6K
----------------------------------------------
-愿天下有情人终成眷属!
http://www.pro-thinking.com/bbs/
作者:
男 zbdzjx (zbdzjx) ★☆☆☆☆ -
盒子活跃会员
2004/2/2 11:45:59
5楼: 在DBGrid1DrawDataCell事件中加:
if field.DataSet.RecNo mod 2 =1 then  //分奇偶行
  DBgrid1.canvas.brush.color := clblue
else
  DBgrid1.canvas.brush.color := clred;
  DBgrid1.Canvas.FillRect(Rect);
  DBgrid1.Canvas.TextOut(Rect.left+4,Rect.top+4,field.Asstring);
此帖子包含附件:
GIF 图像
大小:8.8K
----------------------------------------------
-也许我懂的很多,也许我懂的很少,但为什么没有公司要我?我很奇怪!
作者:
男 iamdream (银河恒久远,梦想无止境!) ★☆☆☆☆ -
大贡献会员
2004/2/2 12:38:25
6楼: RecNo并不是在所有数据库中都起作用的;
最好是重写一个控件,可以从TDBGrid继承,并利用DataLink.ActiveRecord来取得当前活动行号(注意:DataLink是protected的),即可在DrawDataCell或DrawColumnCell中实现奇偶行不同颜色的效果了。
具体实现请参看我写一个简单控件(尚有很多待完善的地方)。
----------------------------------------------
-广袤璀璨的银河,永无止境的梦想(梦无止境游银河) 博客挂了……
作者:
男 iamdream (银河恒久远,梦想无止境!) ★☆☆☆☆ -
大贡献会员
2004/2/2 12:40:19
7楼: 不好意思,重发一下控件!
----------------------------------------------
-广袤璀璨的银河,永无止境的梦想(梦无止境游银河) 博客挂了……
作者:
男 iamdream (银河恒久远,梦想无止境!) ★☆☆☆☆ -
大贡献会员
2004/2/2 12:41:31
8楼: 怎么搞的,附件怎么传不上?只好将代码直接贴在下面:


unit DBGridEx;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Grids, DBGrids, DB, Graphics;

type

  TDBGridEx = class(TDBGrid)
  private
    FBeautyDraw: boolean;
    FRowOddColor: TColor;
    FRowEvenColor: TColor;
    FOnDrawDataCellEx: TLLDrawDataCellExEvent;
    procedure SetRowOddColor(NewColor: TColor);
    procedure SetRowEvenColor(NewColor: TColor);
    procedure SetBeautyDraw(bAutoDraw: boolean);
    procedure WMVScroll(var Msg: TWMVScroll); message WM_VSCROLL;
    procedure WMMouseWheel(var Message: TWMMouseWheel); message WM_MOUSEWHEEL;
  protected
    { Protected declarations }
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure DrawDataCell(const Rect: TRect; Field: TField;
      State: TGridDrawState); override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    function GetActiveRecord: integer;
  published
    { Published declarations }
    property BeautyDraw: boolean read FBeautyDraw write SetBeautyDraw default true;
    property RowOddColor: TColor read FRowOddColor write SetRowOddColor default TColor($0FBFBF4);
    property RowEvenColor: TColor read FRowEvenColor write SetRowEvenColor default TColor($0FEFDED);
  end;


procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TDBGridEx]);
end;

constructor TDBGridEx.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FBeautyDraw   := true;
  FRowOddColor  := TColor($0FBFBF4);
  FRowEvenColor := TColor($0FEFDED);
end;

procedure TDBGridEx.WMVScroll(var Msg: TWMVScroll);
begin
  inherited ;
  Invalidate;   
end; 

procedure TDBGridEx.WMMouseWheel(var Message: TWMMouseWheel);
var
  iWheelDis: Integer;
begin
  if (Self.DataSource <> nil) and (Self.DataSource.DataSet <> nil) and
        (Self.DataSource.DataSet.Active) then
  begin
    iWheelDis := (- Message.WheelDelta) div WHEEL_DELTA;
    Self.DataSource.DataSet.MoveBy(iWheelDis);
    Self.Refresh;
    //inherited;
  end;
end;

procedure TDBGridEx.KeyDown(var Key: Word; Shift: TShiftState);
var                 //用于处理上下光标键移动时部分单元格刷新不正常的问题 2003.10.21
  index: integer;
begin
  inherited KeyDown(Key, Shift);
  if not (ssCtrl in Shift) and ((Key = VK_UP) or (Key = VK_DOWN))
       and DataLink.Active then
  begin
    index := DataLink.ActiveRecord + 1;
    if (index = 1) or (index = DataLink.RecordCount) then
      Invalidate;
  end;    
end;

procedure TDBGridEx.SetRowOddColor(NewColor: TColor);
begin
  FRowOddColor := NewColor;
  if FBeautyDraw then
    Invalidate;
end;

procedure TDBGridEx.SetRowEvenColor(NewColor: TColor);
begin
  FRowEvenColor := NewColor;
  if FBeautyDraw then
    Invalidate;
end;

procedure TDBGridEx.SetBeautyDraw(bAutoDraw: boolean);
begin
  FBeautyDraw := bAutoDraw;
  Invalidate;
end;

procedure TDBGridEx.DrawDataCell(const Rect: TRect; Field: TField;
      State: TGridDrawState);
var
  Index: integer;
begin
  inherited DrawDataCell(Rect, Field, State);
  if FBeautyDraw then
  begin
    if (GetActiveRecord mod 2)<> 0 then
    begin
      Canvas.Brush.Color := FRowOddColor;
    end
    else
    begin        //设置偶数行颜色
      Canvas.Brush.Color := FRowEvenColor; //clSkyBlue;
    end;
    if State <> [] then
    begin
      if Self.Focused and ((gdFocused in State) or (gdSelected in State)) or
            (dgAlwaysShowSelection in Self.Options) then
        Canvas.Brush.Color := TColor(not(Longint(Canvas.Font.Color)))
    end
    else if (dgMultiSelect in Self.Options) and
             SelectedRows.Find(Datalink.Datasource.Dataset.Bookmark, Index)then
        Canvas.Brush.Color := TColor(not(Longint(Canvas.Font.Color)));
    DefaultDrawDataCell(Rect, Field, State);
  end
  else if not Self.DefaultDrawing then
    DefaultDrawDataCell(Rect, Field, State);
end;

function TDBGridEx.GetActiveRecord: integer;
begin
  if DataLink.Active then
    Result := DataLink.ActiveRecord + 1
  else
    Result := 0;
end;

end.

----------------------------------------------
-广袤璀璨的银河,永无止境的梦想(梦无止境游银河) 博客挂了……
作者:
男 dingning (丁宁) ★☆☆☆☆ -
盒子活跃会员
2004/2/2 17:29:54
9楼: 很好啊,谢谢你啦
----------------------------------------------
-愿天下有情人终成眷属!
http://www.pro-thinking.com/bbs/
作者:
女 haikely (阿) ★☆☆☆☆ -
盒子活跃会员
2004/2/10 9:51:12
10楼: 有没有办法自定义颜色啊!Delphi的颜色都太重了.
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行89.84375毫秒 RSS