Einzelnen Beitrag anzeigen

Benutzerbild von sakura
sakura

Registriert seit: 10. Jun 2002
Ort: München
11.412 Beiträge
 
Delphi 11 Alexandria
 
#7

Re: Stringgrid selection Problem !

  Alt 2. Jul 2004, 10:30
Okay, verstanden und ... gelöst.

Das ist schon etwas einfacher. Du mußt zwei Ereignisse abfangen. Einmal das OnSelectCell, um dem StringGrid mitzuteilen, daß sich die Auswahl grundlegend geändert hat, und einmal das OnDrawCell Ereigniss, um die Auswahl darzustellen.

Außerdem habe ich noch eine Methode geschrieben, welche ermittelt, ob sich eine Zelle (Spalte, Zeile) in der erweiterten Auswahl befindet.

Hier die drei Methoden und es geht.
Delphi-Quellcode:
type
  TForm1 = class(TForm)
    [...]
  private
    { Private declarations }
    function CellInRange(aGrid: TCustomDrawGrid; aCol, aRow: Integer): Boolean;
    [...]
  end;

// testen, ob sich eine bestimmte Zelle in der erweiterten Auswahl aufhält
function TForm1.CellInRange(aGrid: TCustomDrawGrid; aCol, aRow: Integer
    ): Boolean;
begin
  Result := False;
  if aRow < aGrid.Selection.Top then
    Exit;
  if aRow = aGrid.Selection.Top then
    if aCol < aGrid.Selection.Left then
      Exit;
  if aRow > aGrid.Selection.Bottom then
    Exit;
  if aRow = aGrid.Selection.Bottom then
    if aCol > aGrid.Selection.Right then
      Exit;
  Result := True;
end;

// die Ereignisroutinen
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if Sender = nil then
    Exit;
  if not (Sender is TStringGrid) then
    Exit;
  with Sender as TStringGrid do
  begin
    // skip fixed columns
    if (ACol < FixedCols) or (ARow < FixedRows) then
      Exit;
    // select color
    if CellInRange(Sender as TStringGrid, ACol, ARow) then
    begin
      // selected cell
      Canvas.Brush.Color := clHighlight;
      Canvas.Font.Color := clHighlightText;
    end else begin
      // cell not selected
      Canvas.Brush.Color := clWindow;
      Canvas.Font.Color := clWindowText;
    end;
    // draw cell
    Canvas.TextRect(Rect, Rect.Left, Rect.Top, Cells[ACol, ARow]);
  end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  if Sender = nil then
    Exit;
  if not (Sender is TStringGrid) then
    Exit;
  with Sender as TStringGrid do
  begin
    Update;
    Invalidate;
  end;
end;
......
Daniel W.
Ich bin nicht zurück, ich tue nur so
  Mit Zitat antworten Zitat