The property VisibleColCount does not apply to columns which are hidden by turning their Visible property off. According to Delphi documentation (
https://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.Grids.TCustomGrid.VisibleColCount), it is the number of fully visible, scrollable columns. In Lazarus basically it is the same, but gets more complicated because in contrast to Delphi we have the goSmoothScroll option. And the consequence is that when the grid is scrolled to the right the left-most columns may be partially visible - but Lazarus counts this as "fully visible" ...
Play with attached demo project to see how it works. The rules are (according to my experiments):
- The fixed columns and fully out-of-view columns are not counted in VisibleColCount.
- Partially visible columns at the right are not counted.
- Partially visible columns at the left ARE counted.
- Columns having Visible=false ARE counted
Really quite confusing, I agree...
Looking at it from the viewpoint of the GCache.VisibleGrid appears more logical to me: It includes all partially and fully visible columns, except for the fixed column(s) (and "hidden" columns).
The number of visible columns then would be
type
TMyStringGrid = class(TStringGrid);
function GetPartiallyVisibleCols(AGrid: TStringGrid): Integer;
var
xLeft: Integer;
xRight: Integer;
c, c1, c2: Integer;
begin
if AGrid.FixedCols > 0 then
xLeft := AGrid.CellRect(AGrid.FixedCols-1, 0).Right
else
xLeft :=0;
xRight := AGrid.ClientRect.Right;
Result := 0;
with TMyStringGrid(AGrid) do
begin
c1 := GCache.VisibleGrid.Left;
c2 := GCache.VisibleGrid.Right;
end;
for c := c1 to c2 do begin
if AGrid.CellRect(c, 0).Left < xLeft then
inc(Result)
else
if AGrid.CellRect(c, 0).Right > xRight then
inc(Result);
end;
end;
function GetFullyOrPartiallyVisibleCols(AGrid: TStringGrid): Integer;
begin
with TMyStringGrid(AGrid) do
Result := GCache.VisibleGrid.Right - GCache.VisibleGrid.Left + 1;
end;
function GetFullyVisibleCols(AGrid: TStringGrid): Integer;
begin
Result := GetFullyOrPartiallyVisibleCols(AGrid) - GetPartiallyVisibleCols(AGrid);
end;