So ich habe jetzt eine Lösung, die besser funktioniert, zwar bleibt beim löschen noch eine graue Zelle übrig, aber damit habe ich jetzt nicht unbedingt ein Problem...
Verzichtet man ganz auf das DrawCell Ereignis mit eigenen Farben für die Spalten, dann klappt das Löschen auch komplett.
Code: Alles auswählen
var
Reihe: Integer = 0;
procedure TForm1.FormCreate(Sender: TObject);
begin
with StringGrid1 do
begin
DefaultDrawing:= False;
Options:= Options + [goSelectionActive,goRowSelect];
Cells[1,1]:= 'Hallo';
FixedCols := 0;
FixedRows := 0;
end;
end;
procedure TForm1.Button1Click(Sender: TObject); // Ganze Reihen nacheinander markieren
begin
StringGrid1.Options:= StringGrid1.Options + [goSelectionActive,goRowSelect];
if Reihe > StringGrid1.RowCount-1 then Reihe:= 0;
StringGrid1.Selection := TGridRect(Rect(0,Reihe,StringGrid1.ColCount-1,Reihe));
Inc(Reihe);
end;
procedure TForm1.Button2Click(Sender: TObject); //Auswahl löschen
begin
StringGrid1.Options:= StringGrid1.Options - [goSelectionActive,goRowSelect];
//StringGrid1.Selection := TGridRect(Rect(-1,-1,-1,-1)); nicht mehr nötig
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
var
ZeilenText: string;
MyTxtStyle: TTextStyle;
begin
with StringGrid1 do
begin
if (gdSelected in aState) or (gdFocused in aState) then //Wenn die Zelle ausgewählt ist
begin
Canvas.Font.Color := clblack;
Canvas.Brush.Color := clGray; //Farbe wenn Zelle ausgewählt ist
end
else if ACol mod 2 = 0 then //Jede zweite Spalte
begin
Canvas.Font.Color := clWhite;
Canvas.Brush.Color := clBlue; //Farbe auswählen zum Zeichnen
end
else //sonstige Zellen
begin
Canvas.Font.Color := clWindowText;
Canvas.Brush.Color := clWindow; //Farbe auswählen zum Zeichnen
end;
Canvas.FillRect(ARect); //Zelle Zeichnen
ZeilenText := Cells[ACol, ARow]; // Text der Zelle holen
//Text zentriert zeichnen
MyTxtStyle.Alignment := taCenter;
MyTxtStyle.Layout := tlCenter;
Canvas.TextRect(aRect,aRect.Left, aRect.Top, ZeilenText,MyTxtStyle);
end;
end;