Sign in to follow this  
ursus

win32 GDI and bitmaps - I'm stuck

Recommended Posts

ursus    206
Ok, here is what I do:
#include <windows.h>

HBITMAP backbuffer;
HBITMAP tile;

HDC hdc;
HDC backbuffer_dc;
HDC tile_dc;


HBITMAP CreateTile();


LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow)
{	
	
	HWND hwnd;
    MSG msg;
    WNDCLASSEX wndclassex = {0};
	
	char class_name[] = "test";

	wndclassex.cbSize = sizeof(WNDCLASSEX);
    wndclassex.style = CS_HREDRAW | CS_VREDRAW;
    wndclassex.lpfnWndProc = WinProc;
    wndclassex.hInstance = hinstance;
	wndclassex.hIcon = LoadIcon(hinstance,IDI_APPLICATION);
    wndclassex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wndclassex.lpszClassName = class_name;
    wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW);

    RegisterClassEx(&wndclassex);


    hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, class_name, "test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700, 550, NULL, NULL, hinstance, NULL);

		if(!hwnd)
		{
			UnregisterClass(class_name,hinstance);
				return EXIT_FAILURE;
		}

    ShowWindow(hwnd, ishow);
    UpdateWindow(hwnd);
    
    	
	while(1)
	{
		
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;
				
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	UnregisterClass(class_name,hinstance);

		return msg.wParam;
}


LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    PAINTSTRUCT ps;
			
	switch(message)
    {
	    case WM_CREATE:
	    {
		    	hdc = GetDC(hwnd);
				backbuffer_dc = CreateCompatibleDC(hdc);
				tile_dc = CreateCompatibleDC(hdc);

				tile = CreateTile();						//  <- HERE I"M STUCK

				backbuffer = CreateCompatibleBitmap(hdc, 700, 500);

				backbuffer = (HBITMAP)SelectObject(backbuffer_dc,backbuffer);
				tile = (HBITMAP)SelectObject(tile_dc,tile);
	    }
	    break;
		
		case WM_PAINT:
			BeginPaint(hwnd, &ps);
			EndPaint(hwnd, &ps);
				return 0;

		case WM_CLOSE:
		case WM_DESTROY:
	
			PostQuitMessage(0);
				return 0;
    
	}

    return(DefWindowProc(hwnd, message, wparam, lparam));
}

HBITMAP CreateTile()
{
	HBITMAP tmp_tile;
	HDC tmp_tile_dc;
	HBRUSH hBrush;
	int i;
	
	// creating bitmap
	tmp_tile_dc = CreateCompatibleDC(hdc);
	tmp_tile = CreateCompatibleBitmap(hdc, 64, 64);
	tmp_tile = (HBITMAP)SelectObject(tmp_tile_dc,tmp_tile);
		
	hBrush=CreateSolidBrush(RGB (0,0,0));
	
	// setting rectangle
	RECT rc={0,0,64,64};
	
	// filling the bitmap with black color of the rectangle
	FillRect (tmp_tile_dc, &rc, hBrush);
	
	return tmp_tile;
}

I try to get a black (tile) returned by CreateTile() function but for some reason this function fails. Can anyone suggest where is my mistake? Thanks

Share this post


Link to post
Share on other sites
Colin Jeanne    1114
A bitmap may only be selected into a single DC at a time. A the end of CreateTile() you should select the original bitmap that was in that DC back into it so that you bitmap can then be used in other DCs.


HBITMAP CreateTile()
{
HBITMAP tmp_tile, old_tile;
HDC tmp_tile_dc;
HBRUSH hBrush;
int i;

// creating bitmap
tmp_tile_dc = CreateCompatibleDC(hdc);
tmp_tile = CreateCompatibleBitmap(hdc, 64, 64);
old_tile = (HBITMAP)SelectObject(tmp_tile_dc,tmp_tile);

hBrush=CreateSolidBrush(RGB (0,0,0));

// setting rectangle
RECT rc={0,0,64,64};

// filling the bitmap with black color of the rectangle
FillRect (tmp_tile_dc, &rc, hBrush);

// Restore the DC to its original state
SelectObject(tmp_tile_dc, old_tile);

// Delete the DC so you dont leak GDI resources
DeleteDC(tmp_tile_dc);

// Delete the brush so that you dont leak GDI resources
DeleteObject(hBrush);

return tmp_tile;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this