Domanda

Credo che la mia domanda è abbastanza chiaro, ma mi spiego di più. Semplicemente, quando stiamo usando AntiAlias ??sulla procedura renderText, il valore ottenuto all'interno della funzione TextWidth non è corretto. Che cosa posso fare per ottenere la larghezza del testo giusto?

È stato utile?

Soluzione

È possibile cercare l'algoritmo nel proprio codice. Essi devono anche calcolare esso. In ogni caso questo è come lo faccio.

function TGR32Canvas.TextWidth(const Text: string): Integer;
var
  TempFont: TFont;
  TempWidth: Integer;
begin
  if Text <> '' then
  begin
    TempFont := TFont.Create;
    try
      TempFont.Assign(Font);
      TempFont.Size := Font.Size shl AA_MODE;
      TempWidth := GetTextWidth(Text, TempFont);
    finally
      TempFont.Free;
    end;
  end
  else
    TempWidth := 0;

  TempWidth := (TempWidth shr AA_MODE + 1) shl AA_MODE;
  Result := TempWidth shr AA_MODE;
end;

La funzione GetTextWidth è semplice. È possibile farlo in modo diverso.

function GetTextWidth(const Text: string; const Font: TFont): Integer;
var
  Canvas: TCanvas;
begin
  Canvas := TCanvas.Create;
  try
    Canvas.Handle := GetDC(0);
    try
      Canvas.Font.Assign(Font);
      Result := Canvas.TextWidth(Text);
    finally
      ReleaseDC(0, Canvas.Handle);
    end;
  finally
    Canvas.Free;
  end;
end;

Altri suggerimenti

È possibile anche utilizzare la funzione API di Windows GetTextExtentPoint32 Fare qualche google per trovare ad esempio su Delphi

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top