DBGRID的顏色(50分)

  • 主题发起人 主题发起人 lilywin
  • 开始时间 开始时间
L

lilywin

Unregistered / Unconfirmed
GUEST, unregistred user!
如何讓DBGRID中的記錄分行顯示顏色,即記錄號為單(1.3.5……)時是為淡黃色,記錄號為雙(2.4.6……)時為淡藍色
 

在dbgrid中ondrawcolumncell事件中写入
if ....条件 then begin
dbgrid1.canvas.brush.color:=clblue; //整条记录颜色
dbgrid1.canvas.font.color:=clblue; //字体颜色
dbgrid1.canvas.fillrect(rect);
dbgrid1.defauldrawcolumncell(rect,datacol,column,state);
end;
 
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (DataCol div 2) = 0 then
begin
dbgrid1.canvas.brush.color:=clyellow; //整条记录颜色
dbgrid1.canvas.font.color:=clblue; //字体颜色
dbgrid1.canvas.fillrect(rect);
dbgrid1.defauldrawcolumncell(rect,datacol,column,state);
end
else begin
dbgrid1.canvas.brush.color:=clblue; //整条记录颜色
dbgrid1.canvas.font.color:=clblue; //字体颜色
dbgrid1.canvas.fillrect(rect);
dbgrid1.defauldrawcolumncell(rect,datacol,column,state);
end;
end;
end;
 
case (DataCol mod 4) of
0 : AColor := clWhite;
1 : AColor := $00FFEEEE;
2 : AColor := clWhite;
3 : AColor := $00F0F0FF;
end
else
AColor := clNavy;
end;
 
全贴出来了:--我自己珍藏的,很简单的-随光标移动:
在dbgrid光标所在行的颜色值:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (state=[gdselected]) then
begin
dbgrid1.Canvas.Brush.Color:=clred;
dbgrid1.Canvas.FillRect(rect);
dbgrid1.Canvas.Pen.Color:=clwhite;
dbgrid1.Canvas.TextOut(rect.Left,rect.Top,column.field.asstring);
end;
end;
首先要设置dbgrid的options的dbrowselected为true;
 
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
const
AlignFlags : array [TAlignment] of Integer =
( DT_LEFT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
DT_RIGHT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
DT_CENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX );
var
Value: string;
RecColor:TColor;
R:TRect;
dbg:TDBGrid;
cs:TCanvas;
begin
Value := '';
if Assigned(Column.Field) then
Value := Column.Field.DisplayText;
dbg:=Sender as TDBGrid;
if gdselected in State then
RecColor:=clHighlight
else if dbg.DataSource.DataSet.RecNo mod 2 =0 then
RecColor:=$00C0E0E0
else
RecColor:=$00E8FFFF;
with Rect do
R:= Classes.Rect(Left+2,Top+ 2, Right-2, Bottom);
dbg.Canvas.Brush.Color:=RecColor;
dbg.Canvas.FillRect(Rect);
DrawText(dbg.Canvas.Handle, PChar(Value), Length(Value), R,
AlignFlags[Column.Alignment]);
cs:=TCanvas.Create;
cs.Handle:=GetDC(dbg.Handle);
try
cs.FrameRect(Rect);
finally
ReleaseDC(dbg.Handle,cs.Handle);
cs.Free;
end;
end;
end;
 
后退
顶部