Unable to Write to Canvas losing GDI resources, and unable to write to canvas
Scenario:
I have an application with a small status form that runs all of the time.
I made this form resizable by using a MetaFile, and drawing the display
info on a TMetaFileCanvas.
This allows for easy resizing, and strech to fit. The problem I am hiving
is loosing GDI resources, and finally unable to write to the canvas if the
form is covered by another form from any other application for a period of
time. IF the form is left visible it never has any problems. It is just
when it is covered by another form do the the problems begin to arise. I
do test that the form is showing with "not StatForm.Showing" before I call
the display update procedure. A timer is used to trigger the display update
about once a minute.
Code Snippit:
Procedure TStatForm.DoDisplay(Sender: TObject);
Const TMFW = 640; // TMetaFileCanvas Width 640
TMFH = 480; // TMetafileCanvas Height 480
Var X,Y:Integer;
TMFC:TMetaFileCanvas;
TMF:TMetaFile;
Rect:TRect;
begin
Try
TMF:=TMEtaFile.Create;
TMF.Width:=TMFW;
TMF.Height:=TMFH;
Try
TMFC:=TMetaFileCanvas.Create(TMF,0);
TMFC.Font.Name:='Arial';
// build status display stuff here
// and write on the MetaFileCanvas
Finally // Try TMFC
TMFC.Free;
End; // Try Finally
Rect.Top:=0;
Rect.Left:=0;
Rect.Right:=ClientWidth;
Rect.Bottom:=ClientHeight;
StatForm.Canvas.FillRect(Rect);
StatForm.Canvas.StretchDraw(Rect,TMF);
Finally // TRY tmf
TMF.Free;
End; //Try Finally TMF