procedure TFrmMoney.DrawGrid4MoneyCell
(
View: TcxCustomGridTableView;
ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo;
var ADone: Boolean
);
var
value:double;
I,Len,InxSep,dig,txtLen: Integer;
fmtStr,DrawText, DrawSubText: String;
SubTextWidth,SubTextHeight: Integer;
LineLeft: Integer;
IsNegative : Boolean;
Rect,SubRect:TRect;
Prop:TcxCurrencyEditProperties;
grdCanvas:TcxCanvas;
begin
ADone:=AViewInfo.Properties is TcxCurrencyEditProperties;//不绘制非货币属性的值
if not ADone then exit;
Prop:=AViewInfo.Properties as TcxCurrencyEditProperties;
dig:=Prop.DecimalPlaces;
value:=AViewInfo.Value;
InxSep:=dig+1;
fmtStr:='0.';
for i:=1 to dig do fmtStr:=fmtStr+'0';
fmtStr := FormatFloat(fmtStr, Value);
fmtStr := StringReplace(fmtStr, '.', '', []);
IsNegative := Value<0;
if IsNegative
then fmtStr:=Copy(fmtStr,2,Length(fmtStr)-1);//去除负号
//下面循环去除前导零
txtLen:=Length(fmtStr);
if txtLen>0 then begin
I:=0;
repeat
inc(I);
until (fmtStr<>'0')or (I>txtLen);
DrawText:='';
if I=1 then DrawText:=fmtStr
else for I:=I to txtLen do
DrawText:=DrawText+fmtStr;
end else DrawText:=fmtStr;
//结束
Rect:=AViewInfo.ContentBounds;
ACanvas.Brush.Color := clWhite;
ACanvas.FillRect(Rect);
ACanvas.Font.Color := clNavy;
grdCanvas:=(View.Control as TcxGrid).Canvas;
SubTextWidth :=grdCanvas.TextWidth('佰')+4;
SubTextHeight:=grdCanvas.TextHeight('佰');
Len:=(Rect.Right-Rect.Left)div SubTextWidth;
txtLen:=Length(DrawText);
for I := 1 to Len do begin
if I > txtLen+1
then DrawSubText := ''
else if I=txtLen+1 then DrawSubText:='¥'
else DrawSubText :=Copy(DrawText, Length(DrawText) - I + 1, 1);
with ACanvas do begin
Pen.Color := GetPenColor(I,InxSep);
if IsNegative //负数则为红色
then Font.Color := clRed; //修改本句应可实现你的需求
LineLeft := Rect.Right - SubTextWidth * (I - 1);
SubRect.Left:= LineLeft - SubTextWidth + 2;
SubRect.Top:= Rect.Top + (Rect.Bottom - Rect.Top - SubTextHeight) div 2;
SubRect.Right:=LineLeft-2;
SubRect.Bottom:=Rect.Top + (Rect.Bottom - Rect.Top+SubTextHeight) div 2;
DrawTexT(DrawSubText,SubRect,cxAlignHCenter or cxAlignVCenter);
MoveTo(LineLeft, Rect.Top);
LineTo(LineLeft, Rect.Bottom);
end;
end;
end;