Einzelnen Beitrag anzeigen

madas

Registriert seit: 9. Aug 2007
207 Beiträge
 
#11

AW: TVirtualStringTree AutoFitColumns erste Spalte wird nicht angepasst

  Alt 4. Apr 2017, 10:56
Dann mache die Berechnung doch selber.
Wir machen das bei uns so (ist ein Frame mit einem VST drauf, der Tree als Name hat):

Delphi-Quellcode:
function TFrameVST.GetMaxHeaderWidth(const Column: TColumnIndex): Integer;
var
  col: TVirtualTreeColumn;
  Size: TSize;
begin
  col := Tree.Header.Columns[Column];
  Result := 2 * col.Margin;
  if (Length(col.Text) > 0) then
  begin
    Tree.Canvas.Font := Tree.Header.Font;
    GetTextExtentPoint32W(Tree.Canvas.Handle, PWideChar(col.Text), Length(col.Text), Size);
    Inc(Result, Size.cx + 4);
  end;
  if (hoShowImages in Tree.Header.Options)and(Assigned(Tree.Header.Images)) then
    Inc(Result,(Tree.Header.Images.Width + col.Spacing));
  if (hoShowSortGlyphs in Tree.Header.Options)and(Tree.Header.SortColumn = Column) then
  begin
    Inc(Result, 20); // Platz für Sortimage
    if (col.CaptionAlignment = taCenter) then
      Inc(Result, 14); // mehr Platz für Sortimage berücksichtigen
  end;
  if Result < col.MinWidth then
    Result := col.MinWidth;
  if Result > col.MaxWidth then
    Result := col.MaxWidth;
end;

procedure TFrameVST.AutoFitColumn(Column: TColumnIndex);
var
  cWidth, hWidth: Integer;
begin
  if [coResizable, coVisible] * Tree.Header.Columns[Column].Options = [coResizable, coVisible] then
  begin
    if (hoVisible in Tree.Header.Options) then
    begin
      hWidth := GetMaxHeaderWidth(Column);
      cWidth := Tree.GetMaxColumnWidth(Column);
      if cWidth < hWidth then
        cWidth := hWidth;
      if cWidth > Tree.Header.Columns[Column].MaxWidth then
        cWidth := Tree.Header.Columns[Column].MaxWidth;
      Tree.Header.Columns[Column].Width := cWidth;
    end;
  end;
end;

procedure TFrameVST.AutoFitColumns;
var
  Column: TColumnIndex;
  merkCursor: TCursor;
begin
  merkCursor := Screen.Cursor;
  Tree.BeginUpdate;
  try
    Screen.Cursor := crHourGlass;
    for Column := 0 to Tree.Header.Columns.Count-1 do
      AutoFitColumn(Column);
  finally
    Screen.Cursor := merkCursor;
    Tree.EndUpdate;
  end;
end;

FmerkOnBeforeCellPaint: TVTBeforeCellPaintEvent;

procedure TFrameVST.TreeBeforeGetMaxColumnWidth(Sender: TVTHeader; Column: TColumnIndex; var UseSmartColumnWidth: Boolean);
begin
  if Assigned(Tree.OnBeforeCellPaint) then
  begin
    FmerkOnBeforeCellPaint := Tree.OnBeforeCellPaint;
    Tree.OnBeforeCellPaint := nil;
  end;
end;

procedure TFrameVST.TreeAfterGetMaxColumnWidth(Sender: TVTHeader; Column: TColumnIndex; var MaxWidth: Integer);
begin
  if Assigned(FmerkOnBeforeCellPaint) then
  begin
    Tree.OnBeforeCellPaint := FmerkOnBeforeCellPaint;
    FmerkOnBeforeCellPaint := nil;
  end;
end;
  Mit Zitat antworten Zitat