すみませんが教えてください。
StringGridのある一部のセルの色を塗りつぶすには
どのようにすれば良いですか?
知っている方がおりましたら、アドバイスお願いします。
これを参考に
http://www2.big.or.jp/~osamu/Delphi/delphi-browse.cgi?index=068882
あっ、文字色じゃなくて背景色なら、Brush.Color を設定して FillRect() 。
以下のコードでは、StringGrid1 の固定じゃないセルをクリックするとそのセルの
背景色が水色になります。もう一度クリックするともとに戻ります。
procedure TForm1.StringGrid1Click(Sender: TObject);
var
CurPos:TPoint;
ic,ir:integer;
r:TRect;
begin
with StringGrid1 do begin
CurPos := ScreenToClient(Mouse.CursorPos);
MouseToCell(CurPos.x,CurPos.y,ic,ir);
if Assigned(Objects[ic,ir]) then
Objects[ic,ir] := nil
else
Objects[ic,ir] := TObject(clAqua);
r := CellRect(ir,ic);
InvalidateRect(Handle,@r,true);
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do begin
if Assigned(Objects[ACol,ARow]) then begin
Canvas.Brush.Color := TColor(Objects[ACol,ARow]);
Canvas.FillRect(Rect);
end;
Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol,ARow]);
end;
end;
訂正です。すみません。
× r := CellRect(ir,ic);
○ r := CellRect(ic,ir);
× InvalidateRect(Handle,@r,true);
○ InvalidateRect(Handle,@r,false);
ツイート | ![]() |