Click to See Complete Forum and Search --> : Double Buffering with DC


adam_bailey
June 19th, 2006, 06:16 PM
I know this is a subject that comes up time and time again but ive tried all day to get this thing to work and im still not there yet. Im new to both DC and Double buffering but this is the code i have so far.

PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);


HDC memDC = CreateCompatibleDC(hdc);
HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, 800, 600);


Rectangle(memDC, 100, 100, 200, 300);

BitBlt(hdc, 0, 0, 800, 600, memDC, 0, 0, SRCCOPY);


DeleteObject(hMemBmp);
DeleteDC(memDC);

EndPaint(hWnd, &ps);


It compiles without a problem but i canrt see the Rectangle

nolxev
June 19th, 2006, 06:56 PM
You have to specify NULL here:

HDC memDC = CreateCompatibleDC(NULL);

You have to select the bitmap within the DC:

HGDIOBJ old_object = SelectObject(memDC, hMemBmp);

Before delete the hMemBmp and the memDC you should select the old_object:

SelectObject(memDC, old_object);

Here is the code:

PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);

HDC memDC = CreateCompatibleDC(NULL);
HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, 800, 600);

HGDIOBJ old_object = SelectObject(memDC, hMemBmp);

Rectangle(memDC, 100, 100, 200, 300);

BitBlt(hdc, 0, 0, 800, 600, memDC, 0, 0, SRCCOPY);

SelectObject(memDC, old_object);

DeleteObject(hMemBmp);
DeleteDC(memDC);

EndPaint(hWnd, &ps);

adam_bailey
June 20th, 2006, 07:43 AM
Thanks that realy helped allot, but the result is not what I expected. Without the double buffer I get a rectangle that has a single thin black line around it. Now the screen is black with a white rectangle.

nolxev
June 20th, 2006, 09:23 AM
You should read something about GDI programming before write code or you'll lose more time making tests.

VladimirF
June 21st, 2006, 05:42 PM
Thanks that realy helped allot, but the result is not what I expected. Without the double buffer I get a rectangle that has a single thin black line around it. Now the screen is black with a white rectangle.
Check out MSDN help:The Rectangle function draws a rectangle. The rectangle is outlined by using the current pen and filled by using the current brush.
You do not select any pen or brush into you DC, so you are getting some left-overs.
If you could only inspect the content of your memory DC in the debugger!
(Pardon my shameless plug)
But wait! There is such tool: FeinViewer (http://www.feinsoftware.com/FeinViewer.php)

cindyonlyone
June 22nd, 2006, 11:22 PM
For simple drawing application, the drawcli sample is a good start I think, if you want to build much Visio like drawing application, another good choice is to XD++ MFC Library, this will save you much long time, you can find it as below:
http://www.********.net/XDFeature/feature.htm

Hope it is useful to you!
Ciny