Hi all
I made a new component derived from TWinControl.
I put it on a TPanel and I tried to call the PaintTo procedure of the panel. The result is the panel and its caption and my component is not painted on the canvas at all. What should I do about this?
a part of the source (as dear David asked):
Procedure TApListBox.Paint;
var
C: TCanvas;
B: TBitmap;
ItemClient: TPoint;
Begin
Try
If (FUpdating > 0) Then
Exit;
Try
BeginUpdate;
B := TBitmap.Create;
B.Width := Width;
B.Height := Height;
With B.Canvas Do Begin
Lock;
// Begin :
ItemClient := Point(IVisPanel + 3, 2);
// Draw Items
PaintItems(B.Canvas, ItemClient);
Unlock;
End;
C := TCanvas.Create;
C.Handle := GetWindowDC(Self.Handle);
C.Lock;
inherited;
C.Draw(1, 1, B);
If (RenameEdit.Visible) Then
RenameEdit.Repaint;
Finally
C.Unlock;
ReleaseDC(0, C.Handle);
C.Free;
B.Free;
Dec(FUpdating);
End;
Except
End;
End;
In your method handling the WM_PAINT message, you can NOT use the Canvas directly, because WM_PAINT specify a GDI handle (HDC) in the Message.DC parameter.
Take a look, for example, at this code snippet from TGraphicControl, which handle it as expected:
So check your WM_PAINT implementation method, and follow this code scheme.
Instead of the “Paint” method above, put your own drawing code using the Canvas property.
The “PaintTo” method will work as expected.
Another possibility is to use direct Windows API drawings, using the Message.DC handle… but I guess the above method, allowing the use of a regular Canvas, is more easy for most of us! 😉
In all cases, WM_PAINT shouldn’t be the place where Delphi components implement the painting, but only an overridden Paint method. So let your component inherits from TGraphicControl, and put all the drawing code into an overridden Paint method.