Difference between revisions of "Graphics - Working with TCanvas/fr"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
Line 4: Line 4:
  
 
Cela peut être fait avec ce simple code
 
Cela peut être fait avec ce simple code
<syntaxhighlight>SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));</syntaxhighlight>
+
<syntaxhighlight lang=pascal>SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));</syntaxhighlight>
  
 
==Dessin d'un texte Drawing limité en largeur==
 
==Dessin d'un texte Drawing limité en largeur==
 
Employez la routine DrawText, d'abord avec DT_CALCRECT puis sans cela.
 
Employez la routine DrawText, d'abord avec DT_CALCRECT puis sans cela.
  
<syntaxhighlight>// First calculate the text size then draw it
+
<syntaxhighlight lang=pascal>// First calculate the text size then draw it
 
TextBox := Rect(0, currentPos.Y, Width, High(Integer));
 
TextBox := Rect(0, currentPos.Y, Width, High(Integer));
 
DrawText(ACanvas.Handle, PChar(Text), Length(Text),
 
DrawText(ACanvas.Handle, PChar(Text), Length(Text),
Line 20: Line 20:
 
Certains ensemble de widgets supportent cela à travers :
 
Certains ensemble de widgets supportent cela à travers :
  
<syntaxhighlight>Canvas.Font.Quality := fqNonAntialiased;</syntaxhighlight>
+
<syntaxhighlight lang=pascal>Canvas.Font.Quality := fqNonAntialiased;</syntaxhighlight>
  
 
Certains ensembles de widgets tel GTK2 ne supportent pas cela et peignent toujours avec l'antialiasing, Voici une simple procédure pour dessiner du texte sans sous GTK2. Il ne prend pas en compte tous les cas, mais il donne une idée:
 
Certains ensembles de widgets tel GTK2 ne supportent pas cela et peignent toujours avec l'antialiasing, Voici une simple procédure pour dessiner du texte sans sous GTK2. Il ne prend pas en compte tous les cas, mais il donne une idée:
  
<syntaxhighlight>procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string);
+
<syntaxhighlight lang=pascal>procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string);
 
var
 
var
 
   w,h: integer;
 
   w,h: integer;

Latest revision as of 13:12, 16 February 2020

English (en) français (fr) italiano (it) русский (ru)

Utilisation de la police d'IHM par défaut

Cela peut être fait avec ce simple code

SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));

Dessin d'un texte Drawing limité en largeur

Employez la routine DrawText, d'abord avec DT_CALCRECT puis sans cela.

// First calculate the text size then draw it
TextBox := Rect(0, currentPos.Y, Width, High(Integer));
DrawText(ACanvas.Handle, PChar(Text), Length(Text),
  TextBox, DT_WORDBREAK or DT_INTERNAL or DT_CALCRECT);

DrawText(ACanvas.Handle, PChar(Text), Length(Text),
  TextBox, DT_WORDBREAK or DT_INTERNAL);

Dessin de texte avec des contours vifs (sans antialiasing)

Certains ensemble de widgets supportent cela à travers :

Canvas.Font.Quality := fqNonAntialiased;

Certains ensembles de widgets tel GTK2 ne supportent pas cela et peignent toujours avec l'antialiasing, Voici une simple procédure pour dessiner du texte sans sous GTK2. Il ne prend pas en compte tous les cas, mais il donne une idée:

procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string);
var
  w,h: integer;
  IntfImg: TLazIntfImage;
  Img: TBitmap;
  dy: Integer;
  dx: Integer;
  col: TFPColor;
  FontColor: TColor;
  c: TColor;
begin
  w:=0;
  h:=0;
  Canvas.GetTextSize(TheText,w,h);
  if (w<=0) or (h<=0) then exit;
  Img:=TBitmap.Create;
  IntfImg:=nil;
  try
    // paint text to a bitmap
    Img.Masked:=true;
    Img.SetSize(w,h);
    Img.Canvas.Brush.Style:=bsSolid;
    Img.Canvas.Brush.Color:=clWhite;
    Img.Canvas.FillRect(0,0,w,h);
    Img.Canvas.Font:=Canvas.Font;
    Img.Canvas.TextOut(0,0,TheText);
    // get memory image
    IntfImg:=Img.CreateIntfImage;
    // replace gray pixels
    FontColor:=ColorToRGB(Canvas.Font.Color);
    for dy:=0 to h-1 do begin
      for dx:=0 to w-1 do begin
        col:=IntfImg.Colors[dx,dy];
        c:=FPColorToTColor(col);
        if c<>FontColor then
          IntfImg.Colors[dx,dy]:=colTransparent;
      end;
    end;
    // create bitmap
    Img.LoadFromIntfImage(IntfImg);
    // paint
    Canvas.Draw(x,y,Img);
  finally
    IntfImg.Free;
    Img.Free;
  end;
end;