Why do I need to assign a Canvas font to change the font size in Delphi 2009?

I have a subclass of TPanel, which I really like and very upset, this font, as a rule, does not occur at the same time:

font.size := AFontsize; font.style := AFontStyle; font.color := AFontColor; 

but it changes when I do this:

 Canvas.Font.Assign(Font); 

I did not need to do this in Delphi 7, but it seems I need to do this in 2009. What's the deal?

+6
source share
1 answer

If you draw the text on the panel using your canvas, you must set the canvas font.

Some components and / or some versions of Delphi can either intentionally or as a side effect of a previous drawing task install Canvas.Font , but you should not rely on it.

Therefore, before starting to draw text, it is recommended Canvas.Font := Font; .

The same applies to Canvas.Brush and Canvas.Pen .

 type TMyPanel = class(TCustomPanel) protected procedure Paint; override; end; procedure TMyPanel.Paint; var r: TRect; begin r := ClientRect; Canvas.Brush.Color := Color; Canvas.FillRect(r); // fill the background Canvas.Font := Font; DrawText(Canvas.Handle, 'Sample Text', -1, r, DT_SINGLELINE or DT_CENTER or DT_VCENTER or DT_EXPANDTABS or DT_NOPREFIX); end; 
+8
source

Source: https://habr.com/ru/post/899324/


All Articles