在OnDrawCell事件中写:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (ACol = 2) and (ARow = 2) then
with StringGrid1.Canvas do
begin
Brush.Color := clRed;
Brush.Style := bsSolid;
FillRect(Rect);
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (ACol = 2) and (ARow = 2) then
with StringGrid1.Canvas do
begin
Brush.Color := clRed;
Brush.Style := bsSolid;
FillRect(Rect);
end;
end;
……
try
MyImage:=TImage.Create(self);
Rect:=stringgrid2.CellRect(j,i);//j,i指行列值
FileName:='c:\test.bmp';//用你需要的颜色或位图进行填充
Myimage.Picture.LoadFromFile(FileName);
stringgrid2.Canvas.Draw(Rect.Left,rect.Top,myImage.Picture.Graphic);
finally
Myimage.Free;
end;
……
if (ACol = 2) and (ARow = 2) then
with StringGrid1.Canvas do
begin
Brush.Color := clRed;
Brush.Style := bsSolid;
FillRect(Rect);
end;
别忘了给分
该段代码实现 选中的Cell颜色改变procedure TMain_frm.FillFocuText;
var MRect0,MRect1:TRect;
begin
With StringSGrid do
begin
MRect0:=CellRect(0,TempInt);
Canvas.Font.Color:=Font.Color;
Canvas.Brush.Color:=Color;
Canvas.TextRect(MRect0, MRect0.Left+2, MRect0.Top+2, Cells[0,TempInt]);
//删除上一次点击时留下的颜色 MRect0:=CellRect(0,Row);
Canvas.Font.Color:=ClWhite;
Canvas.Brush.Color:=ClNavy;
Canvas.TextRect(MRect0, MRect0.Left+2, MRect0.Top+2, Cells[0,Row]);
//给这次选中的行着色 TempInt:=StringGrid.Row; //记录这一次选中的行数,以便下次使用
end;
end;