Afficher un texte incliné à 45 degrés avec GDI (CreateFontIndirect)
Par Delphi Source - March 22, 2026 · Vues: 14 · Catégories: Snippets · Tags: #GDI+ #Texte

Afficher un texte incliné à 45 degrés avec GDI (CreateFontIndirect)

Delphi permet de manipuler directement les polices Windows via l’API GDI.

Voici un exemple simple qui affiche un texte incliné à 45 degrés, parfaitement centré dans la fenêtre.


procedure TForm1.FormPaint(Sender: TObject);
var
  LFnt: TLogFont;
  Fnt: TFont;
  ATitle: String;
  CW, CH: Integer;
  CoSinus_45: single;
begin
  with Form1.Canvas do
  begin
    MoveTo(ClientWidth div 2, 0);
    LineTo(ClientWidth div 2, ClientHeight);
    MoveTo(0, ClientHeight div 2);
    LineTo(ClientWidth, ClientHeight div 2);
    Font.Size := 30;

    Fnt := TFont.Create;
    try
      Fnt.Assign(Font);
      GetObject(Fnt.Handle, SizeOf(LFnt), @LFnt);

      LFnt.lfEscapement := 45 * 10;
      LFnt.lfOrientation := LFnt.lfEscapement;

      SetBkMode(Handle, TRANSPARENT);
      Fnt.Handle := CreateFontIndirect(LFnt);
      Font.Assign(Fnt);
    finally
      Fnt.Free;
    end;

    ATitle := 'RAD Studio 12.2';
    CW := TextWidth(ATitle);
    CH := TextHeight(ATitle);

    CoSinus_45 := Sqrt(2) / 2;
    TextOut((ClientWidth - Trunc((CW + CH) * CoSinus_45)) div 2,
      (ClientHeight - Trunc((CH - CW) * CoSinus_45)) div 2, ATitle);
  end;
end;

Le principe

  • Créer une police via TLogFont,

  • Définir l’angle d’inclinaison (lfEscapement),

  • Appliquer la police au Canvas,

  • Calculer la position du texte en tenant compte de la rotation.


Un petit clic (J'aime) qui fait plaisir !