一个格子只能一种颜色:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do begin
if Cells[ARow, ACol]='张三' then
begin
Canvas.Brush.Color := clBlack;
Canvas.Font.Color := clWhite;
end
else if Cells[ARow, ACol]='教师' then //教师 和 张三 不在一个格子里
begin
Canvas.Brush.Color := clRed;
Canvas.Font.Color := clYellow;
end
else
begin
Canvas.Brush.Color := clWhite;
Canvas.Font.Color := clBlack;
end;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left+2,Rect.Top+2,StringGrid1.Cells[aRow, aCol]);;
end;
end;