单元格中的字符串网格和图形

1 delphi delphi-2010

我已将图标放入字符串网格中,但遇到了并非所有图形都对齐的问题。我尝试重新调整文本居中以使图标对齐,但没有成功。我尝试研究位图及其功能,但我还没有(所以我认为)找到任何对我有帮助的东西。有人可以帮我吗?

编辑(来自错误地回答问题时添加的代码):

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);
Run Code Online (Sandbox Code Playgroud)

Ken*_*ite 5

这是一个示例(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;
Run Code Online (Sandbox Code Playgroud)

它看起来是这样的:

图像对齐的 StringGrid