RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TWinControl.GetDeviceContext Method

Provides access to a device context for the control.

Pascal
function GetDeviceContext(var WindowHandle: HWnd): HDC; override;
C++
virtual __fastcall HDC GetDeviceContext(HWnd WindowHandle);

Call GetDeviceContext to obtain a handle to a device context for the control. 

GetDeviceContext calls the Windows API function GetDC, passing the windowed control's Handle property. It returns the window's handle in the WindowHandle parameter and the HDC as the return value. If the call is unsuccessful, the EOutOfResources exception is raised.  

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!