Grid每个单元格内显示颜色问题? ( 积分: 50 )

  • 主题发起人 主题发起人 johnrice
  • 开始时间 开始时间
J

johnrice

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么在Grid的每个单元格内显示不同颜色,比如在单元格内加一点着重部分
 
我也想知道
 
自己画吧
 
在onDrawColumnCell事件里面实现
 
1.StringGrid1.DefaultDrawing 设为 False
2.OnDrawCell 事件中填写代码
下面代码将StringGrid1的背景色设为黄色:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do begin
Canvas.Brush.Color := clYellow;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left+2,Rect.Top+2,StringGrid1.Cells[aRow, aCol]);;
end;
end;
 
to kaida:
你的意思是把整个单元格设为一个颜色,我举个例子吧:
比如:一个单元格的内容是 张三(教师),"张三"是黑色,"教师"是红色,怎么实现呀?
 
一个格子只能一种颜色:
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;
 
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 := clBlue;
Canvas.FillRect(Rect);
Canvas.Font.Color := clWhite;
Canvas.TextOut(Rect.Left+2,Rect.Top+2,'张三');
Canvas.Font.Color := clYellow;
Canvas.TextOut(Rect.Left+2+Canvas.TextWidth('张三'),Rect.Top+2,'(教师)');
end else
begin
//.......
end;
end;
end;
 
热烈关注中,我也想实现这种功能!
 
接受答案了.
 
后退
顶部