user1137313
user1137313

Reputation: 2410

Delphi StringGrid HighLight row and column

I have a StringGrid and I color on the fixed row and Column the position of the clicked cell. So it looks like this so far:

enter image description here

In order to do this I used this code:

procedure TForm2.sgDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  md: integer;
begin
  with sg do
    begin
   Canvas.Brush.Color:= clwhite;
   if ((sg.Row = ARow)and(ACol=0)) or ((sg.Col = ACol)and(ARow=0)) then
      Canvas.Brush.Color:= $00FFDE9B; //your highlighted color
   Canvas.FillRect(Rect);
   Canvas.TextOut(0, Rect.top + 4, cells[ACol, ARow]);
  end;
  if gdSelected in State then
    sg.Canvas.DrawFocusRect(Rect);
end;

plus of course the invalidate in OnMouseDown.

procedure TForm2.sgMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  sg.invalidate;
end;

Now, I want to color all the selected rows in the top side. So like in the image, there are 4 cells selected but only one cell is colored blue (Col 4) in the fixed area. I want all the corresponding fixed cells to be blue. (in this case: Col 4, Col 5, Col 6, Col 7)

Any ideas?

EDIT

The idea is to show the selection when selecting with the mouse, not with SHIFT+Click

Upvotes: 0

Views: 4253

Answers (1)

Luben Popov
Luben Popov

Reputation: 31

Little improvement

procedure TForm3.sgDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  md: integer;
begin
  with sg do
    begin

   Canvas.Brush.Color:= clwhite;

   if ( (Arow >=selection.Top) and (Arow<=selection.Bottom) and(ACol=0)) or
      ( (ACol>=selection.Left) and (Acol<=selection.Right) and(ARow=0)) then
         Canvas.Brush.Color:= $00FFDE9B; //your highlighted color
   Canvas.FillRect(Rect);
   Canvas.TextOut(0, Rect.top + 4, cells[ACol, ARow]);
  end;
  if gdSelected in State then
    sg.Canvas.DrawFocusRect(Rect);
end;

procedure TForm3.sgMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
      if (ssLeft in shift) then sg.Invalidate;
end;

This will highlight the rows too whitout helper function.

Upvotes: 1

Related Questions