以下是比较复杂的 OnDrawCell实现了分行显示,供参考
procedure TForm1.SgDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
n:integer;
s,s1:string;
begin
if (gdfixed in state) and (ARow=0) then
begin
sg.Canvas.font.style:=sg.Canvas.font.style-[fsbold];
s:=sg.cells[acol,arow];
if (acol=0) then
begin
sg.Canvas.brush.color:=$ff00ff;
sg.Canvas.Rectangle(rect);
sg.Canvas.font.color:=$ff0000;
sg.Canvas.TextOut(4+rect.left,20+rect.Top,s);
exit;
end;
sg.Canvas.brush.color:=clSkyBlue;
sg.Canvas.Rectangle(rect);
n:=0;
while length(s)>n do
begin
s1:=copy(s,n+1,2) ;
if (pos('B',s1)>0)
or (pos('A',s1)>0)
or (pos('1',s1)>0)
then
sg.Canvas.font.color:=$0000ff
else
sg.Canvas.font.color:=$000000;
sg.Canvas.TextOut(4+rect.left,2+rect.Top+6*n,s1);
n:=n+2
end;
exit;
end;
if (gdFocused in state) then
begin
sg.Canvas.brush.color:=$00ffff;
sg.Canvas.Rectangle(rect);
sg.Canvas.font.color:=$000000;
sg.Canvas.font.style:=sg.Canvas.font.style-[fsbold];
sg.Canvas.TextOut(rect.left+2,rect.Top+2,sg.cells[acol,arow]);
exit;
end ;
if (gdfixed in state) and (ARow>0) then
begin
if arow=sg.row then
sg.Canvas.brush.color:=$00ffff
else
sg.Canvas.brush.color:=clMoneyGreen;
sg.Canvas.Rectangle(rect);
sg.Canvas.font.color:=$808000;
sg.Canvas.font.style:=sg.Canvas.font.style+[fsbold];
sg.Canvas.TextOut(rect.left+10,rect.Top+2,sg.cells[acol,arow]);
exit;
end ;
if strtoint('0'+trim(sg.cells[Acol,Arow]))=0 then
begin
sg.Canvas.brush.color:=$ff00ff;
sg.Canvas.Rectangle(rect);
exit;
end;
n:=strtoint('0'+trim(sg.cells[Acol,Arow]));
if (Arow<=nOfq) and (n>qs[aas[Acol-1].QesNo].n) then
begin
sg.Canvas.brush.color:=$0000ff;
sg.Canvas.Rectangle(rect);
sg.Canvas.font.color:=$000000;
sg.Canvas.font.style:=sg.Canvas.font.style-[fsbold];
sg.Canvas.TextOut(rect.left+3,rect.Top+2,sg.cells[acol,arow]);
exit;
end;
///////////////////////////////////////////////////
if (Arow<=nOfq) and not(valid(arow,acol)) then
begin
sg.Canvas.brush.color:=$00ff00;
sg.Canvas.Rectangle(rect);
sg.Canvas.font.color:=$000000;
sg.Canvas.font.style:=sg.Canvas.font.style-[fsbold];
sg.Canvas.TextOut(rect.left+3,rect.Top+2,sg.cells[acol,arow]);
exit;
end
end;