我已经将图标放在字符串网格中,但是我遇到了一个问题,即并非所有图形都对齐。我试图重做文本居中以使图标对齐但没有运气。我已经尝试研究位图及其功能,但是我还没有找到任何可以帮助我的东西。谁能帮我吗?

编辑(从错误回答问题中添加的代码):

bitmap := Tbitmap.Create;
bitmap.LoadFromFile('equal.bmp');
bitmap.SetSize(150,60);
stringgrid1.Canvas.StretchDraw(stringgrid1.CellRect(3,J), bitmap);
SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
StringGrid1.Canvas.TextRect(stringgrid1.CellRect(3,J),
   (stringgrid1.CellRect(3,J).Left+stringgrid1.CellRect(3,J).Right) div 2,

stringgrid1.CellRect(3,J).Top + 5,StringGrid1.Cells[3,J]);
SetTextAlign(StringGrid1.Canvas.Handle, TA_LEFT);

最佳答案

这是一个示例(Delphi 7,因为这是我所需要的,但是代码应该在D2010中可用):

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Bmp:  TBitmap;
  CellText: string;
  R: TRect;
const
  L_PAD = 5;  // Amount between right side of image and start of text
  T_PAD = 5;   // Amount between top of cell and top of text
begin
  // Some text to display in cells.
  CellText := Format('Row: %d Col: %d', [ARow, ACol]);

  // Draw an image along the left side of each cell in the first
  // col (not the fixed ones, which we'll leave alone)
  if ((ACol = 1) or (ACol = 3)) and  (ARow > 0) then
  begin
    Bmp := TBitmap.Create;
    try
      Bmp.LoadFromFile('C:\glyfx\common\bmp\24x24\favorites24.bmp');
      if ACol = 1 then // left align image
      begin
        R.Top := Rect.Top + 1;
        R.Left := Rect.Left + 1;
        R.Right := R.Left + Bmp.Width;
        R.Bottom := R.Top + Bmp.Height;
        StringGrid1.Canvas.StretchDraw(R, Bmp);
        StringGrid1.Canvas.TextOut(R.Right + L_PAD, R.Top + T_PAD, CellText);
      end
      else
      begin // right align image
        StringGrid1.Canvas.TextOut(Rect.Left + L_PAD,
                                   Rect.Top + L_PAD,
                                   CellText);
        R.Top := Rect.Top + 1;
        R.Left := Rect.Right - Bmp.Width - 1;
        R.Right := Rect.Right - 1;
        R.Bottom := R.Top + L_PAD + Bmp.Height;
        StringGrid1.Canvas.StretchDraw(R, Bmp);
      end;
    finally
      Bmp.Free;
    end;
  end
  else
    StringGrid1.Canvas.TextOut(Rect.Left + L_PAD, Rect.Top + T_PAD, CellText);
end;


看起来是这样的:

07-26 09:36