gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:17:00 26 4
gpt4 key购买 nike

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

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

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;

它看起来像这样:

StringGrid with image aligned

关于delphi - 单元格中的字符串网格和图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7419344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com