基本思路: 1.在内存中创建一张Canvas; 2.通过API将Canvas贴到图形设备上。 实现如下: Unit1.h #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <ExtCtrls.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *btn1; TPanel *pnl1; TPaintBox *pb1; void __fastcall btn1Click(TObject *Sender); private: // User declarations TCanvas* canvas; HBITMAP hBmp; HDC hMemDc; void CreateMemCanvas(int width, int height); void ReleaseMemCanvas(); public: // User declarations __fastcall TForm1(TComponent* Owner); __fastcall ~TForm1(); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif Unit1.cpp //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { CreateMemCanvas(pnl1->Width, pnl1->Height); } __fastcall TForm1::~TForm1() { ReleaseMemCanvas(); } //--------------------------------------------------------------------------- void __fastcall TForm1::btn1Click(TObject *Sender) { //TODO: int w = pb1->Width; int h = pb1->Height; canvas->Brush->Color = clBlue; canvas->Ellipse(w/2-50, h/2-50, w/2+50, h/2+50); ::BitBlt(pb1->Canvas->Handle,0,0,w,h,canvas->Handle,0,0,SRCCOPY); } //--------------------------------------------------------------------------- void TForm1::CreateMemCanvas(int width, int height) { canvas = new TCanvas; canvas->Font->Name="宋体"; hBmp = NULL; hMemDc = NULL; HDC hScrDc = ::GetDC(NULL); if (width<1500&&height<1500) hBmp = ::CreateCompatibleBitmap(hScrDc, width,height); else hBmp = ::CreateCompatibleBitmap(hScrDc, 1500,1500); hMemDc = ::CreateCompatibleDC(NULL); ::SelectObject(hMemDc,hBmp); ::ReleaseDC(NULL,hScrDc); canvas->Handle = hMemDc; // offsetX = scrBox->HorzScrollBar->Offset; // offsetY = scrBox->VertScrollBar->Offset; // //说明将哪个设备点映射到窗口原点(0,0) // ::SetViewportOrgEx(canvas->Handle, -offsetX, -offsetY, NULL); } //--------------------------------------------------------------------------- void TForm1::ReleaseMemCanvas() { canvas->Handle = NULL; ::DeleteObject(hBmp); ::DeleteDC(hMemDc); hMemDc = NULL; hBmp = NULL; } //---------------------------------------------------------------------------