1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls, ExtCtrls;
type TForm1 = class(TForm) StringGrid: TStringGrid; procedure FormCreate(Sender: TObject); procedure StringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); private end;
TCellObject = class(TObject) private FActivated: Boolean; public constructor create; destructor destroy; override; published property Activated: Boolean read FActivated write FActivated; end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); var i, j: integer; CObject: TCellObject; begin StringGrid.Cells[0, 1] := 'Monday'; StringGrid.Cells[0, 2] := 'Tuesday'; StringGrid.Cells[0, 3] := 'Wednesday'; StringGrid.Cells[0, 4] := 'Thursday'; StringGrid.Cells[0, 5] := 'Friday'; StringGrid.Cells[0, 6] := 'Saturday'; StringGrid.Cells[0, 7] := 'Sunday';
for i := 1 to 50 do begin StringGrid.ColWidths[i] := 5; end;
for i := 1 to 7 do begin for j := 1 to 50 do begin CObject := TCellObject.create; StringGrid.Objects[j, i] := CObject; end; end; end;
procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var LColor: TColor; begin
if (state = [gdSelected]) or (state = [gdFocused]) then begin
with TStringGrid(Sender), Canvas do begin TCellObject(StringGrid.Objects[ACol, ARow]).Activated := not TCellObject(StringGrid.Objects[ACol, ARow]).Activated; end; end;
if (ACol > 0) and (ARow > 0) and (ACol <= 50) and (ARow <= 7) then begin try if TCellObject(StringGrid.Objects[ACol, ARow]).Activated then LColor := clGreen else LColor := clRed;
with TStringGrid(Sender).Canvas do begin Brush.Color := LColor; FillRect(Rect); end; except end; end; end;
constructor TCellObject.create; begin FActivated := True; end;
destructor TCellObject.destroy; begin
end;
end. |