TStringGrid中的Delphi多色值

TStringGrid中的Delphi多色值,delphi,delphi-7,Delphi,Delphi 7,我要货币价值​​在TStringGrid表中使用不同的颜色小数。你怎么能这么做 您需要通过实现处理程序自己绘制单元格 大概是这样的: procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var Grid: TStringGrid; S: string; Val: Double; FracVal, IntVa

我要货币价值​​在TStringGrid表中使用不同的颜色小数。你怎么能这么做


您需要通过实现处理程序自己绘制单元格

大概是这样的:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Grid: TStringGrid;
  S: string;
  Val: Double;
  FracVal, IntVal: Integer;
  FracStr, IntStr: string;
  IntW, FracW, W, H: Integer;
  Padding: Integer;
const
  PowersOfTen: array[0..8] of Integer =
    (
      1,
      10,
      100,
      1000,
      10000,
      100000,
      1000000,
      10000000,
      100000000
    );
  Decimals = 2;
  BgColor = clWhite;
  IntColor = clBlack;
  FracColor = clRed;
begin

  Grid := Sender as TStringGrid;

  if (ACol < Grid.FixedCols) or (ARow < Grid.FixedRows) then
    Exit;

  Grid.Canvas.Brush.Color := BgColor;
  Grid.Canvas.FillRect(Rect);

  S := Grid.Cells[ACol, ARow];
  Padding := Grid.Canvas.TextWidth('0') div 2;

  if not TryStrToFloat(S, Val) or not InRange(Val, Integer.MinValue, Integer.MaxValue) then
  begin
    Grid.Canvas.TextRect(Rect, S, [tfSingleLine, tfVerticalCenter, tfLeft]);
    Exit;
  end;

  IntVal := Trunc(Val);
  IntStr := IntVal.ToString;
  if Decimals > 0 then
    IntStr := IntStr + FormatSettings.DecimalSeparator;
  IntW := Grid.Canvas.TextWidth(IntStr);
  FracVal := Round(Frac(Abs(Val)) * PowersOfTen[Decimals]);
  FracStr := FracVal.ToString.PadRight(Decimals, '0');
  if Decimals = 0 then
    FracStr := '';
  FracW := Grid.Canvas.TextWidth(FracStr);
  W := IntW + FracW;
  H := Grid.Canvas.TextHeight(IntStr);

  if W >= Grid.ColWidths[ACol] - 2*Padding then
  begin
    S := '###';
    Grid.Canvas.TextRect(Rect, S, [tfSingleLine, tfVerticalCenter, tfRight]);
  end
  else
  begin
    Grid.Canvas.Font.Color := IntColor;
    Grid.Canvas.TextOut(Rect.Right - Padding - W,
      Rect.Top + Rect.Height div 2 - H div 2, IntStr);
    Grid.Canvas.Font.Color := FracColor;
    Grid.Canvas.TextOut(Rect.Right - Padding - FracW,
      Rect.Top + Rect.Height div 2 - H div 2, FracStr);
  end;

end;
此代码将按原样写入左对齐的非数字数据。对于数字数据,它将以固定的小数位数绘制值。您可以选择小数0..8,以及整数和小数部分的颜色。如果数字不适合其单元格,则将显示

我还没有完全测试代码。我把它留给你做练习


更新:对不起,我忘了你正在使用Delphi7。这意味着您需要将IntVal.ToString替换为IntToStrIntVal,依此类推。

您需要通过实现处理程序自己绘制单元格

大概是这样的:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Grid: TStringGrid;
  S: string;
  Val: Double;
  FracVal, IntVal: Integer;
  FracStr, IntStr: string;
  IntW, FracW, W, H: Integer;
  Padding: Integer;
const
  PowersOfTen: array[0..8] of Integer =
    (
      1,
      10,
      100,
      1000,
      10000,
      100000,
      1000000,
      10000000,
      100000000
    );
  Decimals = 2;
  BgColor = clWhite;
  IntColor = clBlack;
  FracColor = clRed;
begin

  Grid := Sender as TStringGrid;

  if (ACol < Grid.FixedCols) or (ARow < Grid.FixedRows) then
    Exit;

  Grid.Canvas.Brush.Color := BgColor;
  Grid.Canvas.FillRect(Rect);

  S := Grid.Cells[ACol, ARow];
  Padding := Grid.Canvas.TextWidth('0') div 2;

  if not TryStrToFloat(S, Val) or not InRange(Val, Integer.MinValue, Integer.MaxValue) then
  begin
    Grid.Canvas.TextRect(Rect, S, [tfSingleLine, tfVerticalCenter, tfLeft]);
    Exit;
  end;

  IntVal := Trunc(Val);
  IntStr := IntVal.ToString;
  if Decimals > 0 then
    IntStr := IntStr + FormatSettings.DecimalSeparator;
  IntW := Grid.Canvas.TextWidth(IntStr);
  FracVal := Round(Frac(Abs(Val)) * PowersOfTen[Decimals]);
  FracStr := FracVal.ToString.PadRight(Decimals, '0');
  if Decimals = 0 then
    FracStr := '';
  FracW := Grid.Canvas.TextWidth(FracStr);
  W := IntW + FracW;
  H := Grid.Canvas.TextHeight(IntStr);

  if W >= Grid.ColWidths[ACol] - 2*Padding then
  begin
    S := '###';
    Grid.Canvas.TextRect(Rect, S, [tfSingleLine, tfVerticalCenter, tfRight]);
  end
  else
  begin
    Grid.Canvas.Font.Color := IntColor;
    Grid.Canvas.TextOut(Rect.Right - Padding - W,
      Rect.Top + Rect.Height div 2 - H div 2, IntStr);
    Grid.Canvas.Font.Color := FracColor;
    Grid.Canvas.TextOut(Rect.Right - Padding - FracW,
      Rect.Top + Rect.Height div 2 - H div 2, FracStr);
  end;

end;
此代码将按原样写入左对齐的非数字数据。对于数字数据,它将以固定的小数位数绘制值。您可以选择小数0..8,以及整数和小数部分的颜色。如果数字不适合其单元格,则将显示

我还没有完全测试代码。我把它留给你做练习


更新:对不起,我忘了你正在使用Delphi7。这意味着您需要将IntVal.ToString替换为IntToString,依此类推。

请参阅f.i,TStringGrid的OnDrawCell事件,如联机帮助中所述。我查看了联机帮助。但是我没有找到任何关于多色网格的帮助canvas@zuluman:那是因为这取决于程序员,也就是说,你自己来实现它。@Martyn如果你允许的话,我认为你提到的答案不能回答这个问题。另一个,虽然指明了OnDrawCell编码的位置,但它只涉及背景色。此问题要求更改文本的颜色,特别是值的小数。从@Andreas的解决方案中可以看出,它比绘制背景更复杂。Thanks@TomBrunberg,我已经重新打开了。显然今天过得不好。顺便说一句,我肯定有很多关于这个的问题,见f.I,TStringGrid的OnDrawCell事件,在线帮助中有描述。我查看了在线帮助。但是我没有找到任何关于多色网格的帮助canvas@zuluman:那是因为这取决于程序员,也就是说,你自己来实现它。@Martyn如果你允许的话,我认为你提到的答案不能回答这个问题。另一个,虽然指明了OnDrawCell编码的位置,但它只涉及背景色。此问题要求更改文本的颜色,特别是值的小数。从@Andreas的解决方案中可以看出,它比绘制背景更复杂。Thanks@TomBrunberg,我已经重新打开了。显然今天过得不好。顺便说一句,我肯定关于这件事一定有很多问题