Sign in Join
Wiki copytemporary

Graphics - Working with TCanvas/it: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Graphics - Working with TCanvas/it

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

Back to history