RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCanvas.Handle Property

Specifies the handle for this canvas.

Pascal
property Handle: HDC;
C++
__property HDC Handle;

The Handle property specifies the Windows GDI handle to the device context for this canvas. 

Set Handle to the HDC for the device context the canvas must draw into. When a windowed control responds to a Windows paint message, the HDC for drawing is passed in to the PaintWindow method. In other cases, an HDC can be obtained for a window by calling the GetDeviceContext method of a control. Additionally, Windows provides API calls to obtain an HDC for a printer or for a memory image. 

Read the Handle property to supplement the drawing services provided by the TCanvas object with API calls that require a handle to a device context. Most of the Windows GDI calls require an HDC. 

TCanvas does not own the HDC. Applications must create an HDC and set the Handle property. Applications must release the HDC when the canvas no longer needs it. Setting the Handle property of a canvas that already has a valid HDC will not automatically release the initial HDC.

Note: Some descendants of TCanvas, such as TControlCanvas, do own the HDC. Do not set the Handle property for these objects. They fetch and free their own Handle.
 

C++ Examples: 

 

/*
The GetFormImage allows you to easily obtain a bitmap of a
form. The following method can be added to a custom
TWinControl descendant to save itself as a bitmap.  Check the
produced BMP file to confirm.  Click the button and them
double click on the newly created foo.bmp file to view.
*/
class TMyControl : public TColorListBox
{
__published:    // IDE-managed Components
    void __fastcall SaveAsBmp(TFileName fileName);
private:    // User declarations
public:     // User declarations
    __fastcall TMyControl(TComponent* Owner);
};

TForm1 *Form1;
TMyControl *MyControl1;

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
__fastcall TMyControl::TMyControl(TComponent* Owner)
    : TColorListBox(Owner)
{
}

typedef struct {
  TLogPalette lpal;
  TPaletteEntry dummy[256];
} LogPal;

void __fastcall TMyControl::SaveAsBmp(TFileName fileName)
{
  TCanvas *tempCanvas = new TCanvas;
  try
  {
    HWND notUsed;
    tempCanvas->Handle = GetDeviceContext(notUsed);
    TImage *image2save = new TImage(Form1);
    try
    {
      image2save->Height = Height;
      image2save->Width = Width;
      TRect destRect = Rect(0,0,Width,Height);
      TRect sourceRect = destRect;
      image2save->Canvas->CopyRect(destRect, tempCanvas, sourceRect);
      LogPal SysPal;
      SysPal.lpal.palVersion = 0x300;
      SysPal.lpal.palNumEntries = 256;
      GetSystemPaletteEntries(tempCanvas->Handle,0,256,SysPal.lpal.palPalEntry);
      image2save->Picture->Bitmap->Palette = CreatePalette((const tagLOGPALETTE *)&SysPal.lpal);
      image2save->Picture->SaveToFile(fileName);
    }
    __finally
    {
     delete image2save;
    }
  }
  __finally
  {
    delete tempCanvas;
  }
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  MyControl1->SaveAsBmp("../foo.bmp");
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  MyControl1 = new TMyControl(Form1);
  MyControl1->Parent = Form1;
  MyControl1->Visible  = true;
}

 

Delphi Examples: 

{
The GetFormImage allows you to easily obtain a bitmap of a
form. The following method can be added to a custom
TWinControl descendant to save itself as a bitmap.  Check the
produced BMP file to confirm.  Click the button and them
double click on the newly created foo.bmp file to view.
}
type
  TMyControl = class(TColorListBox)
    procedure SaveAsBmp(fileName: TFileName);
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MyControl1: TMyControl;
implementation

{$R *.dfm}

type
  LogPal = record
  lpal : TLogPalette;
  dummy:Array[0..255] of TPaletteEntry;
  end;  

procedure TMyControl.SaveAsBmp(fileName: TFileName);
var
  Source: TComponent;
  SysPal : LogPal;
  tempCanvas: TCanvas;
  sourceRect, destRect: TRect;
  image2save: TImage;
  notUsed: HWND;
begin
  tempCanvas := TCanvas.Create;
  try
    tempCanvas.Handle := GetDeviceContext(notUsed);
    image2save:=TImage.create(self);
    try
      with image2save do
      begin
        Height := Self.Height;
        Width :=  Self.Width;
        destRect := Rect(0,0,Width,Height);
        sourceRect := destRect;
        Canvas.CopyRect(destRect,tempCanvas,sourceRect);
        SysPal.lPal.palVersion:=$300;
        SysPal.lPal.palNumEntries:=256;
        GetSystemPaletteEntries(
          tempCanvas.Handle,0,256,SysPal.lpal.palPalEntry);
        Picture.Bitmap.Palette:= CreatePalette(Syspal.lpal);
      end;
      image2save.Picture.SaveToFile(fileName);
    finally
     image2save.Free;
    end;
  finally
    tempCanvas.Free;
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  MyControl1.SaveAsBmp('foo.bmp');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyControl1:= TMyControl.Create(Form1);
  MyControl1.Parent:= Form1;
  MyControl1.visible := true;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!