Page 1 of 1

onPaint event high cpu usage [resolved]

Posted: Fri Dec 13, 2019 2:28 am
by 18687215
Hello, I have a client application that needs to have cell-specific text formatting (e.g., bold, font colour, background). I am using the OnPaint event, but notice that the event is continuously firing and taking up high cpu usage. I tried both settings for DoubleBuffered and that did not resolve the issue.

Please advise any workarounds. Thanks!

I am using the latest TeeGrid 2019.

In the test code below, the test label starts counting continuously and does not stop. I am not moving the mouse.

Here is the onPaint code I am using:

Code: Select all

// declare the following in the form class:
//		MyData : TStringsData;
//		iCount: Longint;
//

procedure TForm1.FormShow(Sender: TObject);
begin
	MyData:= TStringsData.Create(3, 10); // 3 columns, 10 rows
	grid.Data:= MyData;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
	iCount := 0;
	
	grid.Columns[0].OnPaint := OnPaintColumn;
	grid.Columns[1].OnPaint := OnPaintColumn;
	grid.Columns[2].OnPaint := OnPaintColumn;

	mydata.Cells[0,0] := 'hello';
	mydata.Cells[1,0] := 'world';
	mydata.Cells[2,0] := '0';

	mydata.Cells[0,1] := 'special';
	mydata.Cells[1,1] := 'line';
	mydata.Cells[2,1] := '37.45';

end;


procedure TForm1.onPaintColumn(const Sender:TColumn; var AData:TRenderData; var DefaultPaint:Boolean);
var
	formatSave: TTextFormat;
begin

	if aDAta.Row = 1 then
	begin
		with (Sender.Render as TTextRender) do
		begin
			formatSave := grid.Cells.Format;
		
			defaultPaint := false;
		
			format.Font := grid.Cells.Format.Font;
			format.Font.Style := [fsBold];
			adata.Painter.SetFont(format.Font);
			
			Paint(AData);

			format.Font.Style := [];
			adata.Painter.SetFont(format.Font);
		end;
	end
	else begin
		defaultPaint := true;
	end;

	Inc(iCount);
	lblStatus.Caption := timetostr(now()) + intToStr(iCount);
end;

Re: onPaint event high cpu usage

Posted: Fri Dec 13, 2019 3:28 pm
by 18687215
Never mind. The key concept here is to instantiate the special text format, not make a reference. Here is the solution:

Code: Select all

procedure TForm1.FormShow(Sender: TObject);
begin
	MyData:= TStringsData.Create(3, 5); // 3 columns, 5 rows
	grid.Data:= MyData;

	formatBold := TTextFormat.Create(nil);
	formatBold.Font.Style := [fsBold];

	formatOriginal := grid.Cells.Format;
end;

procedure TForm1.onPaintColumn(const Sender:TColumn; var AData:TRenderData; var DefaultPaint:Boolean);
begin

< if line = ... > 

		with (Sender.Render as TTextRender) do
		begin
			defaultPaint := false;

			// background color
			Format.Brush.Visible:=True;
			Format.Brush.Color:=clGreen;

			// bold
			adata.Painter.SetFont(formatBold.Font);

			// font color
			adata.Painter.SetFontColor(clYellow);
			
			Paint(AData);

			// undo bold and font color
			adata.Painter.SetFont(formatOriginal.font);

		end;

end;