//CXGrid在OnCustomDrawCell里处理显示 procedure TMainForm.cxGrid1DBTableView1WageCustomDrawCell( Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean); var S,ATextToDraw: String; i, x, y, iTextWidth: Integer; Rect: TRect; begin ATextToDraw := AViewInfo.GridRecord.DisplayTexts[AViewInfo.Item.Index]; Rect:=AViewInfo.Bounds; with ACanvas.Canvas do begin iTextWidth := TextWidth('8') + 4; y := Rect.Top + (Rect.Bottom - Rect.Top - TextHeight('8')) div 2; S := StringReplace(FormatFloat('0.00', AViewInfo.Value), '.', ', []); if AViewInfo.Value < 0 then Font.Color := clRed;
FillRect(Rect);
i := (Rect.Right - Rect.Left) div iTextWidth; repeat x := Rect.Right - iTextWidth * i; if i <= Length(S) then TextOut(x+3, y, S[Length(S)-i+1]);
if i = 2 then Pen.Color := clRed else if (i = 0) or ( (i>2) and (((i-2) mod 3) = 0) ) then Pen.Color := clGreen else Pen.Color := clSilver; MoveTo(x, Rect.Top); LineTo(x, Rect.Bottom); Dec(i); until (i<0); ACanvas.FrameRect(Rect); ADone:=True; end; end;
间隔色不且反差大太,而且应尽量避开gdSelected的单元格,把以下代码加到if Field.DataType <> ftBCD then Exit;前面:
with TDBGrid(Sender) do begin if (not (gdSelected in State)) and ((DataSource.DataSet.RecNo mod 2) = 1) then Canvas.Brush.Color := $EEEEEE; DefaultDrawDataCell(Rect, Field, State); end;
改进了一下加入了“¥” procedure TMainForm.DestGridDrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); var S: String; i, x, y, iTextWidth,h: Integer; begin if Field.DataType <> ftBCD then Exit;
with TDBGrid(Sender).Canvas do begin h:=1; iTextWidth := TextWidth('8') + 4; y := Rect.Top + (Rect.Bottom - Rect.Top - TextHeight('8')) div 2; S := '¥'+trim(StringReplace(FormatFloat('0.00', Field.AsCurrency), '.', ', [])); if Field.AsCurrency < 0 then Font.Color := clRed;
FillRect(Rect);
i := (Rect.Right - Rect.Left) div iTextWidth; repeat x := Rect.Right - iTextWidth * i; if i <= Length(S) then if S[Length(S)-i+1] in ['0'..'9','-'] then TextOut(x+3, y, S[Length(S)-i+1]) else if h=1 then begin TextOut(x+10, y, '¥'); h:=2; end;
if i = 2 then Pen.Color := clRed else if (i = 0) or ( (i>2) and (((i-2) mod 3) = 0) ) then Pen.Color := clGreen else Pen.Color := clSilver; MoveTo(x, Rect.Top); LineTo(x, Rect.Bottom); Dec(i); until (i<0); end; end;