Главная страница | назад





Article #17361: What is the fastest way to draw graphics...

 Question and Answer Database
FAQ2361C.txt What is the fastest way to draw graphics...
Category :Windows API
Platform :All
Product :C++Builder 3.x
Question:
What is the fastest way to draw graphics to the screen, without
using DirectX?
Answer:
While the VCL does provide very fast graphic operations with a
minimum of work and overhead, there are times when direct access to
the windows GDI is necessary for the ultimate in performance, since
there is some amount of overhead in using object oriented VCL canvas
objects. The following example demonstrates creating a Window memory
dc (Display Context) to draw upon. Ideally, you will want to create
your dc and associated bitmap, pen, brush and font objects during your
application's startup, and destroy the objecs upon your applications
termination. Note that when a Windows dc is created, it automatically
has a 1 pixel by 1 pixel monochrome bitmap, a stock pen, a stock
brush, and a stock font associated with it. It is your responsibility
to create and destory additional objects you wish to associate with
the dc. You must also never destroy an object while it is associated
(selected into) a dc, and you must never delete the Windows stock
objects.
Example:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HDC dc; //handle to a display contect
HDC memdc; //handle to a display contect
HBITMAP membitmap; //handle to a bitmap
HBITMAP oldmembitmap; //handle to a bitmap
HPEN pen; //handle to a pen
HPEN oldpen; //handle to a pen
HBRUSH brush; //handle to a brush
HBRUSH oldbrush; //handle to a brush
//retrieve a handle to the screen dc so we can create one like it
dc = GetDC(0);
//create a dc that is compatable with the screen
memdc = CreateCompatibleDC(dc);
//create a bitmap surface thats compatible with the screen
//note: if you want a monochrome bitmap pass zero instead of "dc"
membitmap = CreateCompatibleBitmap(dc, 100, 100);
//release the screen dc as soon as possible
ReleaseDC(0, dc);
//select our bitmap surface into our dc and remember the old bitmap
oldmembitmap = SelectObject(memdc, membitmap);
//initialize our dc by painting it white,
//otherwise it will contain random pixel values
PatBlt(memdc,
0, 0,
100, 100,
WHITENESS);
//create a solid red pen that is 2 pixels wide
pen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
//select our new pen into the dc, and remember the old default pen
oldpen = SelectObject(memdc, pen);
//create a solid blue brush
brush = CreateSolidBrush(RGB(0, 0, 255));
//select our new brush into the dc, and remember the old brush
oldbrush = SelectObject(memdc, brush);
//lets draw a rectangle
Rectangle(memdc, 0, 0, 100, 100);
//copy form our dc to the forms canvas
BitBlt(Form1->Canvas->Handle, //destination
0, 0, //x,y start point on the form
100, 100, //the width and height of our bitmap
memdc, //the source dc
0, 0, //blt from 0,0 in the source
SRCCOPY); //COPY form source operation
//select the old default brush into our dc
SelectObject(memdc, oldbrush);
//delete the brush we created
DeleteObject(brush);
//select the old default pen into our dc
SelectObject(memdc, oldpen);
//delete the pen we created
DeleteObject(pen);
//select the old default bitmap into our dc
SelectObject(memdc, oldmembitmap);
//delete the bitmap we created
DeleteObject(membitmap);
//delete the dc we created
DeleteDC(memdc);
}
7/2/98 10:32:32 AM

Last Modified: 01-SEP-99