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

时间:2011-09-14 15:58:31

标签: 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);

1 个答案:

答案 0 :(得分:4)

这是一个例子(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;

这是它的样子:

StringGrid with image aligned