Graphics - Working with TCanvas/fr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Graphics - Working with TCanvas/fr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Utilisation de la police d'IHM par défaut | |
| 2 | + | ||
| 3 | + | Cela peut être fait avec ce simple code | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT)); | |
| 7 | + | ``` | |
| 8 | + | ||
| 9 | + | ## Dessin d'un texte Drawing limité en largeur | |
| 10 | + | ||
| 11 | + | Employez la routine DrawText, d'abord avec DT_CALCRECT puis sans cela. | |
| 12 | + | ||
| 13 | + | ``` pascal | |
| 14 | + | // First calculate the text size then draw it | |
| 15 | + | TextBox := Rect(0, currentPos.Y, Width, High(Integer)); | |
| 16 | + | DrawText(ACanvas.Handle, PChar(Text), Length(Text), | |
| 17 | + | TextBox, DT_WORDBREAK or DT_INTERNAL or DT_CALCRECT); | |
| 18 | + | ||
| 19 | + | DrawText(ACanvas.Handle, PChar(Text), Length(Text), | |
| 20 | + | TextBox, DT_WORDBREAK or DT_INTERNAL); | |
| 21 | + | ``` | |
| 22 | + | ||
| 23 | + | ## Dessin de texte avec des contours vifs (sans antialiasing) | |
| 24 | + | ||
| 25 | + | Certains ensemble de widgets supportent cela à travers : | |
| 26 | + | ||
| 27 | + | ``` pascal | |
| 28 | + | Canvas.Font.Quality := fqNonAntialiased; | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | 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: | |
| 32 | + | ||
| 33 | + | ``` pascal | |
| 34 | + | procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string); | |
| 35 | + | var | |
| 36 | + | w,h: integer; | |
| 37 | + | IntfImg: TLazIntfImage; | |
| 38 | + | Img: TBitmap; | |
| 39 | + | dy: Integer; | |
| 40 | + | dx: Integer; | |
| 41 | + | col: TFPColor; | |
| 42 | + | FontColor: TColor; | |
| 43 | + | c: TColor; | |
| 44 | + | begin | |
| 45 | + | w:=0; | |
| 46 | + | h:=0; | |
| 47 | + | Canvas.GetTextSize(TheText,w,h); | |
| 48 | + | if (w<=0) or (h<=0) then exit; | |
| 49 | + | Img:=TBitmap.Create; | |
| 50 | + | IntfImg:=nil; | |
| 51 | + | try | |
| 52 | + | // paint text to a bitmap | |
| 53 | + | Img.Masked:=true; | |
| 54 | + | Img.SetSize(w,h); | |
| 55 | + | Img.Canvas.Brush.Style:=bsSolid; | |
| 56 | + | Img.Canvas.Brush.Color:=clWhite; | |
| 57 | + | Img.Canvas.FillRect(0,0,w,h); | |
| 58 | + | Img.Canvas.Font:=Canvas.Font; | |
| 59 | + | Img.Canvas.TextOut(0,0,TheText); | |
| 60 | + | // get memory image | |
| 61 | + | IntfImg:=Img.CreateIntfImage; | |
| 62 | + | // replace gray pixels | |
| 63 | + | FontColor:=ColorToRGB(Canvas.Font.Color); | |
| 64 | + | for dy:=0 to h-1 do begin | |
| 65 | + | for dx:=0 to w-1 do begin | |
| 66 | + | col:=IntfImg.Colors[dx,dy]; | |
| 67 | + | c:=FPColorToTColor(col); | |
| 68 | + | if c<>FontColor then | |
| 69 | + | IntfImg.Colors[dx,dy]:=colTransparent; | |
| 70 | + | end; | |
| 71 | + | end; | |
| 72 | + | // create bitmap | |
| 73 | + | Img.LoadFromIntfImage(IntfImg); | |
| 74 | + | // paint | |
| 75 | + | Canvas.Draw(x,y,Img); | |
| 76 | + | finally | |
| 77 | + | IntfImg.Free; | |
| 78 | + | Img.Free; | |
| 79 | + | end; | |
| 80 | + | end; | |
| 81 | + | ``` | |
| 82 | + | ||
| 83 | + | --- | |
| 84 | + | *From the Free Pascal wiki, page "Graphics - Working with TCanvas/fr", as it stood on 2020-02-16 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
