GDI BackBuffer

I was curious today about writing a backbuffer in regular GDI. My understanding is to do this you would have to:

1. Create an HDC and Bitmap in memory based on the original HDC.
2. Select the Bitmap in memory in to the HDC in memory.
3. Draw on the HDC in memory.
4. BitBlt to the HDC, from the HDC in memory.
5. DeleteObject a few things.

I did try this, but because I have no experience with GDI (I have done GDI+ before) I am not sure that my result is correct. My code follows, as well as an image of my output in question is on the link below:

http://img407.imageshack.us/img407/4164/gdiresult.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	HDC hdc;
	HDC hdcmem;
	HBRUSH hBrush = CreateSolidBrush(RGB(255,0,0));
	HBRUSH hOldBrush;
	HBITMAP hbmBackBuffer;
	HBITMAP hbmOldBitmap;
	PAINTSTRUCT ps;

	hdc = BeginPaint(hWnd,&ps);
	
	// Create BackBuffer Stuff
	hdcmem = CreateCompatibleDC(hdc);
	hbmBackBuffer = CreateCompatibleBitmap(hdc, 160, 288);
	hbmOldBitmap = (HBITMAP)SelectObject(hdcmem,hbmBackBuffer);
	hOldBrush = (HBRUSH)SelectObject(hdcmem,hBrush);
	
	// Draw on Back Buffer
	Rectangle(hdcmem,0,0,64,64);

	// Draw on Screen
	BitBlt(hdc,0,0,160,288,hdcmem,0,0,SRCCOPY);

	// Delete Stuff
	
	DeleteObject(SelectObject(hdcmem,hbmOldBitmap));
	DeleteObject(SelectObject(hdcmem,hOldBrush));
	DeleteDC(hdcmem);

	// End Painting
	EndPaint(hWnd,&ps);


(Note: I know the buffering should not be in WM_PAINT, this is just for a simple test)

If I do have the process correct, then my guess is all the garbled mess is because its left over from memory that I have not yet written to.

If I have messed up somewhere, I am all ears. :)
Yes, it looks correct. And yes, the garbled mess is because the hbmBackBuffer has garbage memory in it.
Topic archived. No new replies allowed.