I'm trying to get an image of a control on a form just after it's been painted for the first time. I do this using
with anImage do begin
Width := aControl.Width;
Height := aControl.Height;
with Picture.Bitmap do
begin
SetSize(aControl.Width, aControl.Height);
aControl.PaintTo(Canvas, 0, 0);
end;
end;
which works fine in itself, but timing is crucially important. I tried a number of things (on Linux, using GTK2):
- Run the capture code above on the form's OnShow event. No dice - a black rectangle is captured.
- In the OnShow handler, call QueueAsyncCall to another method, where I run the capture code. No dice again - a black rectangle is captured.
- In the OnShow handler, I start a one-shot TTimer and run the capture code when it triggers. This works, if the timer interval is set to 100 ms on my machine. For smaller values, you get either a black rectangle or a rectangle the colour of the form background. But with a long enough interval, the control's appearance is faithfully captured.
This isn't ideal, but I couldn't find any other documented way of doing this more reliably. Does anyone know of any more reliable way of doing this capture which doesn't involve waiting for what is essentially an arbitrary length of time? The timer interval might need tweaking on slower machines, for example.
I need this because after doing the capture, I need to call a method on the control which changes its appearance. However, I need a record of the original appearance.
Any help gratefully received!