如何在StringGrid中使数据居中对齐?(20分)

N

natolee

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在StringGrid中使数据居中对齐?
 
换个控件吧!delphi自带的没有此功能.
使用topgrid到这儿下:
http://211.100.17.210:208/main.asp
 
很简单:)
procedure TfrmGrid.stgMainDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
//居中
if State = [gdFixed] then
begin
with Sender as TStringGrid do
begin
Canvas.FillRect(IncRect(Rect, -1));
Rect := IncRect(Rect, -2);
OffsetRect(Rect, 0,
(RowHeights[ARow] - Canvas.TextHeight('|')) div 2 - 1);
DrawText(Canvas.Handle,
PChar(Cells[ACol, ARow]),
Length(Cells[ACol, ARow]),
Rect,
DT_EXPANDTABS or DT_END_ELLIPSIS or DT_CENTER);
end;
end
else
with Sender as TStringGrid do
begin
Canvas.FillRect(IncRect(Rect, 0));
Rect := IncRect(Rect, -2); //IncRect是我的函数
if StrScan(PChar(Cells[ACol, ARow]), #13) <> nil then //多行
begin
DrawText(Canvas.Handle,
PChar(Cells[ACol, ARow]),
Length(Cells[ACol, ARow]),
Rect,
DT_EXPANDTABS or DT_WORDBREAK);
end
else //单行
begin
OffsetRect(Rect, 0,
(RowHeights[ARow] - Canvas.TextHeight('|')) div 2 - 1);
DrawText(Canvas.Handle,
PChar(Cells[ACol, ARow]),
Length(Cells[ACol, ARow]),
Rect,
DT_EXPANDTABS or DT_END_ELLIPSIS or DT_CENTER);
end;
end;
end;
 
上面的代码应该能再简化
 
接受答案了.
 
顶部