Witam,
Na forum bylo jak ustawić kolor wybranej komórki w TStringGrid, ale nigdzie się nie doszukalem jak pobrać kolor komórki.
Z góry dzieki
Witam,
Na forum bylo jak ustawić kolor wybranej komórki w TStringGrid, ale nigdzie się nie doszukalem jak pobrać kolor komórki.
Z góry dzieki
Nie chce mi sie tłumaczyć, wiec wrzucam ci kod programu, który kiedyś napisałem - kalendarz, po którym da sie chodzić kursorami i jak się kliknie Enter to zaznacza wybraną komórkę StringGrida na niebiesko(dokładnie na clAqua) - mam nadzieję że dojdziesz
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
ComboBox1: TComboBox;
Edit1: TEdit;
procedure FormShow(Sender: TObject);
procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
a,b:integer;
kolumna,wiersz:integer;
tablica:array[0..6,1..7] of integer;
const
styczen=31;
marzec=31;
maj=31;
lipiec=31;
sierpien=31;
pazdziernik=31;
grudzien=31;
luty=28;
luty1=29;
kwiecien=30;
czerwiec=30;
wrzesien=30;
listopad=30;
implementation
{$R *.DFM}
procedure kalendarz(rok,miesiac:word;StringGrid:TStringGrid);
var
i,j,k:integer;
dz_tyg,liczbadni:integer;
data:TDateTime;
begin
data:=EncodeDate(rok,miesiac,1);
case miesiac of
1:liczbadni:=styczen;
2:liczbadni:=luty1;
3:liczbadni:=marzec;
4:liczbadni:=kwiecien;
5:liczbadni:=maj;
6:liczbadni:=czerwiec;
7:liczbadni:=lipiec;
8:liczbadni:=sierpien;
9:liczbadni:=wrzesien;
10:liczbadni:=pazdziernik;
11:liczbadni:=listopad;
12:liczbadni:=grudzien;
end;
if (miesiac=2) and ((rok mod 4) = 0) then
liczbadni:=luty1;
if (miesiac=2) and ((rok mod 400) <> 0) and ((rok mod 100) = 0) then
liczbadni:=luty;
dz_tyg:=DayOfWeek(data);
for i:=0 to 6 do
for j:=0 to 7 do
StringGrid.Cells[i,j]:='';
StringGrid.Cells[0,0]:='Pon';
StringGrid.Cells[1,0]:='Wt';
StringGrid.Cells[2,0]:='Śr';
StringGrid.Cells[3,0]:='Czw';
StringGrid.Cells[4,0]:='Pt';
StringGrid.Cells[5,0]:='So';
StringGrid.Cells[6,0]:='Nie';
case dz_tyg of
2: i:=0;
3: i:=1;
4: i:=2;
5: i:=3;
6: i:=4;
7: i:=5;
1: i:=6;
end;
j:=1;
for k:=1 to liczbadni do
begin
StringGrid.Cells[i,j]:=IntToStr(k);
i:=i+1;
if i>6 then
begin
i:=0;
j:=j+1;
end;
end;
end;
procedure TForm1.FormShow(Sender: TObject);
var
miesiac,rok,dzien:word;
data_probna:TDateTime;
begin
data_probna:=date;
DecodeDate(data_probna,rok,miesiac,dzien);
ComboBox1.Text:=ComboBox1.Items.Strings[miesiac-1];
Edit1.Text:=IntToStr(rok);
kalendarz(rok,miesiac,StringGrid1);
end;
procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
begin
a:=kolumna;
b:=wiersz;
if StringGrid1.Cells[a,b]<>'' then
if tablica[a,b]=0 then
tablica[a,b]:=1
else
tablica[a,b]:=0;
StringGrid1.Refresh;
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
i,j:integer;
begin
for i:=0 to 6 do
for j:=1 to 7 do
begin
if tablica[i,j]=1 then
begin
a:=i;
b:=j;
end;
//tu załatwia siekolorowanie
if ((ACol >= a) and (ACol < a+1)) and ((ARow >=b) and (ARow < b+1)) then
begin
StringGrid1.Canvas.Brush.Color:=clAqua; //tutaj ustaw kolor tła
StringGrid1.Canvas.Rectangle(Rect);
StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow] );
end;
end;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
//to coś jest potrzebne
kolumna:=Acol;
wiersz:=Arow;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
kalendarz(StrToInt(Edit1.Text),ComboBox1.ItemIndex+1,StringGrid1);
end;
end.
Hmm..., zapisywanie koloru do tablicy przy rysowaniu to też jakaś metoda. Szczerze powiedziawszy rozważałem już taką, ale mi chodzi o jakiś prostszy sposób, jedna funkcja która pobiera kolor komórki.
Czy jest w ogóle coś takiego ??
stringgird1.onDrawcell(...)
begin
with Rect do
begin
if Cells[Acol,ARow]='co chcesz' then
begin
Canvas.Brush.color= jakis tam kolor(np clBlue);
Canvas.FillRect(Rext);
Canvas.TextOut(left,top,Cells{Acol,ARow);
end else
begin
...
end
end
end
Gumis nie rozumiesz. Ja chce pobrać kolor komórki a nie go ustawić. Tak to ja też umie :(