Difference between revisions of "Graphics - Working with TCanvas"
From Free Pascal wiki
Jump to navigationJump to searchLine 10: | Line 10: | ||
<syntaxhighlight>SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));</syntaxhighlight> | <syntaxhighlight>SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));</syntaxhighlight> | ||
− | === | + | ===Disegnare un testo delimitato in larghezza=== |
− | + | Usare la routine DrawText, prima con il parametro DT_CALCRECT e poi senza. | |
<syntaxhighlight>// First calculate the text size then draw it | <syntaxhighlight>// First calculate the text size then draw it | ||
Line 22: | Line 22: | ||
TextBox, DT_WORDBREAK or DT_INTERNAL);</syntaxhighlight> | TextBox, DT_WORDBREAK or DT_INTERNAL);</syntaxhighlight> | ||
− | === | + | ===Disegnare testo a bordi netti (non antialiased)=== |
− | + | Alcuni widgetsets lo supportano impostando | |
<syntaxhighlight>Canvas.Font.Quality := fqNonAntialiased;</syntaxhighlight> | <syntaxhighlight>Canvas.Font.Quality := fqNonAntialiased;</syntaxhighlight> | ||
− | + | Nel widgetset gtk2 che non lo supporta (disegnerebbe comunque antialiased) si può utilizzare la procedura seguente. Pur non tenendo conto di tutti i casi, da un idea di come fare: | |
<syntaxhighlight>procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string); | <syntaxhighlight>procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string); |
Revision as of 19:59, 22 October 2016
│
English (en) │
français (fr) │
italiano (it) │
русский (ru) │
Using the default GUI font
!! temporary message to Nifan : could you leave this content in English please, write the Italian version in the page Graphics_-_Working_with_TCanvas/it. !!
This can be obtained with following code:
SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT));
Disegnare un testo delimitato in larghezza
Usare la routine DrawText, prima con il parametro DT_CALCRECT e poi senza.
// 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);
Disegnare testo a bordi netti (non antialiased)
Alcuni widgetsets lo supportano impostando
Canvas.Font.Quality := fqNonAntialiased;
Nel widgetset gtk2 che non lo supporta (disegnerebbe comunque antialiased) si può utilizzare la procedura seguente. Pur non tenendo conto di tutti i casi, da un idea di come fare:
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;