实现TStringGrid的左对齐、居中和右对齐

//sgrdQty: TStringGrid

procedure TfrmPDStock.sgrdQtyDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  sText: String;
  iTextWidth: Integer;
  rNum: Double;
begin
  inherited;
  with sgrdQty do
  begin
    sText := Cells[ACol, ARow];                          //輸出內容
    iTextWidth := canvas.TextWidth(sText);      //輸出內容寬度
    //
    if ARow = 0 then
      Canvas.TextRect(Rect, (Rect.left+Rect.right-iTextWidth) div 2, Rect.Top+2, sText)   //第1行, 居中
    else if ACol = 0 then
      Canvas.TextRect(Rect, Rect.Left+2, Rect.top+2, sText)                               //第1列, 左對齊
    else
    begin
      //如果<0以紅色顯示
      if TryStrToFloat(sText, rNum) and (rNum < 0) then
        Canvas.Font.Color := clRed
      else
        Canvas.Font.Color := clWindowText;
      //
      Canvas.TextRect(Rect, Rect.right - iTextWidth - 2 ,Rect.Top + 2, sText);            //其它, 右對齊心
    end;
  end;
end;

实现TStringGrid的左对齐、居中和右对齐_第1张图片

你可能感兴趣的:(实现TStringGrid的左对齐、居中和右对齐)