Board index » delphi » TCanvas derived class handle?

TCanvas derived class handle?

I'm creating my own class which is derived from TCanvas. In this way, I can
use all the Canvas LineTo,MoveTo etc. procedures but I can perform a
different action if I am printing instead of displaying on the screen. The
screen object is a TImage which I display after doing all the drawing on the

However, just creating a TCanvas object does not permit me to draw, I must
have to create a HDC (or something). Ideally, the handle of my derived
TCanvas class would be the same as that of the TImage.Bitmap.Canvas.

Any ideas of how to do this?

Regards, Ian

Ian Bakker
Calido Software Inc.


Re:TCanvas derived class handle?


This is off the top of my head.

The function of the TCanvas is basically to encapsulate the Windows
device context (your HDC).  You can go ahead and create a TCanvas, but
you assign the device context through setting the Handle property:

{ example : draw something on the desktop }

procedure TForm2.DrawDragRect;
var DC : HDC;
  Rect : TRect;
  Canvas : TCanvas;
  DC := GetWindowDC(GetDesktopWindow);
  Rect.Left := Left + FOldMouseX - FOffsetX; { Form2's properties }
  Rect.Top  := Top + FOldMouseY - FOffsetY;
  Rect.Right := Rect.Left + Width;
  Rect.Bottom := Rect.Top + Height;
  Canvas := TCanvas.Create;
  Canvas.Handle := DC;  { here's where it's assigned }
  Canvas.DrawFocusRect(Rect); { draw a focus rect on the desktop }

I don't remember how to get them, but printers also have device
contexts, and you will have to assign that DC to your subclassed Canvas.

Hope this helps

Jim Stanley
Jacobs Engineering

Re:TCanvas derived class handle?

Thanks, this should work.

Regards, Ian

Ian Bakker
Calido Software Inc.

Other Threads