1、根据条件更改某一单元格的颜色 procedure TMainFrm.First_DGDrawColumnCell(Sender: TObject;const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if DM.FirstListStatus.AsString='生产'thenbegin First_DG.Canvas.Font.Color:=clGreen end elsebegin First_DG.Canvas.Font.Color:=clRed; end; if DataCol = 4then First_DG.DefaultDrawColumnCell(Rect,DataCol,Column,State); end; 2、多种改变方式 procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState); var i :integer; begin if gdSelected in State then Exit; for i :=0to (Sender as TDBGrid).Columns.Count-1do begin (Senderas TDBGrid).Columns[i].Title.Font.Name :='宋体'; //字体 (Senderas TDBGrid).Columns[i].Title.Font.Size :=9; //字体大小 (Senderas TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //字体颜色(红色) (Senderas TDBGrid).Columns[i].Title.Color :=$0000ff00; //背景色(绿色) end; if Query1.RecNo mod2 = 0then (Senderas TDBGrid).Canvas.Brush.Color := clInfoBk //定义背景颜色 else (Senderas TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定义背景颜色 DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); with (Sender as TDBGrid).Canvas do//画 cell 的边框 begin Pen.Color := $00ff0000; //定义画笔颜色(蓝色) MoveTo(Rect.Left, Rect.Bottom); //画笔定位 LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线 Pen.Color := $0000ff00; //定义画笔颜色(绿色) MoveTo(Rect.Right, Rect.Top); //画笔定位 LineTo(Rect.Right, Rect.Bottom); //画绿色的竖线 end; end; 3、纵向斑马线效果:实现网格的奇数列和偶数列分别以不同的颜色显示以区别相邻的数据列。 Case DataCol Mod 2 = 0ofTrue: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶数列用蓝色 False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇数列用浅绿色 End; DbGrid1.Canvas.Pen.Mode:=pmMask; DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); 4、纵向斑马线,同时以红色突出显示当前单元格效果:以突出显示当前选中的字段。 Case DataCol Mod 2 = 0ofTrue: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶数列用蓝色 False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇数列用浅绿色 End; If ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then If Not DbGrid1.SelectedRows.CurrentRowSelected then DbGrid1.Canvas.Brush.Color:=clRed; file://当前选中单元格显示红色 DbGrid1.Canvas.Pen.Mode:=pmMask; DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); 5、在数据网格中以红色突出显示当前选中的行。 设置DbGrid控件的Options属性中的dgRowSelect属性为真,Color属性为clAqua(背景色)在DbGrid的DrawColumnCell事件中编写如下代码: if ((State = [gdSelected]) or (State=[gdSelected gdFocused])) then DbGrid1.Canvas.Brush.color:=clRed; file://当前行以红色显示,其它行使用背景的浅绿色 DbGrid1.Canvas.pen.mode:=pmmask; DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); 6、行突显的斑马线效果:既突出当前行,又区分不同的列(字段)。 if ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) thenbegin Case DataCol Mod 2 = 0of True : DbGrid1.Canvas.Brush.color:=clRed; file://当前选中行的偶数列显示红色 False: DbGrid1.Canvas.Brush.color:=clblue; file://当前选中行的奇数列显示蓝色 end; DbGrid1.Canvas.pen.mode:=pmmask; DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); 7、横向斑马线, 同时以红色突显当前行效果。 Case Table1.RecNo mod2 = 0offile://根据数据集的记录号进行判断True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶数行用浅绿色显示 False: DbGrid1.Canvas.Brush.color:=clblue; file://奇数行用蓝色表示 end; if ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) thenfile://选中行用红色显示 DbGrid1.Canvas.Brush.color:=clRed; DbGrid1.Canvas.pen.mode:=pmMask; DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); 8、双向斑马线效果:即行间用不同色区分,同时,选中行以纵向斑马线效果区分不同的列。 Case Table1.RecNo mod2 = 0offile://根据数据集的记录号进行判断True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶数行用浅绿色显示 False: DbGrid1.Canvas.Brush.color:= clblue; file://奇数行用蓝色表示 end; If ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then Case DataCol mod2 = 0of True : DbGrid1.Canvas.Brush.color:=clRed; file://当前选中行的偶数列用红色 False: DbGrid1.Canvas.Brush.color:= clGreen; file://当前选中行的奇数列用绿色表示 end; DbGrid1.Canvas.pen.mode:=pmMask; DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); |
|
来自: 360大测试 > 《DELPHI眼前使用》