Ребята, мучаюсь уже давным-давно с нормальным изменением размера StrigGrid после подгонки высоты строк по событию OnDrawCell... Неправильно пересчитывается сумма высот строк с первого раза... Если выполнять FillarraySG и SizeArraySG последовательно, то подгонка выполняется неверно и размер StringGrid меньше, чем общая высота всех строк... Если подгонку (SizeArraySG) назначить на кнопку - сразу считается верно. Такое ощущение, что переменные в RowHeights[ARow] присваиваются не совсем сразу... Никак не пойму...
Вот код:
Создание таблицы:
Код:
OiljobsSG := TArraySG.Create(Self);
OiljobsSG.Name:='OiljobsSG';
OiljobsSG.Parent := ScrollBox1;
OiljobsSG.Options:=OiljobsSG.Options+[goRowSelect]-[goRangeSelect];
OiljobsSG.OnClick:=OiljobsSGClick;
OiljobsSG.OnDrawCell:=SGDrawCell;
OiljobsSG.ScrollBars:=ssHorizontal;
Заполнение таблицы из массива:
Код:
procedure FillArraySG(SG: TArraySG; CellsData: TArrOfArrOfStr);
var
i,j:integer;
begin
with SG do
begin
if Row=0 then Row:=1;
Width:=5000;
Height:=5000;
ColCount:=Length(CellsData);
RowCount:=Length(CellsData[0]);
for i:=0 to ColCount-1 do
begin
for j:=0 to RowCount-1 do
begin
Cells[i,j]:= CellsData[i,j];
end;
end;
end;
end;
Код:
procedure TArraySG.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
var
Format: Word;
C: array[0..255] of Char;
h, w:Cardinal;
rectoffset:Cardinal;
begin
rectoffset:=2;
Self.Canvas.FillRect(ARect);
ARect.Left:=ARect.Left+rectoffset;
ARect.Top:=ARect.Top+rectoffset;
ARect.Right:=ARect.Right-rectoffset;
ARect.Bottom:=ARect.Bottom-rectoffset;
StrPCopy(C, Self.Cells[ACol, ARow]);
if ARow=0 then
begin
Format:=DT_CENTER;
Self.canvas.font.style:=Self.canvas.font.style+[fsbold];
end
else
begin
Format:=DT_WORDBREAK;
Self.canvas.font.style:=Self.canvas.font.style-[fsbold];
end;
h:=WinProcs.DrawText(Self.Canvas.Handle, C, StrLen(C), ARect, Format)+2*rectoffset;
// w:=ARect.Right-ARect.Left+2*rectoffset;
if Self.RowHeights[ARow]<h then Self.RowHeights[ARow]:=h;
// if Self.ColWidths[ACol]<w then Self.ColWidths[ACol]:=w;
Self.Canvas.Brush.Color := clBlack;
ARect.Left:=ARect.Left-rectoffset-1;
ARect.Top:=ARect.Top-rectoffset-1;
ARect.Right:=ARect.Right+rectoffset+1;
ARect.Bottom:=ARect.Bottom+rectoffset+1;
Self.Canvas.FrameRect(ARect);
end;
Код:
procedure SizeArraySG(SG: TArraySG);
var
i, h, HorScrollHeight:Cardinal;
Rect:TRect;
begin
i:=0;
h:=0;
if SG.RowCount>1 then
begin
while i<SG.RowCount do
begin
h:=h+SG.RowHeights[i]+SG.GridLineWidth;
Inc(i)
end;
//åñëè ñóùåñòâóåò ïîëîñà ïðîêðóòêè íà ãðèäå - òî ïðèáàâëÿåì å¸ øèðèíó (òî÷íåå, âûñîòó)
if (GetWindowlong(SG.Handle, GWL_STYLE) and WS_HSCROLL) <> 0 then
HorScrollHeight := GetSystemMetrics(SM_CYHSCROLL)
else HorScrollHeight:=0;
SG.Height:=h+3+HorScrollHeight;
end
else
begin
SG.Height:=h;
end;
end;