RAD Studio VCL Reference
|
Provides access to a device context for the control.
function GetDeviceContext(var WindowHandle: HWND): HDC; override; overload;
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. */ #include <memory> //for STL auto_ptr class 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) { std::auto_ptr<TCanvas> tempCanvas(new TCanvas); HWND notUsed; tempCanvas->Handle = GetDeviceContext(notUsed); std::auto_ptr<TImage> image2save(new TImage(Form1)); // The owner will clean this up. image2save->Height = Height; image2save->Width = Width; TRect destRect = Rect(0,0,Width,Height); TRect sourceRect = destRect; image2save->Canvas->CopyRect(destRect, tempCanvas.get(), sourceRect); LogPal SysPal; SysPal.lpal.palVersion = 0x300; SysPal.lpal.palNumEntries = 256; GetSystemPaletteEntries(tempCanvas->Handle,0,256,SysPal.lpal.palPalEntry); image2save->Picture->Bitmap->Palette = CreatePalette(dynamic_cast<const tagLOGPALETTE *>(&SysPal.lpal)); image2save->Picture->SaveToFile(fileName); } void __fastcall TForm1::Button1Click(TObject *Sender) { static_cast<TMyControl*>(MyControl1)->SaveAsBmp("../foo.bmp"); } void __fastcall TForm1::FormCreate(TObject *Sender) { TMyControl* control = new TMyControl(Form1); // The owner will clean this up. MyControl1 = control; control->Parent = this; control->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 then 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) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|