Sign in Join
Wiki copytemporary

Graphics - Working with TCanvas/it: what changed

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

OldNewLine
11### Usare il font di default della GUI
22
3-Il font di default si ottiene con il seguente codice:
4-
5-``` pascal
3+Il font di default si ottiene con il seguente codice:
4+```
65SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));
76```
87
98## Disegnare un testo delimitato in larghezza
109
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 ...
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+```
1713..
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);
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);
3129```
3230
3331## Disegnare un testo con bordi netti (non antialiased)
3432
35-Alcuni widgetsets lo supportano, nel qual caso si usa il seguente codice:
36-
37-``` pascal
33+Alcuni widgetsets lo supportano, nel qual caso si usa il seguente codice:
34+```
3835Canvas.Font.Quality := fqNonAntialiased;
3936```
4037
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
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+```
4440procedure 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;
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;
8085 end;
8186 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;
9187```
9288
9389---
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).*
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).*

Back to history