字符串网格的自绘画

  字符串网格文字输出的对齐

  比如想要使字符串网格的第一行字符居中对齐,其他行右对齐,可以通过StringGrid.OnDrawCell事件处理来完成。

procedure WriteText(ACanvas: TCanvas; const ARect: TRect; DX, DY: Integer;
const Text: string; Format: Word);
var
S: array[0..255] of Char;
B, R: TRect;

begin
with ACanvas, ARect do
begin
case Format of
DT_LEFT : ExtTextOut(Handle, Left + DX, Top + DY, ETO_OPAQUE or
ETO_CLIPPED, @ARect, StrPCopy(S, Text),
length(Text), nil);

DT_RIGHT : ExtTextOut(Handle, Right - TextWidth(Text) - 3, Top + DY,
ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);

DT_CENTER : ExtTextOut(Handle, Left + (Right - Left - TextWidth(Text)) div 2, Top + DY, ETO_OPAQUE or ETO_CLIPPED, @ARect,
StrPCopy(S, Text), Length(Text), nil);
end;
end;
end;


procedure TBEFStringGrid.DrawCell(Col, Row: Longint; Rect: TRect; State: TGridDrawState);
var

procedure Display(const S: string; Alignment: TAlignment);
const
Formats: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
begin
WriteText(Canvas, Rect, 2, 2, S, Formats[Alignment]);
end;

begin
case Row of
0 : { 居中对齐第一行 }
if (Col < ColCount) then
Display(Cells[Col,Row], taCenter)
else
{ 右对齐其他行 }
Display(Cells[Col,Row], taRight);
end;

end;

在字符串网格中画图

只需要处理StringGrid.OnDrawCell事件就可以了

with StringGrid1.Canvas do
begin
{...}
Draw(Rect.Left, Rect.Top, Image1.Picture.Graphic);
{...}
end;


本站原创及翻译内容保留版权,欢迎转贴,转贴时请注明转自Delphi深度探索