在DrawCell中就可以呀,在该事件判断是否选中了一个cell,如果选中就画一个图标
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if gdSelected in State then
StringGrid1.Canvas.Draw(Rect.Left, Rect.Top, Image1.Picture.Graphic);
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if gdSelected in State then
StringGrid1.Canvas.Draw(Rect.Left, Rect.Top, Image1.Picture.Graphic);
end;
当选中一行后,如果当前行的指定Cell为空就显示一个icon,如果已经有一个icon了就去掉这个icon,显示空,不过此操作对别的行没有影响,别的行如果有icon依旧让它显示。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs, Graphics,
Grids, ExtCtrls;type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
private
{ Private declarations }
FDraw: array of boolean;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
SetLength(FDraw, StringGrid1.RowCount);
end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
i: integer;
begin
for i := 1 to StringGrid1.RowCount - 1 do
if FDraw[ARow] and (ACol = 3) and (ARow <> i) then
StringGrid1.Canvas.Draw(Rect.Left, Rect.Top, Image1.Picture.Graphic);
end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
ARect: TRect;
begin
with StringGrid1 do
begin
ARect := CellRect(ACol, ARow);
if (ARow <>0) and (ACol <> 0) then
if not FDraw[ARow] then
begin
Canvas.Draw(ARect.Left, ARect.Top, Image1.Picture.Graphic);
FDraw[ARow] := true;
end
else begin
Canvas.Draw(ARect.Left, ARect.Top, nil);
FDraw[ARow] := false;
end;
end;
end;end.