Graphics - Working with TCanvas/it: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ### Usare il font di default della GUI | |
| 2 | 2 | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 3 | + | Il font di default si ottiene con il seguente codice: | |
| 4 | + | ``` | |
| 6 | 5 | SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT)); | |
| 7 | 6 | ``` | |
| 8 | 7 | ||
| 9 | 8 | ## Disegnare un testo delimitato in larghezza | |
| 10 | 9 | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 10 | + | Usare la routine DrawText due volte, prima con l'opzione DT_CALCRECT e poi senza. | |
| 11 | + | Esempio (Lazarus v1.6 con windows vista): | |
| 12 | + | ``` | |
| 17 | 13 | .. | |
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 14 | + | uses ... windows ... | |
| 15 | + | .. | |
| 16 | + | var Image1: TImage; | |
| 17 | + | TextBox: TRect; | |
| 18 | + | Text: String; | |
| 19 | + | ... | |
| 20 | + | Text:= ...; | |
| 21 | + | // Prima calcoliamo la dimensione del testo, poi lo disegniamo: | |
| 22 | + | TextBox:= Rect(10, 10, Image1.Width - 10, High(Integer)); | |
| 23 | + | // questa istruzione ricalcola TextBox.Bottom: | |
| 24 | + | DrawText(Image1.canvas.Handle, PChar(Text), Length(Text), | |
| 25 | + | TextBox, DT_WORDBREAK or DT_INTERNAL or DT_CALCRECT); | |
| 26 | + | // questa istruzione scrive il testo | |
| 27 | + | DrawText(Image1.Canvas.Handle, PChar(Text), Length(Text), | |
| 28 | + | TextBox, DT_WORDBREAK or DT_INTERNAL); | |
| 31 | 29 | ``` | |
| 32 | 30 | ||
| 33 | 31 | ## Disegnare un testo con bordi netti (non antialiased) | |
| 34 | 32 | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 33 | + | Alcuni widgetsets lo supportano, nel qual caso si usa il seguente codice: | |
| 34 | + | ``` | |
| 38 | 35 | Canvas.Font.Quality := fqNonAntialiased; | |
| 39 | 36 | ``` | |
| 40 | 37 | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 38 | + | Altri widgetsets come il gtk2 non lo supportano e disegnano sempre antialiased. Di seguito una semplice procedura per disegnare un testo a bordi netti sotto gtk2. Non tiene conto di tutti i casi, ma rende l'idea: | |
| 39 | + | ``` | |
| 44 | 40 | procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string); | |
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 41 | + | var | |
| 42 | + | w,h: integer; | |
| 43 | + | IntfImg: TLazIntfImage; | |
| 44 | + | Img: TBitmap; | |
| 45 | + | dy: Integer; | |
| 46 | + | dx: Integer; | |
| 47 | + | col: TFPColor; | |
| 48 | + | FontColor: TColor; | |
| 49 | + | c: TColor; | |
| 50 | + | begin | |
| 51 | + | w:=0; | |
| 52 | + | h:=0; | |
| 53 | + | Canvas.GetTextSize(TheText,w,h); | |
| 54 | + | if (w<=0) or (h<=0) then exit; | |
| 55 | + | Img:=TBitmap.Create; | |
| 56 | + | IntfImg:=nil; | |
| 57 | + | try | |
| 58 | + | // paint text to a bitmap | |
| 59 | + | Img.Masked:=true; | |
| 60 | + | Img.SetSize(w,h); | |
| 61 | + | Img.Canvas.Brush.Style:=bsSolid; | |
| 62 | + | Img.Canvas.Brush.Color:=clWhite; | |
| 63 | + | Img.Canvas.FillRect(0,0,w,h); | |
| 64 | + | Img.Canvas.Font:=Canvas.Font; | |
| 65 | + | Img.Canvas.TextOut(0,0,TheText); | |
| 66 | + | // get memory image | |
| 67 | + | IntfImg:=Img.CreateIntfImage; | |
| 68 | + | // replace gray pixels | |
| 69 | + | FontColor:=ColorToRGB(Canvas.Font.Color); | |
| 70 | + | for dy:=0 to h-1 do begin | |
| 71 | + | for dx:=0 to w-1 do begin | |
| 72 | + | col:=IntfImg.Colors[dx,dy]; | |
| 73 | + | c:=FPColorToTColor(col); | |
| 74 | + | if c<>FontColor then | |
| 75 | + | IntfImg.Colors[dx,dy]:=colTransparent; | |
| 76 | + | end; | |
| 77 | + | end; | |
| 78 | + | // create bitmap | |
| 79 | + | Img.LoadFromIntfImage(IntfImg); | |
| 80 | + | // paint | |
| 81 | + | Canvas.Draw(x,y,Img); | |
| 82 | + | finally | |
| 83 | + | IntfImg.Free; | |
| 84 | + | Img.Free; | |
| 80 | 85 | end; | |
| 81 | 86 | end; | |
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | 87 | ``` | |
| 92 | 88 | ||
| 93 | 89 | --- | |
| 94 | - | ||
| 90 | + | *From the Free Pascal wiki, page "Graphics - Working with TCanvas/it", as the Internet Archive kept it on 2024-08-12. 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).* |
