请看下面的代码:
TStringGrid has an Objects property that you can assign objects. Create an object which contains a TColor variable and assign it to Objects[col,row], so you can retrieve it any time. You must assign an OnDrawCell Event to the StringGrid, drawing the text on the Correct color. To retrieve the selection, you can use Selection property, that contains the user Selection. This should be something like that:
type
TStrColor = class(TObject)
public
Color : TColor; {you could also define a private variable and
public access methods}
end;
...
procedure TForm1.FormCreate(Sender:TObject)
var
i,j : Integer;
begin
With StringList1 do
for i := 0 to ColCount-1
for j := 0 to RowCount-1
Objects[i,j] := TStrColor.Create;
end;
...
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
var
OldColor : TColor;
begin
with StringGrid1.Canvas do begin
OldColor := Font.Color;
Font.Color := (StringGrid1.Objects[col,row] as TStrColor).Color;
TextOut(Rect.Left+2,Rect.Top+2,StringGrid1.Cells[Col,Row]);
Font.Color := OldColor;
end;
end;
...
procedure TForm1.ProcessSelection(Sender: TObject);
var
i,j : Integer;
begin
With StringGrid1.Selection do
For i := left to right do
for j := top to bottom do
MessageDlg(IntToStr(i)+','+IntToStr(j)+'-'+
IntToStr((StringGrid1.Objects[i,j] as TStrColor).Color),
mtInformation,[mbOk],0);
end;
This component cannot allow multiple selections....