Wybiórczy select komórek z klawiszem Ctrl i mouse

0

Witam.

Jak zrobić coś takiego w TStringGrid?
Potrafię zrobić select ale z klawiatury i to w dodatku nie wybiórczo tylko ciągle.
A na to nie ma pomysłu nawet jak zacząć.

Z góry dziękuje za sugestie.

Bez tytułu.png

2

Musisz obsłużyć OnDrawCell i zrobić sobie jakąś listę zaznaczonych komórek. Musiałem się oderwać więc trzymaj minimalny przykład:

uses
  Generics.Collections;

procedure TForm5.FormCreate(Sender: TObject);
begin
  SelectedItems := TList<Integer>.Create();
end;

procedure TForm5.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  selectedCell : Integer;

begin
  with StringGrid1 do
  begin
    Canvas.Font.Color := clBlack;
    Canvas.Brush.Color := clWhite;
    if ((ACol < FixedCols) or (ARow < FixedRows)) then
      Canvas.Brush.Color := clBtnFace
    else
      if SelectedItems.BinarySearch(ARow * ColCount + ACol, selectedCell) then
        Canvas.Brush.Color := clAqua;
    Canvas.FillRect(Rect);
    Canvas.TextOut(Rect.Left, Rect.Top, Cells[ACol, ARow]);
  end;
end;

procedure TForm5.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  HitCol, HitRow : Integer;
  selectedCell : Integer;

begin
  if ssCtrl in Shift then
  with StringGrid1 do
  begin
    MouseToCell(X, Y, HitCol, HitRow);
    if ((HitCol > FixedCols - 1) and (HitRow > FixedRows - 1)) then
     if SelectedItems.BinarySearch(HitRow * ColCount + HitCol, selectedCell) then
       SelectedItems.Delete(selectedCell)
     else
       SelectedItems.Add(HitRow * ColCount + HitCol);

    Invalidate;
    SelectedItems.Sort();
  end;
end;

Jeszcze mała uwaga. W tej wersji nie zadziała to z RangeSelect. Trzeba wyłączyć tą opcję (jak w przykładzie z załącznika albo dorobić funkcjonalność).

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