StringGrid1 Kolorowanie komórki naciśniętej.

0

cd

x := StringGrid1.Col;
y := StringGrid1.Row;

  begin

  if(StringGrid1.Color=clBtnFace) then
    begin


    StringGrid1.Color:=clLime;
    ilosc:=ilosc+1;
    Label10.Caption:=FloatToStr(ilosc);
    end
 else
  begin
if(ilosc<4) then
begin
    StringGrid1.Color:=clBtnFace;
    ilosc:=ilosc-1;
    Label10.Caption:=FloatToStr(ilosc);
    end;

//cd///
Mam taki kod i po naciśnięciu komórki cały stringgrid robi mi się kolorowy a chciałbym żeby była tylko jedna komórka jak to zrobić ?
Ktoś pomoże ?

0

Zrób to w obsłudze zdarzenia OnDrawCell. Np coś takiego:

StringGrid1.Canvas.Brush.Color := clRed;
StringGrid1.Canvas.Rectangle(Rect);

Tylko najpierw musisz sprawdzić czy State jest na selected.

0
Mpas napisał(a)

Mam taki kod i po naciśnięciu komórki cały stringgrid robi mi się kolorowy a chciałbym żeby była tylko jedna komórka jak to zrobić ?

Do malowania pojedynczej komórki masz zdarzenie OnDrawCell, jak słusznie wskazał Ci @sephirot8608;

Tutaj masz przykładowy kod malujący komórkę na czerwono jeżeli jest zaznaczona, w przeciwnym wypadku na biało (nie sprawdza natomiast indeksów, więc tytułowe komórki będą też białe):

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
                                     State: TGridDrawState);
begin
  with TStringGrid(Sender).Canvas do
    begin
      { SPRAWDZENIE STANU ZAZNACZENIA KOMÓRKI }
      if gdSelected in State then
        begin
          Pen.Color := clRed;
          Brush.Color := clRed;
        end
      else
        begin
          Pen.Color := clWhite;
          Brush.Color := clWhite;
        end;

      { WYPEŁNIENIE KOMÓRKI USTALONYM KOLOREM }
      Rectangle(Rect);

      { USUNIĘCIE WIEJSKIEJ RAMKI Z KOMÓRKI }
      if gdFocused in State then
        begin
          Pen.Color := Pen.Color xor $FFFFFF;
          DrawFocusRect(Rect);
        end;
    end;
end;

Jeżeli chcesz rozpoznać stan zaznaczenia (i nie tylko) komórki, musisz zaznajomić się z parametrem State i możliwymi wartościami w tym zbiorze:

  • gdSelected - The cell is currently selected.
  • gdFocused - The cell has input focus.
  • gdFixed - The cell is in the fixed region of the grid.
  • gdRowSelected - The row is selected.
  • gdHotTrack - The cell is hot-tracked.
  • gdPressed - The cell is being pressed on.
    Źródło: Grids.TGridDrawState w Embarcadero
0

Właśnie tu mam problem bo zamiast zaznaczyć mi jedna komórke to zaznacza po kliknieciu caly stringgrid

tak wyglada moj caly kod :
Załączam mój program +screena
i Sprecyzuje Chciałbym żeby można było zazczaczyc komórke i zeby zmieniala kolor
i zeby po nacisnieciu innej tak samo sie robilo
Prosze o pomoc sam chyba nie potrafie tak zrobic aby mi pokolorowalo komórki


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
var
x,y,ilosc:integer;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Button1: TButton;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
procedure StringGrid1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.StringGrid1Click(Sender: TObject);
begin
Label12.Caption:='';
x := StringGrid1.Col;
y := StringGrid1.Row;

begin
if(StringGrid1.Cells[x,y]='x') then
begin
StringGrid1.Cells[x,y]:='';
ilosc:=ilosc-1;
Label10.Caption:=FloatToStr(ilosc);
end
else
begin
if(ilosc<4) then
begin
StringGrid1.Cells[x,y]:='x';
ilosc:=ilosc+1;
Label10.Caption:=FloatToStr(ilosc);
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);

begin

//Akord A
if(StringGrid1.Cells[0,0]='')and(StringGrid1.Cells[1,1]='x')and(StringGrid1.Cells[3,2]='x')and(StringGrid1.Cells[4,3]='x')and(ilosc=3) then Label12.Caption:='Akord A-Dur' ;
//Akord As-Dur
if(StringGrid1.Cells[0,0]='')and(StringGrid1.Cells[0,1]='x')and(StringGrid1.Cells[2,2]='x')and(StringGrid1.Cells[3,3]='x')and(ilosc=3) then Label12.Caption:='Akord As-Dur' ;
//Akord a-mol
if(StringGrid1.Cells[0,0]='')and(StringGrid1.Cells[1,1]='x')and(StringGrid1.Cells[2,2]='x')and(StringGrid1.Cells[4,3]='x')and(ilosc=3) then Label12.Caption:='Akord a-mol' ;

//Akord B
if(StringGrid1.Cells[0,0]='')and(StringGrid1.Cells[2,1]='x')and(StringGrid1.Cells[4,2]='x')and(StringGrid1.Cells[5,3]='x')and(ilosc=3) then Label12.Caption:='Akord B-Dur' ;
//Akord h-mol
if(StringGrid1.Cells[0,0]='')and(StringGrid1.Cells[3,1]='x')and(StringGrid1.Cells[4,2]='x')and(StringGrid1.Cells[6,3]='x')and(ilosc=3) then Label12.Caption:='Akord h-mol' ;

//Akord C-Dur
if(StringGrid1.Cells[1,1]='x')and(StringGrid1.Cells[2,2]='x')and(ilosc=2) then Label12.Caption:='Akord C-Dur' ;
//Akord c-mol
if(StringGrid1.Cells[0,1]='x')and(StringGrid1.Cells[2,2]='x')and(ilosc=2) then Label12.Caption:='Akord c-mol' ;
//Akord cis-mol
if(StringGrid1.Cells[0,0]='x')and(StringGrid1.Cells[1,1]='x')and(StringGrid1.Cells[3,2]='x')and(ilosc=3) then Label12.Caption:='Akord cis-mol' ;

//Akord D-dur
if(StringGrid1.Cells[1,0]='x')and(StringGrid1.Cells[3,1]='x')and(StringGrid1.Cells[4,2]='x')and(ilosc=3) then Label12.Caption:='Akord D-dur' ;
//Akord d-mol
if(StringGrid1.Cells[1,0]='x')and(StringGrid1.Cells[2,1]='x')and(StringGrid1.Cells[4,2]='x')and(ilosc=3) then Label12.Caption:='Akord d-mol' ;

//Akord E-Dur
if(StringGrid1.Cells[3,0]='x')and(StringGrid1.Cells[5,1]='x')and(StringGrid1.Cells[6,2]='x')and(ilosc=3) then Label12.Caption:='Akord E-dur' ;
//Akord Es-Dur
if(StringGrid1.Cells[2,0]='x')and(StringGrid1.Cells[4,1]='x')and(StringGrid1.Cells[5,2]='x')and(ilosc=3) then Label12.Caption:='Akord Es-dur' ;
//Akord e-mol
if(StringGrid1.Cells[3,0]='x')and(StringGrid1.Cells[4,1]='x')and(StringGrid1.Cells[6,2]='x')and(ilosc=3) then Label12.Caption:='Akord e-mol' ;

//Akord F-Dur
if(StringGrid1.Cells[4,0]='x')and(StringGrid1.Cells[6,1]='x')and(StringGrid1.Cells[7,2]='x')and(ilosc=3) then Label12.Caption:='Akord F-dur' ;
//Akord F-mol
if(StringGrid1.Cells[4,0]='x')and(StringGrid1.Cells[5,1]='x')and(StringGrid1.Cells[7,2]='x')and(ilosc=3) then Label12.Caption:='Akord g-mol' ;
//Akord Fis-mol
if(StringGrid1.Cells[5,0]='x')and(StringGrid1.Cells[6,1]='x')and(StringGrid1.Cells[8,2]='x')and(ilosc=3) then Label12.Caption:='Akord Fis-mol' ;

//Akord G-dur
if(StringGrid1.Cells[1,2]='x')and(StringGrid1.Cells[2,3]='x')and(ilosc=2) then Label12.Caption:='Akord G-dur' ;
//Akord g-mol
if(StringGrid1.Cells[0,2]='x')and(StringGrid1.Cells[2,3]='x')and(ilosc=2) then Label12.Caption:='Akord g-mol' ;

end;

end.


0

Wybacz kolego ale nic nie rozumiem z Twojego kodu i źle się go czyta stąd nawet nie będę próbował;

Tutaj masz poprawioną procedurę malującą pojedynczą komórkę (wraz z uwzględnieniem komórek tytułowych):

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
                                     State: TGridDrawState);
begin
  with TStringGrid(Sender).Canvas do
    begin
      { SPRAWDZENIE INDEKSU KOMÓRKI }
      if (ACol = 0) or (ARow = 0) then
        begin
          Pen.Color := clBtnFace;
          Brush.Color := clBtnFace;
        end
      else
        { SPRAWDZENIE STANU ZAZNACZENIA KOMÓRKI }
        if gdSelected in State then
          begin
            Pen.Color := clRed;
            Brush.Color := clRed;
          end
        else
          begin
            Pen.Color := clWhite;
            Brush.Color := clWhite;
          end;

      { WYPEŁNIENIE KOMÓRKI USTALONYM KOLOREM }
      Rectangle(Rect);

      { USUNIĘCIE WIEJSKIEJ RAMKI Z KOMÓRKI }
      if gdFocused in State then
        begin
          Pen.Color := Pen.Color xor $FFFFFF;
          DrawFocusRect(Rect);
        end;
    end;
end;

Efekt poniżej (samo tło, bez tekstu):

StringGridDrawing.png

Dodatkowo zamieszczam w załączniku programik wykorzystujący ten kod; Popatrz, popróbuj, znajdź błąd u siebie i popraw go;

0

Spróbuje teraz przeanalizować jak będą jakieś problemy napisze z góry dzięki :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1