Why wont my win32 program paint?

Started by
5 comments, last by Endurion 7 years, 7 months ago

Hi

I am trying to make my win32 program double buffer but it just wont display what i am drawing.

My code is:


case WM_ERASEBKGND:
		return 1;
	case WM_PAINT:
		GetClientRect(hwnd, &r);
		if (r.bottom == 0) {

			return;
		}
		int width = r.right - r.left;
		int height = r.top - r.bottom;

		// Get DC for window
		hdc = BeginPaint(hwnd, &ps);

		// Create an off-screen DC for double-buffering
		hdcMem = CreateCompatibleDC(hdc);
		hbmMem = CreateCompatibleBitmap(hdc, width, height);

		hOld = SelectObject(hdcMem, hbmMem);

		// Draw into hdcMem here
		for (int x = 0; x < 100; x++)
			for (int y = 0; y < 100; y++)
				SetPixel(hdcMem, x, y, RGB(255, 0, 0));

		// Transfer the off-screen DC to the screen
		BitBlt(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);

		// Free-up the off-screen DC
		SelectObject(hdcMem, hOld);
		DeleteObject(hbmMem);
		DeleteDC(hdcMem);

		EndPaint(hwnd, &ps);
		return 0;

Any help would be greatly appreciated.

Thanks

Advertisement

Nevermind. I was using ClientRect wrong.

Sorry about that.

Could you elaborate on how you were using it incorrectly? It will help any other poor soul using win32 who has similar issues.

int height = r.top - r.bottom;



Just a stab in the dark, but that looks upside-down to me :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

int height = r.top - r.bottom;

Just a stab in the dark, but that looks upside-down to me :-)

Now now. You're just being picky

Indie game developer - Game WIP

Strafe (Working Title) - Currently in need of another developer and modeler/graphic artist (professional & amateur's artists welcome)

Insane Software Facebook

int height = r.top - r.bottom;



Just a stab in the dark, but that looks upside-down to me :-)

haha, yep

for future reference, this is the correct way :P


GetClientRect(hwnd, &r);
		if (r.bottom == 0) {

			return;
		}
		width = r.right;
		height = r.bottom;
Just a reminder:

Don't rely on rect's top and left members being zero. For simple GetClientRect it'll pretty much always be, but the safe approach would be the one you almost had before:

width = r.right - r.left;
height = r.bottom - r.top;

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement