Search code examples
delphidelphi-7

The arrow pointer in a TStringGrid


Is it possible to add that arrow pointer thing to a String Grind in Delphi 7? You know what I mean, that arrow pointer that you can see at the left in a DBGrid.


Solution

  • Yes, but not automatically. You would need to display a triangle manually. You can override OnDrawCell for your grid. It seems you need to set the FixedCols to 0 since it doesn't appear to redraw the fixed cells again when the row selection changes.

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      aCanvas: TCanvas;
      oldColor: TColor;
      triangle: array [0..2] of TPoint;
    const
      spacing = 4;
    begin
      if (ACol = 0) and (aRow = StringGrid1.Row) then
      begin
        aCanvas := (Sender as TStringGrid).Canvas;  // To avoid with statement
        oldColor := aCanvas.Brush.Color;
        // Shape the triangle
        triangle[0] := TPoint.Create(Rect.Left + spacing, Rect.Top + spacing);
        triangle[1] := TPoint.Create(Rect.Left + spacing, Rect.Top + Rect.Height - spacing);
        triangle[2] := TPoint.Create(Rect.Left + Rect.Width - spacing, Rect.Top + Rect.Height div 2);
    
        // Draw the triangle
        aCanvas.Pen.Color := clBlack;
        aCanvas.Brush.Color := clBlack;
        aCanvas.Polygon(triangle);
        aCanvas.FloodFill(Rect.Left + Rect.Width div 2, Rect.Top + Rect.Height div 2, clBlack, fsSurface);
        aCanvas.Brush.Color := oldColor;
      end;
    end;
    

    This draws a triangle in the box. You should get the general idea.